[ULC] Fixed UL backing field initializers

This commit is contained in:
Igor Yakovlev
2020-05-20 13:48:50 +03:00
parent 129979b0ff
commit c3810dc223
3 changed files with 23 additions and 16 deletions
@@ -973,13 +973,13 @@ class KotlinTypeMapper @JvmOverloads constructor(
fun writeFieldSignature(
backingFieldType: KotlinType,
propertyDescriptor: PropertyDescriptor,
variableDescriptor: VariableDescriptor,
sw: JvmSignatureWriter
) {
if (!propertyDescriptor.isVar) {
mapReturnType(propertyDescriptor, sw, backingFieldType)
if (!variableDescriptor.isVar) {
mapReturnType(variableDescriptor, sw, backingFieldType)
} else {
writeParameterType(sw, backingFieldType, propertyDescriptor)
writeParameterType(sw, backingFieldType, variableDescriptor)
}
}
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.asJava.elements.KtUltraLightSimpleModifierList
import org.jetbrains.kotlin.codegen.PropertyCodegen
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.lexer.KtTokens
@@ -35,7 +37,7 @@ private class KtUltraLightSimpleModifierListField(
private val support: KtUltraLightSupport,
private val declaration: KtNamedDeclaration,
owner: KtLightElement<KtModifierListOwner, PsiModifierListOwner>,
private val modifiers: Set<String>
private val modifiers: Set<String>,
) : KtUltraLightSimpleModifierList(owner, modifiers, support) {
override fun hasModifierProperty(name: String): Boolean = when (name) {
PsiModifier.VOLATILE -> hasFieldAnnotation(VOLATILE_ANNOTATION_FQ_NAME)
@@ -60,7 +62,7 @@ internal class KtUltraLightFieldForSourceDeclaration(
name: String,
containingClass: KtLightClass,
support: KtUltraLightSupport,
modifiers: Set<String>
modifiers: Set<String>,
) : KtUltraLightFieldImpl(declaration, name, containingClass, support, modifiers),
KtLightFieldForSourceDeclarationSupport
@@ -69,7 +71,7 @@ internal open class KtUltraLightFieldImpl protected constructor(
name: String,
private val containingClass: KtLightClass,
private val support: KtUltraLightSupport,
modifiers: Set<String>
modifiers: Set<String>,
) : LightFieldBuilder(name, PsiType.NULL, declaration), KtLightField,
KtUltraLightElementWithNullabilityAnnotation<KtDeclaration, PsiField> {
@@ -87,12 +89,14 @@ internal open class KtUltraLightFieldImpl protected constructor(
override fun getLanguage(): Language = KotlinLanguage.INSTANCE
private val propertyDescriptor: PropertyDescriptor? get() = declaration.resolve() as? PropertyDescriptor
private val variableDescriptor: VariableDescriptor?
get() = declaration.resolve()
?.let { it as? PropertyDescriptor ?: it as? ValueParameterDescriptor }
private val kotlinType: KotlinType?
get() = when {
declaration is KtProperty && declaration.hasDelegate() ->
propertyDescriptor?.let {
(variableDescriptor as? PropertyDescriptor)?.let {
val context = LightClassGenerationSupport.getInstance(project).analyze(declaration)
PropertyCodegen.getDelegateTypeForProperty(it, context)
}
@@ -127,7 +131,7 @@ internal open class KtUltraLightFieldImpl protected constructor(
?: nonExistent()
else -> {
val kotlinType = declaration.getKotlinType() ?: return@lazyPub PsiType.NULL
val descriptor = propertyDescriptor ?: return@lazyPub PsiType.NULL
val descriptor = variableDescriptor ?: return@lazyPub PsiType.NULL
support.mapType(this) { typeMapper, sw ->
typeMapper.writeFieldSignature(kotlinType, descriptor, sw)
@@ -155,7 +159,7 @@ internal open class KtUltraLightFieldImpl protected constructor(
if (!declaration.hasInitializer()) return@lazyPub null
if (!hasModifierProperty(PsiModifier.FINAL)) return@lazyPub null
if (!TypeConversionUtil.isPrimitiveAndNotNull(_type) && !_type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) return@lazyPub null
propertyDescriptor?.compileTimeInitializer
variableDescriptor?.compileTimeInitializer
}
override fun computeConstantValue(): Any? = _constantInitializer?.value
@@ -58,11 +58,12 @@ internal class UltraLightMembersCreator(
usedPropertyNames: HashSet<String>,
forceStatic: Boolean
): KtLightField? {
val property = variable as? KtProperty
if (property != null && !hasBackingField(property)) return null
if (!hasBackingField(variable)) return null
if (variable.hasAnnotation(JVM_SYNTHETIC_ANNOTATION_FQ_NAME) || variable.hasExpectModifier()) return null
val property = variable as? KtProperty
val hasDelegate = property?.hasDelegate() == true
val fieldName = generateUniqueFieldName((variable.name ?: "") + (if (hasDelegate) "\$delegate" else ""), usedPropertyNames)
@@ -93,9 +94,11 @@ internal class UltraLightMembersCreator(
return KtUltraLightFieldForSourceDeclaration(variable, fieldName, containingClass, support, modifiers)
}
private fun hasBackingField(property: KtProperty): Boolean {
private fun hasBackingField(property: KtCallableDeclaration): Boolean {
if (property.hasModifier(ABSTRACT_KEYWORD)) return false
if (property.hasModifier(LATEINIT_KEYWORD) || property.accessors.isEmpty()) return true
if (property.hasModifier(LATEINIT_KEYWORD)) return true
if (property is KtParameter) return true
if ((property as? KtProperty)?.accessors?.isEmpty() == true) return true
val context = LightClassGenerationSupport.getInstance(containingClass.project).analyze(property)
val descriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property)
@@ -237,7 +240,7 @@ internal class UltraLightMembersCreator(
) return PsiType.VOID
val desc =
ktDeclaration.resolve()?.getterIfProperty() as? FunctionDescriptor
ktDeclaration.resolve()?.getterIfProperty() as? CallableDescriptor
?: return PsiType.NULL
return support.mapType(wrapper) { typeMapper, signatureWriter ->