Load static final fields of appropriate types from Java as const
This commit is contained in:
+12
-2
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.types.JetType;
|
||||
import java.util.List;
|
||||
|
||||
public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements JavaCallableMemberDescriptor {
|
||||
private final boolean isStaticFinal;
|
||||
public JavaPropertyDescriptor(
|
||||
@NotNull DeclarationDescriptor containingDeclaration,
|
||||
@NotNull Annotations annotations,
|
||||
@@ -34,10 +35,13 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
|
||||
boolean isVar,
|
||||
@NotNull Name name,
|
||||
@NotNull SourceElement source,
|
||||
@Nullable PropertyDescriptor original
|
||||
@Nullable PropertyDescriptor original,
|
||||
boolean isStaticFinal
|
||||
) {
|
||||
super(containingDeclaration, original, annotations, Modality.FINAL, visibility, isVar, name, Kind.DECLARATION, source,
|
||||
/* lateInit = */ false, /* isConst = */ false);
|
||||
|
||||
this.isStaticFinal = isStaticFinal;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,7 +63,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
|
||||
isVar(),
|
||||
getName(),
|
||||
getSource(),
|
||||
getOriginal()
|
||||
getOriginal(),
|
||||
isStaticFinal
|
||||
);
|
||||
assert getGetter() == null : "Field must not have a getter: " + this;
|
||||
assert getSetter() == null : "Field must not have a setter: " + this;
|
||||
@@ -76,4 +81,9 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
|
||||
);
|
||||
return enhanced;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConst() {
|
||||
return isStaticFinal && ConstUtil.canBeUsedForConstVal(getType());
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -103,7 +103,8 @@ public class LazyJavaClassMemberScope(
|
||||
|
||||
val propertyDescriptor = JavaPropertyDescriptor(
|
||||
getContainingDeclaration(), annotations, method.getVisibility(),
|
||||
/* isVar = */ false, method.getName(), c.components.sourceElementFactory.source(method), /* original */ null
|
||||
/* isVar = */ false, method.getName(), c.components.sourceElementFactory.source(method), /* original */ null,
|
||||
/* isStaticFinal = */ false
|
||||
)
|
||||
|
||||
// default getter is necessary because there is no real field in annotation
|
||||
|
||||
+5
-3
@@ -269,13 +269,15 @@ public abstract class LazyJavaScope(
|
||||
val propertyName = field.getName()
|
||||
|
||||
return JavaPropertyDescriptor(containingDeclaration, annotations, visibility, isVar, propertyName,
|
||||
c.components.sourceElementFactory.source(field), /* original = */ null)
|
||||
c.components.sourceElementFactory.source(field), /* original = */ null, /*isConst= */ field.isFinalStatic)
|
||||
}
|
||||
|
||||
private val JavaField.isFinalStatic: Boolean
|
||||
get() = isFinal && isStatic
|
||||
|
||||
private fun getPropertyType(field: JavaField, annotations: Annotations): JetType {
|
||||
// Fields do not have their own generic parameters
|
||||
val finalStatic = field.isFinal() && field.isStatic()
|
||||
|
||||
val finalStatic = field.isFinalStatic
|
||||
// simple static constants should not have flexible types:
|
||||
val allowFlexible = PLATFORM_TYPES && !(finalStatic && c.components.javaPropertyInitializerEvaluator.isNotNullCompileTimeConstant(field))
|
||||
val propertyType = c.typeResolver.transformJavaType(
|
||||
|
||||
Reference in New Issue
Block a user