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:
Alexander Udalov
2017-12-29 13:04:36 +01:00
parent 5e97517c8b
commit 907f53e539
22 changed files with 207 additions and 247 deletions
@@ -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)
}
}
}