Support @JvmField on primary ctr properties in ultra-light classes
This commit is contained in:
@@ -154,12 +154,7 @@ class KtUltraLightClass(classOrObject: KtClassOrObject, private val support: Ult
|
|||||||
|
|
||||||
|
|
||||||
for (parameter in propertyParameters()) {
|
for (parameter in propertyParameters()) {
|
||||||
val modifiers = hashSetOf<String>()
|
propertyField(parameter, ::generateUniqueName, forceStatic = false)?.let(result::add)
|
||||||
modifiers.add(PsiModifier.PRIVATE)
|
|
||||||
if (!parameter.isMutable) {
|
|
||||||
modifiers.add(PsiModifier.FINAL)
|
|
||||||
}
|
|
||||||
result.add(KtUltraLightField(parameter, generateUniqueName(parameter.name.orEmpty()), this, support, modifiers))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.classOrObject.companionObjects.firstOrNull()?.let { companion ->
|
this.classOrObject.companionObjects.firstOrNull()?.let { companion ->
|
||||||
@@ -220,32 +215,45 @@ class KtUltraLightClass(classOrObject: KtClassOrObject, private val support: Ult
|
|||||||
|
|
||||||
private fun isNamedObject() = classOrObject is KtObjectDeclaration && !classOrObject.isCompanion()
|
private fun isNamedObject() = classOrObject is KtObjectDeclaration && !classOrObject.isCompanion()
|
||||||
|
|
||||||
private fun propertyField(property: KtProperty, generateUniqueName: (String) -> String, forceStatic: Boolean): KtLightField? {
|
private fun propertyField(
|
||||||
if (!hasBackingField(property)) return null
|
// KtProperty | KtParameter
|
||||||
if (property.hasAnnotation(JVM_SYNTHETIC_ANNOTATION_FQ_NAME)) return null
|
variable: KtCallableDeclaration,
|
||||||
|
generateUniqueName: (String) -> String,
|
||||||
|
forceStatic: Boolean
|
||||||
|
): KtLightField? {
|
||||||
|
val property = variable as? KtProperty
|
||||||
|
if (property != null && !hasBackingField(property)) return null
|
||||||
|
|
||||||
val hasDelegate = property.hasDelegate()
|
if (variable.hasAnnotation(JVM_SYNTHETIC_ANNOTATION_FQ_NAME)) return null
|
||||||
val fieldName = generateUniqueName((property.name ?: "") + (if (hasDelegate) "\$delegate" else ""))
|
|
||||||
|
val hasDelegate = property?.hasDelegate() == true
|
||||||
|
val fieldName = generateUniqueName((variable.name ?: "") + (if (hasDelegate) "\$delegate" else ""))
|
||||||
|
|
||||||
val visibility = when {
|
val visibility = when {
|
||||||
property.hasModifier(PRIVATE_KEYWORD) -> PsiModifier.PRIVATE
|
variable.hasModifier(PRIVATE_KEYWORD) -> PsiModifier.PRIVATE
|
||||||
property.hasModifier(LATEINIT_KEYWORD) || property.isConstOrJvmField() -> {
|
variable.hasModifier(LATEINIT_KEYWORD) || variable.isConstOrJvmField() -> {
|
||||||
val declaration = property.setter ?: property
|
val declaration = property?.setter ?: variable
|
||||||
simpleVisibility(declaration)
|
simpleVisibility(declaration)
|
||||||
}
|
}
|
||||||
else -> PsiModifier.PRIVATE
|
else -> PsiModifier.PRIVATE
|
||||||
}
|
}
|
||||||
val modifiers = hashSetOf(visibility)
|
val modifiers = hashSetOf(visibility)
|
||||||
|
|
||||||
if (!property.isVar || property.hasModifier(CONST_KEYWORD) || hasDelegate) {
|
val isMutable = when (variable) {
|
||||||
|
is KtProperty -> variable.isVar
|
||||||
|
is KtParameter -> variable.isMutable
|
||||||
|
else -> error("Unexpected type of variable: ${variable::class.java}")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isMutable || variable.hasModifier(CONST_KEYWORD) || hasDelegate) {
|
||||||
modifiers.add(PsiModifier.FINAL)
|
modifiers.add(PsiModifier.FINAL)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forceStatic || isNamedObject() && isJvmStatic(property)) {
|
if (forceStatic || isNamedObject() && isJvmStatic(variable)) {
|
||||||
modifiers.add(PsiModifier.STATIC)
|
modifiers.add(PsiModifier.STATIC)
|
||||||
}
|
}
|
||||||
|
|
||||||
return KtUltraLightField(property, fieldName, this, support, modifiers)
|
return KtUltraLightField(variable, fieldName, this, support, modifiers)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasBackingField(property: KtProperty): Boolean {
|
private fun hasBackingField(property: KtProperty): Boolean {
|
||||||
|
|||||||
@@ -18,3 +18,10 @@ interface B {
|
|||||||
val a: Collection<*> = emptyList()
|
val a: Collection<*> = emptyList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class C(
|
||||||
|
@JvmField
|
||||||
|
val a: Collection<*> = emptyList(),
|
||||||
|
@JvmField
|
||||||
|
var b: Int = 1
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user