Refactor ConstantValue implementations
Remove KotlinBuiltIns and take a ModuleDescriptor instance in getType instead. This will allow to create constant values in contexts where there's no module or built-ins accessible (such as, deserialization of module annotations during indexing of .kotlin_module files in IDE). Note that some values (KClassValue, EnumValue, AnnotationValue) still take module-dependent objects (KotlinType, ClassDescriptor and AnnotationDescriptor respectively). This is to be refactored later
This commit is contained in:
+2
-4
@@ -20,21 +20,19 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.load.java.structure.JavaField
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValueFactory
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
|
||||
class JavaPropertyInitializerEvaluatorImpl : JavaPropertyInitializerEvaluator {
|
||||
override fun getInitializerConstant(field: JavaField, descriptor: PropertyDescriptor): ConstantValue<*>? {
|
||||
val evaluated = field.initializerValue ?: return null
|
||||
|
||||
val factory = ConstantValueFactory(descriptor.builtIns)
|
||||
return when (evaluated) {
|
||||
//Note: evaluated expression may be of class that does not match field type in some cases
|
||||
// tested for Int, left other checks just in case
|
||||
is Byte, is Short, is Int, is Long -> {
|
||||
factory.createIntegerConstantValue((evaluated as Number).toLong(), descriptor.type)
|
||||
ConstantValueFactory.createIntegerConstantValue((evaluated as Number).toLong(), descriptor.type)
|
||||
}
|
||||
else -> {
|
||||
factory.createConstantValue(evaluated)
|
||||
ConstantValueFactory.createConstantValue(evaluated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user