Kapt: Use constant value references where possible in property initializers
This commit is contained in:
+24
-9
@@ -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<KtExpression>): JCExpression? {
|
||||
val singleArg = args.singleOrNull()
|
||||
|
||||
if (constantValue.isOfPrimitiveType()) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user