From 7e69a5ac9a79b81835fcdc22386e260aaeff752a Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 19 Mar 2015 20:51:24 +0300 Subject: [PATCH] Don't create KProperty instance for synthetic field #KT-5759 Fixed --- .../reflection/mapping/syntheticFields.kt | 14 ++++++++++++++ .../BlackBoxWithStdlibCodegenTestGenerated.java | 6 ++++++ .../src/kotlin/reflect/jvm/mapping.kt | 9 ++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/boxWithStdlib/reflection/mapping/syntheticFields.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/mapping/syntheticFields.kt b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/syntheticFields.kt new file mode 100644 index 00000000000..6bd07fc4bed --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/mapping/syntheticFields.kt @@ -0,0 +1,14 @@ +import kotlin.reflect.jvm.kotlin + +class A { + // There's a synthetic "$kotlinClass" field here +} + +fun box(): String { + for (field in javaClass().getDeclaredFields()) { + val prop = field.kotlin + if (prop != null) return "Fail, property found: $prop" + } + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java index 674cb078a2a..f8012c0beb4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java @@ -2782,6 +2782,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode doTestWithStdlib(fileName); } + @TestMetadata("syntheticFields.kt") + public void testSyntheticFields() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/syntheticFields.kt"); + doTestWithStdlib(fileName); + } + @TestMetadata("topLevelProperty.kt") public void testTopLevelProperty() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/mapping/topLevelProperty.kt"); diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/mapping.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/mapping.kt index bb683ef81db..e29e44245e5 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/mapping.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/mapping.kt @@ -118,12 +118,15 @@ public val Class<*>.kotlinPackage: KPackage get() = KPackageImpl(this) -// TODO: make nullable to filter out synthetic fields (KT-5759) /** - * Returns a [KProperty] instance corresponding to the given Java [Field] instance. + * Returns a [KProperty] instance corresponding to the given Java [Field] instance, + * or `null` if this field cannot be represented by a Kotlin property + * (for example, if it is a synthetic field). */ -public val Field.kotlin: KProperty<*> +public val Field.kotlin: KProperty<*>? get() { + if (isSynthetic()) return null + val clazz = getDeclaringClass() val name = getName() val modifiers = getModifiers()