Support KClass.simpleName and built-in class literals
This commit is contained in:
@@ -2719,8 +2719,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
@Override
|
@Override
|
||||||
public Unit invoke(InstructionAdapter v) {
|
public Unit invoke(InstructionAdapter v) {
|
||||||
Type classAsmType = typeMapper.mapClass(descriptor);
|
Type classAsmType = typeMapper.mapClass(descriptor);
|
||||||
if (descriptor instanceof JavaClassDescriptor) {
|
ModuleDescriptor module = DescriptorUtils.getContainingModule(descriptor);
|
||||||
v.aconst(classAsmType);
|
if (descriptor instanceof JavaClassDescriptor || module == module.getBuiltIns().getBuiltInsModule()) {
|
||||||
|
putJavaLangClassInstance(v, classAsmType);
|
||||||
v.invokestatic(REFLECTION, "foreignKotlinClass", Type.getMethodDescriptor(K_CLASS_TYPE, getType(Class.class)), false);
|
v.invokestatic(REFLECTION, "foreignKotlinClass", Type.getMethodDescriptor(K_CLASS_TYPE, getType(Class.class)), false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
public class javaClassLiteral {
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import javaClassLiteral as J
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val j = J::class
|
||||||
|
if (j.simpleName != "javaClassLiteral") return "Fail: ${j.simpleName}"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
assertEquals("Any", Any::class.simpleName)
|
||||||
|
assertEquals("String", String::class.simpleName)
|
||||||
|
assertEquals("CharSequence", CharSequence::class.simpleName)
|
||||||
|
assertEquals("Number", Number::class.simpleName)
|
||||||
|
assertEquals("Int", Int::class.simpleName)
|
||||||
|
assertEquals("Long", Long::class.simpleName)
|
||||||
|
|
||||||
|
assertEquals("Default", Int.Default::class.simpleName)
|
||||||
|
assertEquals("Default", Double.Default::class.simpleName)
|
||||||
|
|
||||||
|
assertEquals("IntRange", IntRange::class.simpleName)
|
||||||
|
|
||||||
|
assertEquals("List", List::class.simpleName)
|
||||||
|
|
||||||
|
// TODO: this is wrong but should be fixed
|
||||||
|
assertEquals("List", MutableList::class.simpleName)
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
class Klass
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val x = Klass::class
|
||||||
|
if (x.simpleName != "Klass") return "Fail x: ${x.simpleName}"
|
||||||
|
|
||||||
|
val y = java.util.Date::class
|
||||||
|
if (y.simpleName != "Date") return "Fail y: ${y.simpleName}"
|
||||||
|
|
||||||
|
val z = kotlin.jvm.internal.KotlinSyntheticClass.Kind::class
|
||||||
|
if (z.simpleName != "Kind") return "Fail z: ${z.simpleName}"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+16
@@ -331,6 +331,7 @@ public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxCod
|
|||||||
@TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection")
|
@TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@InnerTestClasses({
|
@InnerTestClasses({
|
||||||
|
Reflection.ClassLiterals.class,
|
||||||
Reflection.Mapping.class,
|
Reflection.Mapping.class,
|
||||||
Reflection.Properties.class,
|
Reflection.Properties.class,
|
||||||
})
|
})
|
||||||
@@ -340,6 +341,21 @@ public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxCod
|
|||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxAgainstJava/reflection"), Pattern.compile("^(.+)\\.kt$"), true);
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxAgainstJava/reflection"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection/classLiterals")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class ClassLiterals extends AbstractBlackBoxCodegenTest {
|
||||||
|
public void testAllFilesPresentInClassLiterals() throws Exception {
|
||||||
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxAgainstJava/reflection/classLiterals"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("javaClassLiteral.kt")
|
||||||
|
public void testJavaClassLiteral() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxAgainstJava/reflection/classLiterals/javaClassLiteral.kt");
|
||||||
|
doTestAgainstJava(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection/mapping")
|
@TestMetadata("compiler/testData/codegen/boxAgainstJava/reflection/mapping")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
+12
@@ -2545,6 +2545,18 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
|||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/reflection/classLiterals"), Pattern.compile("^(.+)\\.kt$"), true);
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/reflection/classLiterals"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("builtinClassLiterals.kt")
|
||||||
|
public void testBuiltinClassLiterals() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/classLiterals/builtinClassLiterals.kt");
|
||||||
|
doTestWithStdlib(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("classSimpleName.kt")
|
||||||
|
public void testClassSimpleName() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/classLiterals/classSimpleName.kt");
|
||||||
|
doTestWithStdlib(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleClassLiteral.kt")
|
@TestMetadata("simpleClassLiteral.kt")
|
||||||
public void testSimpleClassLiteral() throws Exception {
|
public void testSimpleClassLiteral() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/classLiterals/simpleClassLiteral.kt");
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/classLiterals/simpleClassLiteral.kt");
|
||||||
|
|||||||
@@ -38,6 +38,11 @@ class KClassImpl<T>(override val jClass: Class<T>) : KCallableContainerImpl(), K
|
|||||||
|
|
||||||
override val scope: JetScope get() = descriptor.getDefaultType().getMemberScope()
|
override val scope: JetScope get() = descriptor.getDefaultType().getMemberScope()
|
||||||
|
|
||||||
|
override val simpleName: String? get() {
|
||||||
|
val name = descriptor.getName()
|
||||||
|
return if (name.isSpecial()) null else name.asString()
|
||||||
|
}
|
||||||
|
|
||||||
override fun getProperties(): Collection<KMemberProperty<T, *>> {
|
override fun getProperties(): Collection<KMemberProperty<T, *>> {
|
||||||
return scope.getAllDescriptors().stream()
|
return scope.getAllDescriptors().stream()
|
||||||
.filterIsInstance<PropertyDescriptor>()
|
.filterIsInstance<PropertyDescriptor>()
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
package kotlin.reflect
|
package kotlin.reflect
|
||||||
|
|
||||||
public trait KClass<T> {
|
public trait KClass<T> {
|
||||||
|
public val simpleName: String?
|
||||||
|
|
||||||
public fun getProperties(): Collection<KMemberProperty<T, *>>
|
public fun getProperties(): Collection<KMemberProperty<T, *>>
|
||||||
|
|
||||||
public fun getExtensionProperties(): Collection<KMemberExtensionProperty<T, *, *>>
|
public fun getExtensionProperties(): Collection<KMemberExtensionProperty<T, *, *>>
|
||||||
|
|||||||
Reference in New Issue
Block a user