Kapt: Support default constant values for constructor parameters
This commit is contained in:
+10
-6
@@ -651,24 +651,28 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
|
||||
val value = field.value
|
||||
|
||||
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 (propertyInitializer != null) {
|
||||
return convertConstantValueArguments(value, listOf(propertyInitializer))
|
||||
if (initializer != null) {
|
||||
return convertConstantValueArguments(value, listOf(initializer))
|
||||
}
|
||||
|
||||
return convertValueOfPrimitiveTypeOrString(value)
|
||||
}
|
||||
|
||||
val propertyType = (origin?.descriptor as? PropertyDescriptor)?.returnType
|
||||
if (propertyInitializer != null && propertyType != null) {
|
||||
if (initializer != null && propertyType != null) {
|
||||
val moduleDescriptor = kaptContext.generationState.module
|
||||
val evaluator = ConstantExpressionEvaluator(moduleDescriptor, LanguageVersionSettingsImpl.DEFAULT, kaptContext.project)
|
||||
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) {
|
||||
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 long someLong = 0L;
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
private final java.lang.String someString = null;
|
||||
private final java.lang.String someString = "";
|
||||
|
||||
public final int getSomeInt() {
|
||||
return 0;
|
||||
@@ -38,9 +38,9 @@ import java.lang.System;
|
||||
@kotlin.Metadata()
|
||||
public final class State2 {
|
||||
public final int someInt = 0;
|
||||
public final long someLong = 0L;
|
||||
public final long someLong = 2L;
|
||||
@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()
|
||||
java.lang.String someString) {
|
||||
|
||||
@@ -66,7 +66,8 @@ annotation class Anno(
|
||||
val a8: Double,
|
||||
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)
|
||||
@BindField(LibR.id.textView)
|
||||
val a = 0
|
||||
|
||||
@@ -134,6 +134,8 @@ public final class MyActivity {
|
||||
public final int propD = app.B.id.textView;
|
||||
public int propE = app.B.id.textView;
|
||||
private final int propF = 0;
|
||||
private final int p0 = 0;
|
||||
private final int p1 = 5;
|
||||
|
||||
@Bind(id = lib.R.id.textView)
|
||||
public static void a$annotations() {
|
||||
@@ -224,6 +226,18 @@ public final class MyActivity {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final int getP0() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public final int getP1() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public MyActivity(int p0, int p1) {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyActivity() {
|
||||
super();
|
||||
}
|
||||
|
||||
+3
-1
@@ -21,4 +21,6 @@ object Foo {
|
||||
object Boo {
|
||||
val z = foo()
|
||||
fun foo() = "abc"
|
||||
}
|
||||
}
|
||||
|
||||
class Zoo(val p: Int = 100)
|
||||
+22
@@ -116,3 +116,25 @@ public final class Foo {
|
||||
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 long someLong = 0L;
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
private final java.lang.String someString = null;
|
||||
private final java.lang.String someString = "";
|
||||
|
||||
public final int getSomeInt() {
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user