From ce13982cfc99e7d9d53f1479b8d821a56f4820ee Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Thu, 14 Mar 2019 14:57:45 +0300 Subject: [PATCH] Kapt: Use constant value references where possible in property initializers --- .../stubs/ClassFileToSourceStubConverter.kt | 33 +++++++++---- .../testData/converter/kt18791.kt | 14 ++++++ .../testData/converter/kt18791.txt | 48 +++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt index d1044f33c5f..a8dc19944bb 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt @@ -635,17 +635,32 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati ifNonError = { signatureParser.parseFieldSignature(field.signature, treeMaker.Type(type)) } ) - val value = field.value - - val initializer = explicitInitializer - ?: convertValueOfPrimitiveTypeOrString(value) - ?: if (isFinal(field.access)) convertLiteralExpression(getDefaultValue(type)) else null - lineMappings.registerField(containingClass, field) + val initializer = explicitInitializer ?: convertPropertyInitializer(field) return treeMaker.VarDef(modifiers, treeMaker.name(name), typeExpression, initializer).keepKdocComments(field) } + private fun convertPropertyInitializer(field: FieldNode): JCExpression? { + val value = field.value + + if (value != null) { + val propertyInitializer = (kaptContext.origins[field]?.element as? KtProperty)?.initializer + if (propertyInitializer != null) { + return convertConstantValueArguments(value, listOf(propertyInitializer)) + } + + return convertValueOfPrimitiveTypeOrString(value) + } + + if (isFinal(field.access)) { + val type = Type.getType(field.desc) + return convertLiteralExpression(getDefaultValue(type)) + } + + return null + } + private fun convertMethod( method: MethodNode, containingClass: ClassNode, @@ -987,12 +1002,12 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati private fun convertAnnotationArgumentWithName(constantValue: Any?, value: ResolvedValueArgument?, name: String): JCExpression? { if (!isValidIdentifier(name)) return null - val expr = convertAnnotationArgument(constantValue, value) ?: return null + val args = value?.arguments?.mapNotNull { it.getArgumentExpression() } ?: emptyList() + val expr = convertConstantValueArguments(constantValue, args) ?: return null return treeMaker.Assign(treeMaker.SimpleName(name), expr) } - private fun convertAnnotationArgument(constantValue: Any?, value: ResolvedValueArgument?): JCExpression? { - val args = value?.arguments?.mapNotNull { it.getArgumentExpression() } ?: emptyList() + private fun convertConstantValueArguments(constantValue: Any?, args: List): JCExpression? { val singleArg = args.singleOrNull() if (constantValue.isOfPrimitiveType()) { diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.kt b/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.kt index 1991b980b0f..df97df237ae 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.kt @@ -110,6 +110,20 @@ class MyActivity { @Bind(B.id.textView) fun plainIntConstant() {} + + const val propA = B.id.textView + val propB = B.id.textView + var propC = B.id.textView + @JvmField + val propD = B.id.textView + @JvmField + var propE = B.id.textView + val propF = JJ.b.length +} + +object JJ { + val b = c() + fun c() = "42" } // EXPECTED_ERROR class B is public, should be declared in a file named B.java diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.txt b/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.txt index 91736dc51fe..a0b74f61b25 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.txt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/kt18791.txt @@ -87,6 +87,33 @@ package app; import java.lang.System; +@kotlin.Metadata() +public final class JJ { + @org.jetbrains.annotations.NotNull() + private static final java.lang.String b = null; + public static final app.JJ INSTANCE = null; + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getB() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String c() { + return null; + } + + private JJ() { + super(); + } +} + +//////////////////// + +package app; + +import java.lang.System; + @kotlin.Metadata() public final class MyActivity { @BindField(id = lib.R.id.textView) @@ -101,6 +128,12 @@ public final class MyActivity { private final int e = 0; @BindField(id = app.B.id.textView) private final int f = 0; + public final int propA = app.B.id.textView; + private final int propB = app.B.id.textView; + private int propC; + public final int propD = app.B.id.textView; + public int propE; + private final int propF = 0; @Bind(id = lib.R.id.textView) public static void a$annotations() { @@ -176,6 +209,21 @@ public final class MyActivity { public final void plainIntConstant() { } + public final int getPropB() { + return 0; + } + + public final int getPropC() { + return 0; + } + + public final void setPropC(int p0) { + } + + public final int getPropF() { + return 0; + } + public MyActivity() { super(); }