Kapt: Support default constant values for constructor parameters

This commit is contained in:
Yan Zhulanow
2019-03-15 15:41:39 +03:00
parent 10e53a2e73
commit e4d758b614
7 changed files with 55 additions and 12 deletions
@@ -651,24 +651,28 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
val value = field.value val value = field.value
val origin = kaptContext.origins[field] val origin = kaptContext.origins[field]
val propertyInitializer = (origin?.element as? KtProperty)?.initializer val initializer = when (val element = origin?.element) {
is KtProperty -> element.initializer
is KtParameter -> element.defaultValue
else -> null
}
if (value != null) { if (value != null) {
if (propertyInitializer != null) { if (initializer != null) {
return convertConstantValueArguments(value, listOf(propertyInitializer)) return convertConstantValueArguments(value, listOf(initializer))
} }
return convertValueOfPrimitiveTypeOrString(value) return convertValueOfPrimitiveTypeOrString(value)
} }
val propertyType = (origin?.descriptor as? PropertyDescriptor)?.returnType val propertyType = (origin?.descriptor as? PropertyDescriptor)?.returnType
if (propertyInitializer != null && propertyType != null) { if (initializer != null && propertyType != null) {
val moduleDescriptor = kaptContext.generationState.module val moduleDescriptor = kaptContext.generationState.module
val evaluator = ConstantExpressionEvaluator(moduleDescriptor, LanguageVersionSettingsImpl.DEFAULT, kaptContext.project) val evaluator = ConstantExpressionEvaluator(moduleDescriptor, LanguageVersionSettingsImpl.DEFAULT, kaptContext.project)
val trace = DelegatingBindingTrace(kaptContext.bindingContext, "Kapt") val trace = DelegatingBindingTrace(kaptContext.bindingContext, "Kapt")
val const = evaluator.evaluateExpression(propertyInitializer, trace, propertyType) val const = evaluator.evaluateExpression(initializer, trace, propertyType)
if (const != null && !const.isError && const.canBeUsedInAnnotations && !const.usesNonConstValAsConstant) { if (const != null && !const.isError && const.canBeUsedInAnnotations && !const.usesNonConstValAsConstant) {
return convertConstantValueArguments(const.getValue(propertyType), listOf(propertyInitializer)) return convertConstantValueArguments(const.getValue(propertyType), listOf(initializer))
} }
} }
@@ -5,7 +5,7 @@ public final class State {
private final int someInt = 0; private final int someInt = 0;
private final long someLong = 0L; private final long someLong = 0L;
@org.jetbrains.annotations.NotNull() @org.jetbrains.annotations.NotNull()
private final java.lang.String someString = null; private final java.lang.String someString = "";
public final int getSomeInt() { public final int getSomeInt() {
return 0; return 0;
@@ -38,9 +38,9 @@ import java.lang.System;
@kotlin.Metadata() @kotlin.Metadata()
public final class State2 { public final class State2 {
public final int someInt = 0; public final int someInt = 0;
public final long someLong = 0L; public final long someLong = 2L;
@org.jetbrains.annotations.NotNull() @org.jetbrains.annotations.NotNull()
public final java.lang.String someString = null; public final java.lang.String someString = "";
public final int test(int someInt, long someLong, @org.jetbrains.annotations.NotNull() public final int test(int someInt, long someLong, @org.jetbrains.annotations.NotNull()
java.lang.String someString) { java.lang.String someString) {
+2 -1
View File
@@ -66,7 +66,8 @@ annotation class Anno(
val a8: Double, val a8: Double,
val a9: String) val a9: String)
class MyActivity { // TODO generate the right default field value for 'p0'
class MyActivity(val p0: Int = lib.R.id.textView, val p1: Int = 5) {
@Bind(LibR.id.textView) @Bind(LibR.id.textView)
@BindField(LibR.id.textView) @BindField(LibR.id.textView)
val a = 0 val a = 0
@@ -134,6 +134,8 @@ public final class MyActivity {
public final int propD = app.B.id.textView; public final int propD = app.B.id.textView;
public int propE = app.B.id.textView; public int propE = app.B.id.textView;
private final int propF = 0; private final int propF = 0;
private final int p0 = 0;
private final int p1 = 5;
@Bind(id = lib.R.id.textView) @Bind(id = lib.R.id.textView)
public static void a$annotations() { public static void a$annotations() {
@@ -224,6 +226,18 @@ public final class MyActivity {
return 0; return 0;
} }
public final int getP0() {
return 0;
}
public final int getP1() {
return 0;
}
public MyActivity(int p0, int p1) {
super();
}
public MyActivity() { public MyActivity() {
super(); super();
} }
@@ -21,4 +21,6 @@ object Foo {
object Boo { object Boo {
val z = foo() val z = foo()
fun foo() = "abc" fun foo() = "abc"
} }
class Zoo(val p: Int = 100)
@@ -116,3 +116,25 @@ public final class Foo {
super(); super();
} }
} }
////////////////////
import java.lang.System;
@kotlin.Metadata()
public final class Zoo {
private final int p = 100;
public final int getP() {
return 0;
}
public Zoo(int p) {
super();
}
public Zoo() {
super();
}
}
@@ -26,7 +26,7 @@ public final class State {
private final int someInt = 0; private final int someInt = 0;
private final long someLong = 0L; private final long someLong = 0L;
@org.jetbrains.annotations.NotNull() @org.jetbrains.annotations.NotNull()
private final java.lang.String someString = null; private final java.lang.String someString = "";
public final int getSomeInt() { public final int getSomeInt() {
return 0; return 0;