diff --git a/compiler/android-tests/tests/org/jetbrains/jet/compiler/android/SpecialFiles.java b/compiler/android-tests/tests/org/jetbrains/jet/compiler/android/SpecialFiles.java index d162776ed5a..045a3426999 100644 --- a/compiler/android-tests/tests/org/jetbrains/jet/compiler/android/SpecialFiles.java +++ b/compiler/android-tests/tests/org/jetbrains/jet/compiler/android/SpecialFiles.java @@ -61,6 +61,7 @@ public class SpecialFiles { excludedFiles.add("boxMultiFile"); // MultiFileTest not supported yet excludedFiles.add("boxInline"); // MultiFileTest not supported yet + excludedFiles.add("reflection"); excludedFiles.add("kt3238.kt"); // Reflection excludedFiles.add("kt1482_2279.kt"); // Reflection @@ -69,11 +70,6 @@ public class SpecialFiles { excludedFiles.add("packageQualifiedMethod.kt"); // Cannot change package name excludedFiles.add("classObjectToString.kt"); // Cannot change package name - /* Reflection tests with full-qualified names*/ - excludedFiles.add("insideLambda"); - excludedFiles.add("lambda"); - excludedFiles.add("kt5112.kt"); - excludedFiles.add("kt326.kt"); // Commented excludedFiles.add("kt1213.kt"); // Commented diff --git a/compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.java b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.java new file mode 100644 index 00000000000..0256b2f75f7 --- /dev/null +++ b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.java @@ -0,0 +1 @@ +public class jClass2kClass {} diff --git a/compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.kt b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.kt new file mode 100644 index 00000000000..414ae0ca5e1 --- /dev/null +++ b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.kt @@ -0,0 +1,11 @@ +import jClass2kClass as J + +import kotlin.reflect.jvm.* +import kotlin.test.assertEquals + +fun box(): String { + val j = javaClass() + assertEquals(j, j.kotlin.java) + + return "OK" +} diff --git a/compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.java b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.java new file mode 100644 index 00000000000..0377951bd4b --- /dev/null +++ b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.java @@ -0,0 +1,9 @@ +public class javaFields { + public final int i; + public String s; + + public javaFields(int i, String s) { + this.i = i; + this.s = s; + } +} diff --git a/compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.kt b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.kt new file mode 100644 index 00000000000..a5de7e5bf12 --- /dev/null +++ b/compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.kt @@ -0,0 +1,43 @@ +import javaFields as J + +import java.lang.reflect.* +import kotlin.reflect.* +import kotlin.reflect.jvm.* + +fun box(): String { + val i = J::i + val s = J::s + + // Check that correct reflection objects are created + assert(i.javaClass.getSimpleName() == "KForeignMemberProperty", "Fail i class") + assert(s.javaClass.getSimpleName() == "KMutableForeignMemberProperty", "Fail s class") + + // Check that no Method objects are created for such properties + assert(i.javaGetter == null, "Fail i getter") + assert(s.javaGetter == null, "Fail s getter") + assert(s.javaSetter == null, "Fail s setter") + + // Check that correct Field objects are created + val ji = i.javaField!! + val js = s.javaField!! + assert(Modifier.isFinal(ji.getModifiers()), "Fail i final") + assert(!Modifier.isFinal(js.getModifiers()), "Fail s final") + + // Check that those Field objects work as expected + val a = J(42, "abc") + assert(ji.get(a) == 42, "Fail ji get") + assert(js.get(a) == "abc", "Fail js get") + js.set(a, "def") + assert(js.get(a) == "def", "Fail js set") + assert(a.s == "def", "Fail js access") + + // Check that valid Kotlin reflection objects are created by those Field objects + val ki = ji.kotlin as KMemberProperty + val ks = js.kotlin as KMutableMemberProperty + assert(ki.get(a) == 42, "Fail ki get") + assert(ks.get(a) == "def", "Fail ks get") + ks.set(a, "ghi") + assert(ks.get(a) == "ghi", "Fail ks set") + + return "OK" +} diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/insideLambda/classInLambda.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda/classInLambda.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/insideLambda/classInLambda.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda/classInLambda.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/insideLambda/objectInLambda.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda/objectInLambda.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/insideLambda/objectInLambda.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda/objectInLambda.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInConstructor.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInConstructor.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInConstructor.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInConstructor.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInFunction.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInFunction.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInFunction.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInFunction.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLambda.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLambda.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLambda.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLambda.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLocalClass.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLocalClass.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLocalClass.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLocalClass.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLocalFunction.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLocalFunction.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLocalFunction.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLocalFunction.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunction.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunction.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunction.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunction.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunctionInLocalClass.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunctionInLocalClass.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunctionInLocalClass.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunctionInLocalClass.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunctionInNestedClass.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunctionInNestedClass.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunctionInNestedClass.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunctionInNestedClass.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInObjectExpression.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInObjectExpression.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInObjectExpression.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInObjectExpression.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPackage.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPackage.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPackage.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPackage.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPropertyGetter.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPropertyGetter.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPropertyGetter.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPropertyGetter.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPropertySetter.kt b/compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPropertySetter.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPropertySetter.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPropertySetter.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/kt5112.kt b/compiler/testData/codegen/boxWithStdlib/reflection/genericSignature/kt5112.kt similarity index 100% rename from compiler/testData/codegen/boxWithStdlib/reflection/kt5112.kt rename to compiler/testData/codegen/boxWithStdlib/reflection/genericSignature/kt5112.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/mapping/extensionProperty.kt b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/extensionProperty.kt new file mode 100644 index 00000000000..9d470a193d0 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/extensionProperty.kt @@ -0,0 +1,27 @@ +import kotlin.reflect.jvm.* +import kotlin.test.assertEquals + +class K(var value: Long) + +var K.ext: Double + get() = value.toDouble() + set(value) { + this.value = value.toLong() + } + +fun box(): String { + val p = K::ext + + val getter = p.javaGetter!! + val setter = p.javaSetter!! + + assertEquals(getter, Class.forName("_DefaultPackage").getMethod("getExt", javaClass())) + assertEquals(setter, Class.forName("_DefaultPackage").getMethod("setExt", javaClass(), javaClass())) + + val k = K(42L) + assert(getter.invoke(null, k) == 42.0, "Fail k getter") + setter.invoke(null, k, -239.0) + assert(getter.invoke(null, k) == -239.0, "Fail k setter") + + return "OK" +} diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/mapping/memberProperty.kt b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/memberProperty.kt new file mode 100644 index 00000000000..e39bb09d767 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/memberProperty.kt @@ -0,0 +1,23 @@ +import kotlin.reflect.jvm.* +import kotlin.test.assertEquals + +class K(var value: Long) + +fun box(): String { + val p = K::value + + assert(p.javaField != null, "Fail p field") + + val getter = p.javaGetter!! + val setter = p.javaSetter!! + + assertEquals(getter, javaClass().getMethod("getValue")) + assertEquals(setter, javaClass().getMethod("setValue", javaClass())) + + val k = K(42L) + assert(getter.invoke(k) == 42L, "Fail k getter") + setter.invoke(k, -239L) + assert(getter.invoke(k) == -239L, "Fail k setter") + + return "OK" +} diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/mapping/package.kt b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/package.kt new file mode 100644 index 00000000000..07d168a5a55 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/package.kt @@ -0,0 +1,13 @@ +package test + +import kotlin.reflect.jvm.* +import kotlin.test.* + +fun box(): String { + val facadeJClass = Class.forName("test.TestPackage") as Class + + assertEquals(facadeJClass, facadeJClass.kotlinPackage.javaFacade) + assertEquals(facadeJClass, facadeJClass.kotlin.java) + + return "OK" +} diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/mapping/topLevelProperty.kt b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/topLevelProperty.kt new file mode 100644 index 00000000000..ada90d5c564 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/topLevelProperty.kt @@ -0,0 +1,23 @@ +import kotlin.reflect.jvm.* +import kotlin.test.assertEquals + +var topLevel = "123" + +fun box(): String { + val p = ::topLevel + + // TODO: uncomment when fields are loaded from package parts + // assert(p.javaField != null, "Fail p field") + + val getter = p.javaGetter!! + val setter = p.javaSetter!! + + assertEquals(getter, Class.forName("_DefaultPackage").getMethod("getTopLevel")) + assertEquals(setter, Class.forName("_DefaultPackage").getMethod("setTopLevel", javaClass())) + + assert(getter.invoke(null) == "123", "Fail k getter") + setter.invoke(null, "456") + assert(getter.invoke(null) == "456", "Fail k setter") + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/AbstractBlackBoxCodegenTest.java b/compiler/tests/org/jetbrains/jet/codegen/generated/AbstractBlackBoxCodegenTest.java index 637e5a27c34..bc086effe2a 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/AbstractBlackBoxCodegenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/AbstractBlackBoxCodegenTest.java @@ -97,7 +97,8 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase { File javaClassesTempDirectory = compileJava(ktFile.replaceFirst("\\.kt$", ".java")); myEnvironment = JetCoreEnvironment.createForTests(getTestRootDisposable(), JetTestUtils.compilerConfigurationForTests( - ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, JetTestUtils.getAnnotationsJar(), javaClassesTempDirectory)); + ConfigurationKind.ALL, TestJdkKind.FULL_JDK, JetTestUtils.getAnnotationsJar(), javaClassesTempDirectory + )); loadFile(ktFile); blackBox(); diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxAgainstJavaCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxAgainstJavaCodegenTestGenerated.java index 22a8556dee2..ae1fa6b0a62 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxAgainstJavaCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxAgainstJavaCodegenTestGenerated.java @@ -31,7 +31,7 @@ import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") @TestMetadata("compiler/testData/codegen/boxAgainstJava") -@InnerTestClasses({BlackBoxAgainstJavaCodegenTestGenerated.Annotations.class, BlackBoxAgainstJavaCodegenTestGenerated.CallableReference.class, BlackBoxAgainstJavaCodegenTestGenerated.Constructor.class, BlackBoxAgainstJavaCodegenTestGenerated.Delegation.class, BlackBoxAgainstJavaCodegenTestGenerated.Enum.class, BlackBoxAgainstJavaCodegenTestGenerated.Functions.class, BlackBoxAgainstJavaCodegenTestGenerated.InnerClass.class, BlackBoxAgainstJavaCodegenTestGenerated.Property.class, BlackBoxAgainstJavaCodegenTestGenerated.Sam.class, BlackBoxAgainstJavaCodegenTestGenerated.StaticFun.class, BlackBoxAgainstJavaCodegenTestGenerated.Visibility.class}) +@InnerTestClasses({BlackBoxAgainstJavaCodegenTestGenerated.Annotations.class, BlackBoxAgainstJavaCodegenTestGenerated.CallableReference.class, BlackBoxAgainstJavaCodegenTestGenerated.Constructor.class, BlackBoxAgainstJavaCodegenTestGenerated.Delegation.class, BlackBoxAgainstJavaCodegenTestGenerated.Enum.class, BlackBoxAgainstJavaCodegenTestGenerated.Functions.class, BlackBoxAgainstJavaCodegenTestGenerated.InnerClass.class, BlackBoxAgainstJavaCodegenTestGenerated.Property.class, BlackBoxAgainstJavaCodegenTestGenerated.Reflection.class, BlackBoxAgainstJavaCodegenTestGenerated.Sam.class, BlackBoxAgainstJavaCodegenTestGenerated.StaticFun.class, BlackBoxAgainstJavaCodegenTestGenerated.Visibility.class}) public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testAllFilesPresentInBoxAgainstJava() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxAgainstJava"), Pattern.compile("^(.+)\\.kt$"), true); @@ -236,6 +236,39 @@ public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxCod } + @TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection") + @InnerTestClasses({Reflection.Mapping.class}) + public static class Reflection extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInReflection() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxAgainstJava/reflection"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection/mapping") + public static class Mapping extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInMapping() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxAgainstJava/reflection/mapping"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("jClass2kClass.kt") + public void testJClass2kClass() throws Exception { + doTestAgainstJava("compiler/testData/codegen/boxAgainstJava/reflection/mapping/jClass2kClass.kt"); + } + + @TestMetadata("javaFields.kt") + public void testJavaFields() throws Exception { + doTestAgainstJava("compiler/testData/codegen/boxAgainstJava/reflection/mapping/javaFields.kt"); + } + + } + + public static Test innerSuite() { + TestSuite suite = new TestSuite("Reflection"); + suite.addTestSuite(Reflection.class); + suite.addTestSuite(Mapping.class); + return suite; + } + } + @TestMetadata("compiler/testData/codegen/boxAgainstJava/sam") @InnerTestClasses({Sam.Adapters.class}) public static class Sam extends AbstractBlackBoxCodegenTest { @@ -609,6 +642,7 @@ public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxCod suite.addTestSuite(Functions.class); suite.addTestSuite(InnerClass.class); suite.addTestSuite(Property.class); + suite.addTest(Reflection.innerSuite()); suite.addTest(Sam.innerSuite()); suite.addTestSuite(StaticFun.class); suite.addTest(Visibility.innerSuite()); diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java index 2297e6821c8..51f5815cc53 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java @@ -1381,99 +1381,151 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode } @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection") - @InnerTestClasses({Reflection.InsideLambda.class, Reflection.Lambda.class}) + @InnerTestClasses({Reflection.Enclosing.class, Reflection.GenericSignature.class, Reflection.Mapping.class}) public static class Reflection extends AbstractBlackBoxCodegenTest { public void testAllFilesPresentInReflection() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection"), Pattern.compile("^(.+)\\.kt$"), true); } - @TestMetadata("kt5112.kt") - public void testKt5112() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/kt5112.kt"); + @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/enclosing") + @InnerTestClasses({Enclosing.InsideLambda.class, Enclosing.Lambda.class}) + public static class Enclosing extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInEnclosing() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/enclosing"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda") + public static class InsideLambda extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInInsideLambda() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("classInLambda.kt") + public void testClassInLambda() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda/classInLambda.kt"); + } + + @TestMetadata("objectInLambda.kt") + public void testObjectInLambda() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/insideLambda/objectInLambda.kt"); + } + + } + + @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda") + public static class Lambda extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInLambda() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("lambdaInConstructor.kt") + public void testLambdaInConstructor() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInConstructor.kt"); + } + + @TestMetadata("lambdaInFunction.kt") + public void testLambdaInFunction() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInFunction.kt"); + } + + @TestMetadata("lambdaInLambda.kt") + public void testLambdaInLambda() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLambda.kt"); + } + + @TestMetadata("lambdaInLocalClass.kt") + public void testLambdaInLocalClass() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLocalClass.kt"); + } + + @TestMetadata("lambdaInLocalFunction.kt") + public void testLambdaInLocalFunction() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInLocalFunction.kt"); + } + + @TestMetadata("lambdaInMemberFunction.kt") + public void testLambdaInMemberFunction() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunction.kt"); + } + + @TestMetadata("lambdaInMemberFunctionInLocalClass.kt") + public void testLambdaInMemberFunctionInLocalClass() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunctionInLocalClass.kt"); + } + + @TestMetadata("lambdaInMemberFunctionInNestedClass.kt") + public void testLambdaInMemberFunctionInNestedClass() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInMemberFunctionInNestedClass.kt"); + } + + @TestMetadata("lambdaInObjectExpression.kt") + public void testLambdaInObjectExpression() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInObjectExpression.kt"); + } + + @TestMetadata("lambdaInPackage.kt") + public void testLambdaInPackage() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPackage.kt"); + } + + @TestMetadata("lambdaInPropertyGetter.kt") + public void testLambdaInPropertyGetter() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPropertyGetter.kt"); + } + + @TestMetadata("lambdaInPropertySetter.kt") + public void testLambdaInPropertySetter() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInPropertySetter.kt"); + } + + } + + public static Test innerSuite() { + TestSuite suite = new TestSuite("Enclosing"); + suite.addTestSuite(Enclosing.class); + suite.addTestSuite(InsideLambda.class); + suite.addTestSuite(Lambda.class); + return suite; + } } - @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/insideLambda") - public static class InsideLambda extends AbstractBlackBoxCodegenTest { - public void testAllFilesPresentInInsideLambda() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/insideLambda"), Pattern.compile("^(.+)\\.kt$"), true); + @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/genericSignature") + public static class GenericSignature extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInGenericSignature() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/genericSignature"), Pattern.compile("^(.+)\\.kt$"), true); } - @TestMetadata("classInLambda.kt") - public void testClassInLambda() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/insideLambda/classInLambda.kt"); - } - - @TestMetadata("objectInLambda.kt") - public void testObjectInLambda() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/insideLambda/objectInLambda.kt"); + @TestMetadata("kt5112.kt") + public void testKt5112() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/genericSignature/kt5112.kt"); } } - @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/lambda") - public static class Lambda extends AbstractBlackBoxCodegenTest { - public void testAllFilesPresentInLambda() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/lambda"), Pattern.compile("^(.+)\\.kt$"), true); + @TestMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping") + public static class Mapping extends AbstractBlackBoxCodegenTest { + public void testAllFilesPresentInMapping() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/codegen/boxWithStdlib/reflection/mapping"), Pattern.compile("^(.+)\\.kt$"), true); } - @TestMetadata("lambdaInConstructor.kt") - public void testLambdaInConstructor() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInConstructor.kt"); + @TestMetadata("extensionProperty.kt") + public void testExtensionProperty() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/mapping/extensionProperty.kt"); } - @TestMetadata("lambdaInFunction.kt") - public void testLambdaInFunction() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInFunction.kt"); + @TestMetadata("memberProperty.kt") + public void testMemberProperty() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/mapping/memberProperty.kt"); } - @TestMetadata("lambdaInLambda.kt") - public void testLambdaInLambda() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLambda.kt"); + @TestMetadata("package.kt") + public void testPackage() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/mapping/package.kt"); } - @TestMetadata("lambdaInLocalClass.kt") - public void testLambdaInLocalClass() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLocalClass.kt"); - } - - @TestMetadata("lambdaInLocalFunction.kt") - public void testLambdaInLocalFunction() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInLocalFunction.kt"); - } - - @TestMetadata("lambdaInMemberFunction.kt") - public void testLambdaInMemberFunction() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunction.kt"); - } - - @TestMetadata("lambdaInMemberFunctionInLocalClass.kt") - public void testLambdaInMemberFunctionInLocalClass() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunctionInLocalClass.kt"); - } - - @TestMetadata("lambdaInMemberFunctionInNestedClass.kt") - public void testLambdaInMemberFunctionInNestedClass() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInMemberFunctionInNestedClass.kt"); - } - - @TestMetadata("lambdaInObjectExpression.kt") - public void testLambdaInObjectExpression() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInObjectExpression.kt"); - } - - @TestMetadata("lambdaInPackage.kt") - public void testLambdaInPackage() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPackage.kt"); - } - - @TestMetadata("lambdaInPropertyGetter.kt") - public void testLambdaInPropertyGetter() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPropertyGetter.kt"); - } - - @TestMetadata("lambdaInPropertySetter.kt") - public void testLambdaInPropertySetter() throws Exception { - doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/lambda/lambdaInPropertySetter.kt"); + @TestMetadata("topLevelProperty.kt") + public void testTopLevelProperty() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/reflection/mapping/topLevelProperty.kt"); } } @@ -1481,8 +1533,9 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode public static Test innerSuite() { TestSuite suite = new TestSuite("Reflection"); suite.addTestSuite(Reflection.class); - suite.addTestSuite(InsideLambda.class); - suite.addTestSuite(Lambda.class); + suite.addTest(Enclosing.innerSuite()); + suite.addTestSuite(GenericSignature.class); + suite.addTestSuite(Mapping.class); return suite; } } diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/Intrinsic.kt b/core/runtime.jvm/src/kotlin/jvm/internal/Intrinsic.kt new file mode 100644 index 00000000000..8fd8b0176d4 --- /dev/null +++ b/core/runtime.jvm/src/kotlin/jvm/internal/Intrinsic.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.jvm.internal + +import java.lang.annotation.* + +Retention(RetentionPolicy.RUNTIME) +public annotation class Intrinsic(val value: String) diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt index 442d7f9fcb6..178806178de 100644 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt @@ -17,6 +17,8 @@ package kotlin.reflect.jvm.internal import kotlin.reflect.* +import kotlin.jvm.internal.KotlinClass +import kotlin.jvm.internal.KotlinSyntheticClass enum class KClassOrigin { BUILT_IN @@ -24,8 +26,8 @@ enum class KClassOrigin { FOREIGN } -private val KOTLIN_CLASS_ANNOTATION_CLASS = Class.forName("kotlin.jvm.internal.KotlinClass") as Class -private val KOTLIN_SYNTHETIC_CLASS_ANNOTATION_CLASS = Class.forName("kotlin.jvm.internal.KotlinSyntheticClass") as Class +private val KOTLIN_CLASS_ANNOTATION_CLASS = javaClass() +private val KOTLIN_SYNTHETIC_CLASS_ANNOTATION_CLASS = javaClass() class KClassImpl(val jClass: Class) : KClass { // TODO: write metadata to local classes diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt index c19eafafd96..7e16b2479c0 100644 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/KPackageImpl.kt @@ -15,8 +15,7 @@ */ package kotlin.reflect.jvm.internal -import kotlin.reflect.* -class KPackageImpl( - val jClass: Class<*> -) : KPackage +import kotlin.reflect.KPackage + +class KPackageImpl(val jClass: Class<*>) : KPackage diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/util.kt b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/util.kt index 84d6b0a3120..0fbf5803908 100644 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/util.kt +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/util.kt @@ -17,6 +17,7 @@ package kotlin.reflect.jvm.internal import java.lang.reflect.Method +import kotlin.jvm.internal.Intrinsic // TODO: use stdlib? suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") @@ -34,6 +35,11 @@ private fun getterName(propertyName: String): String = "get" + propertyName.capi private fun setterName(propertyName: String): String = "set" + propertyName.capitalizeWithJavaBeanConvention() +// A local copy of javaClass() from stdlib is needed because there's no dependency on stdlib in runtime.jvm +[Intrinsic("kotlin.javaClass.function")] +fun javaClass(): Class = throw UnsupportedOperationException() + + private fun Class<*>.getMaybeDeclaredMethod(name: String, vararg parameterTypes: Class<*>): Method { try { return getMethod(name, *parameterTypes) diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/mapping.kt b/core/runtime.jvm/src/kotlin/reflect/jvm/mapping.kt new file mode 100644 index 00000000000..b40f1d6c24c --- /dev/null +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/mapping.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.reflect.jvm + +import java.lang.reflect.* +import kotlin.reflect.* +import kotlin.reflect.jvm.internal.* + +// Kotlin reflection -> Java reflection + +public val KClass.java: Class + get() = (this as KClassImpl).jClass + +public val KPackage.javaFacade: Class<*> + get() = (this as KPackageImpl).jClass + + +public val KProperty<*>.javaGetter: Method? + get() = (this as? KPropertyImpl<*>)?.getter + +public val KMutableProperty<*>.javaSetter: Method? + get() = (this as? KMutablePropertyImpl<*>)?.setter + + +// TODO: val KTopLevelVariable<*>.javaField: Field + +public val KTopLevelVariable<*>.javaGetter: Method + get() = (this as KTopLevelVariableImpl<*>).getter + +public val KMutableTopLevelVariable<*>.javaSetter: Method + get() = (this as KMutableTopLevelVariableImpl<*>).setter + + +public val KTopLevelExtensionProperty<*, *>.javaGetter: Method + get() = (this as KTopLevelExtensionPropertyImpl<*, *>).getter + +public val KMutableTopLevelExtensionProperty<*, *>.javaSetter: Method + get() = (this as KMutableTopLevelExtensionPropertyImpl<*, *>).setter + + +public val KMemberProperty<*, *>.javaField: Field? + get() = (this as KPropertyImpl<*>).field + + +// Java reflection -> Kotlin reflection + +public val Class.kotlin: KClass + get() = kClass(this) + +public val Class<*>.kotlinPackage: KPackage + get() = kPackage(this) + + +public val Field.kotlin: KProperty<*> + get() { + val clazz = getDeclaringClass() + val name = getName()!! + val modifiers = getModifiers() + val static = Modifier.isStatic(modifiers) + val final = Modifier.isFinal(modifiers) + if (static) { + val kPackage = kPackage(clazz) + return if (final) topLevelVariable(name, kPackage) else mutableTopLevelVariable(name, kPackage) + } + else { + val kClass = kClass(clazz as Class) + return if (final) kClass.memberProperty(name) else kClass.mutableMemberProperty(name) + } + } + diff --git a/libraries/stdlib/src/kotlin/jvm/internal/Intrinsic.kt b/libraries/stdlib/src/kotlin/jvm/internal/Intrinsic.kt deleted file mode 100644 index 05fc4ad46c8..00000000000 --- a/libraries/stdlib/src/kotlin/jvm/internal/Intrinsic.kt +++ /dev/null @@ -1,6 +0,0 @@ -package kotlin.jvm.internal - -import java.lang.annotation.* - -Retention(RetentionPolicy.RUNTIME) -annotation class Intrinsic(val value: String)