[AA] support @JvmField in KtSymbolInfoProvider

^KT-54311
^KT-54360 Fixed
This commit is contained in:
Dmitrii Gridin
2022-10-07 17:01:34 +02:00
committed by Space Team
parent f4ae696aee
commit 01d5a5bc5b
13 changed files with 284 additions and 21 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.resolve.deprecation.DeprecationInfo
import org.jetbrains.kotlin.resolve.deprecation.DeprecationLevelValue
import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
import org.jetbrains.kotlin.resolve.deprecation.SimpleDeprecationInfo
import org.jetbrains.kotlin.resolve.jvm.annotations.hasJvmFieldAnnotation
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
@@ -101,6 +102,8 @@ internal class KtFe10SymbolInfoProvider(
}
if (descriptor != null) {
if (descriptor.hasJvmFieldAnnotation()) return descriptor.name
val getter = descriptor.getter ?: return SpecialNames.NO_NAME_PROVIDED
return Name.identifier(DescriptorUtils.getJvmName(getter) ?: JvmAbi.getterName(descriptor.name.asString()))
}
@@ -120,6 +123,8 @@ internal class KtFe10SymbolInfoProvider(
return null
}
if (descriptor.hasJvmFieldAnnotation()) return descriptor.name
val setter = descriptor.setter ?: return SpecialNames.NO_NAME_PROVIDED
return Name.identifier(DescriptorUtils.getJvmName(setter) ?: JvmAbi.setterName(descriptor.name.asString()))
}
@@ -184,6 +184,12 @@ public class Fe10IdeNormalAnalysisSourceModuleSymbolByPsiTestGenerated extends A
runTest("analysis/analysis-api/testData/symbols/symbolByPsi/implicitReturnInLambda.kt");
}
@Test
@TestMetadata("jvmField.kt")
public void testJvmField() throws Exception {
runTest("analysis/analysis-api/testData/symbols/symbolByPsi/jvmField.kt");
}
@Test
@TestMetadata("jvmName.kt")
public void testJvmName() throws Exception {
@@ -18,10 +18,12 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.getDeprecationForCallSite
import org.jetbrains.kotlin.fir.declarations.getJvmNameFromAnnotation
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.resolve.deprecation.DeprecationInfo
internal class KtFirSymbolInfoProvider(
@@ -70,11 +72,9 @@ internal class KtFirSymbolInfoProvider(
if (symbol is KtFirSyntheticJavaPropertySymbol) {
return symbol.javaGetterSymbol.name
}
val jvmName = run {
val firProperty = symbol.firSymbol.fir as? FirProperty ?: return@run null
firProperty.getJvmNameFromAnnotation(AnnotationUseSiteTarget.PROPERTY_GETTER) ?: firProperty.getter?.getJvmNameFromAnnotation()
}
return Name.identifier(jvmName ?: JvmAbi.getterName(symbol.name.identifier))
val firProperty = symbol.firSymbol.fir as? FirProperty ?: return symbol.name
return getJvmName(firProperty, isSetter = false)
}
override fun getJavaSetterName(symbol: KtPropertySymbol): Name? {
@@ -82,14 +82,32 @@ internal class KtFirSymbolInfoProvider(
if (symbol is KtFirSyntheticJavaPropertySymbol) {
return symbol.javaSetterSymbol?.name
}
return if (symbol.isVal) null
else {
val jvmName = run {
val firProperty = symbol.firSymbol.fir as? FirProperty ?: return@run null
firProperty.getJvmNameFromAnnotation(AnnotationUseSiteTarget.PROPERTY_GETTER)
?: firProperty.setter?.getJvmNameFromAnnotation()
}
Name.identifier(jvmName ?: JvmAbi.setterName(symbol.name.identifier))
val firProperty = symbol.firSymbol.fir as? FirProperty ?: return symbol.name
if (firProperty.isVal) return null
return getJvmName(firProperty, isSetter = true)
}
private fun getJvmName(property: FirProperty, isSetter: Boolean): Name {
if (property.hasAnnotation(StandardClassIds.Annotations.JvmField)) return property.name
return Name.identifier(getJvmNameAsString(property, isSetter))
}
private fun getJvmNameAsString(property: FirProperty, isSetter: Boolean): String {
val useSiteTarget = if (isSetter) AnnotationUseSiteTarget.PROPERTY_SETTER else AnnotationUseSiteTarget.PROPERTY_GETTER
val jvmNameFromProperty = property.getJvmNameFromAnnotation(useSiteTarget)
if (jvmNameFromProperty != null) {
return jvmNameFromProperty
}
val accessor = if (isSetter) property.setter else property.getter
val jvmNameFromAccessor = accessor?.getJvmNameFromAnnotation()
if (jvmNameFromAccessor != null) {
return jvmNameFromAccessor
}
val identifier = property.name.identifier
return if (isSetter) JvmAbi.setterName(identifier) else JvmAbi.getterName(identifier)
}
}
@@ -184,6 +184,12 @@ public class FirIdeNormalAnalysisSourceModuleSymbolByPsiTestGenerated extends Ab
runTest("analysis/analysis-api/testData/symbols/symbolByPsi/implicitReturnInLambda.kt");
}
@Test
@TestMetadata("jvmField.kt")
public void testJvmField() throws Exception {
runTest("analysis/analysis-api/testData/symbols/symbolByPsi/jvmField.kt");
}
@Test
@TestMetadata("jvmName.kt")
public void testJvmName() throws Exception {
@@ -184,6 +184,12 @@ public class FirStandaloneNormalAnalysisSourceModuleSymbolByPsiTestGenerated ext
runTest("analysis/analysis-api/testData/symbols/symbolByPsi/implicitReturnInLambda.kt");
}
@Test
@TestMetadata("jvmField.kt")
public void testJvmField() throws Exception {
runTest("analysis/analysis-api/testData/symbols/symbolByPsi/jvmField.kt");
}
@Test
@TestMetadata("jvmName.kt")
public void testJvmName() throws Exception {
@@ -6,7 +6,7 @@ KtFileSymbol:
deprecationStatus: null
CALLABLE NAMES:
[function, functionWithDefault, testVal, initializedVariable, unitializedVariable, lateinitVariable, variableWithBackingField, privateSetter, customGetter, jvmNameOnGetter, propertyWithReceiver, propertyWithGenericReceiver, constant, jvmField]
[function, functionWithDefault, testVal, initializedVariable, unitializedVariable, lateinitVariable, variableWithBackingField, privateSetter, jvmNameOnSetter, customGetter, jvmNameOnGetter, propertyWithReceiver, propertyWithGenericReceiver, constant, jvmField]
CALLABLE SYMBOLS:
KtFunctionSymbol:
@@ -261,7 +261,40 @@ KtKotlinPropertySymbol:
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getPrivateSetter
javaSetterName: setPrivateSetter
javaSetterName: customPrivateSetter
setterDeprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
callableIdIfNonLocal: /jvmNameOnSetter
contextReceivers: []
getter: KtPropertyGetterSymbol(<getter>)
hasBackingField: true
hasGetter: true
hasSetter: true
initializer: null
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: false
modality: FINAL
name: jvmNameOnSetter
origin: SOURCE
receiverType: null
returnType: kotlin/String
setter: KtPropertySetterSymbol(<setter>)
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getJvmNameOnSetter
javaSetterName: customPrivateSetter
setterDeprecationStatus: null
KtKotlinPropertySymbol:
@@ -460,8 +493,8 @@ KtKotlinPropertySymbol:
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getJvmField
javaSetterName: setJvmField
javaGetterName: jvmField
javaSetterName: jvmField
setterDeprecationStatus: null
CLASSIFIER NAMES:
@@ -13,9 +13,14 @@ lateinit var lateinitVariable: String
var variableWithBackingField: Long = 4
get() = field
@set:JvmName("customPrivateSetter")
var privateSetter = ""
private set
var jvmNameOnSetter = ""
@JvmName("customPrivateSetter")
private set
@get:JvmName("myCustomGetter")
val customGetter: Int get() = 2
@@ -6,7 +6,7 @@ KtFileSymbol:
deprecationStatus: null
CALLABLE NAMES:
[function, functionWithDefault, testVal, initializedVariable, unitializedVariable, lateinitVariable, variableWithBackingField, privateSetter, customGetter, jvmNameOnGetter, propertyWithReceiver, propertyWithGenericReceiver, constant, jvmField]
[function, functionWithDefault, testVal, initializedVariable, unitializedVariable, lateinitVariable, variableWithBackingField, privateSetter, jvmNameOnSetter, customGetter, jvmNameOnGetter, propertyWithReceiver, propertyWithGenericReceiver, constant, jvmField]
CALLABLE SYMBOLS:
KtFunctionSymbol:
@@ -261,7 +261,40 @@ KtKotlinPropertySymbol:
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getPrivateSetter
javaSetterName: setPrivateSetter
javaSetterName: customPrivateSetter
setterDeprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
callableIdIfNonLocal: /jvmNameOnSetter
contextReceivers: []
getter: KtPropertyGetterSymbol(<getter>)
hasBackingField: true
hasGetter: true
hasSetter: true
initializer: KtConstantInitializerValue("")
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: false
modality: FINAL
name: jvmNameOnSetter
origin: SOURCE
receiverType: null
returnType: kotlin/String
setter: KtPropertySetterSymbol(<setter>)
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getJvmNameOnSetter
javaSetterName: customPrivateSetter
setterDeprecationStatus: null
KtKotlinPropertySymbol:
@@ -463,8 +496,8 @@ KtKotlinPropertySymbol:
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getJvmField
javaSetterName: setJvmField
javaGetterName: jvmField
javaSetterName: jvmField
setterDeprecationStatus: null
CLASSIFIER NAMES:
@@ -0,0 +1,3 @@
var jvmField: kotlin.Long
var jvmFieldOnField: kotlin.Int
@@ -0,0 +1,65 @@
KtKotlinPropertySymbol:
annotationsList: []
callableIdIfNonLocal: /jvmField
contextReceivers: []
getter: KtPropertyGetterSymbol(<getter>)
hasBackingField: true
hasGetter: true
hasSetter: true
initializer: KtConstantInitializerValue(2)
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: false
modality: FINAL
name: jvmField
origin: SOURCE
receiverType: null
returnType: kotlin/Long
setter: KtPropertySetterSymbol(<setter>)
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: jvmField
javaSetterName: jvmField
setterDeprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
callableIdIfNonLocal: /jvmFieldOnField
contextReceivers: []
getter: KtPropertyGetterSymbol(<getter>)
hasBackingField: true
hasGetter: true
hasSetter: true
initializer: KtConstantInitializerValue(4)
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: false
modality: FINAL
name: jvmFieldOnField
origin: SOURCE
receiverType: null
returnType: kotlin/Int
setter: KtPropertySetterSymbol(<setter>)
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: jvmFieldOnField
javaSetterName: jvmFieldOnField
setterDeprecationStatus: null
@@ -0,0 +1,7 @@
// WITH_STDLIB
@JvmField
var jvmField: Long = 2
@field:JvmField
var jvmFieldOnField: Int = 4
@@ -0,0 +1,5 @@
@kotlin.jvm.JvmField
var jvmField: kotlin.Long
@kotlin.jvm.JvmField
var jvmFieldOnField: kotlin.Int
@@ -0,0 +1,71 @@
KtKotlinPropertySymbol:
annotationsList: [
kotlin/jvm/JvmField()
psi: KtAnnotationEntry
]
callableIdIfNonLocal: /jvmField
contextReceivers: []
getter: KtPropertyGetterSymbol(<getter>)
hasBackingField: true
hasGetter: true
hasSetter: true
initializer: KtConstantInitializerValue(2)
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: false
modality: FINAL
name: jvmField
origin: SOURCE
receiverType: null
returnType: kotlin/Long
setter: KtPropertySetterSymbol(<setter>)
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: jvmField
javaSetterName: jvmField
setterDeprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: [
kotlin/jvm/JvmField()
psi: KtAnnotationEntry
]
callableIdIfNonLocal: /jvmFieldOnField
contextReceivers: []
getter: KtPropertyGetterSymbol(<getter>)
hasBackingField: true
hasGetter: true
hasSetter: true
initializer: KtConstantInitializerValue(4)
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: false
modality: FINAL
name: jvmFieldOnField
origin: SOURCE
receiverType: null
returnType: kotlin/Int
setter: KtPropertySetterSymbol(<setter>)
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: jvmFieldOnField
javaSetterName: jvmFieldOnField
setterDeprecationStatus: null