[AA] KtPsiBasedSymbolPointer: add class type check

^KT-54311
This commit is contained in:
Dmitrii Gridin
2022-11-10 14:25:53 +01:00
committed by Space Team
parent 4dc7b6dddc
commit fe93f49ff7
25 changed files with 177 additions and 27 deletions
@@ -15,15 +15,17 @@ import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtObjectLiteralExpression
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import kotlin.reflect.KClass
public class KtPsiBasedSymbolPointer<S : KtSymbol> private constructor(private val psiPointer: SmartPsiElementPointer<out KtElement>) :
KtSymbolPointer<S>() {
public class KtPsiBasedSymbolPointer<S : KtSymbol> private constructor(
private val psiPointer: SmartPsiElementPointer<out KtElement>,
private val expectedClass: KClass<S>,
) : KtSymbolPointer<S>() {
@Deprecated("Consider using org.jetbrains.kotlin.analysis.api.KtAnalysisSession.restoreSymbol")
override fun restoreSymbol(analysisSession: KtAnalysisSession): S? {
val psi = psiPointer.element ?: return null
@Suppress("UNCHECKED_CAST")
return with(analysisSession) {
val symbol: KtSymbol = with(analysisSession) {
when (psi) {
is KtDeclaration -> psi.getSymbol()
is KtFile -> psi.getFileSymbol()
@@ -31,11 +33,22 @@ public class KtPsiBasedSymbolPointer<S : KtSymbol> private constructor(private v
error("Unexpected declaration to restore: ${psi::class}, text:\n ${psi.text}")
}
}
} as S?
}
if (!expectedClass.isInstance(symbol)) return null
@Suppress("UNCHECKED_CAST")
return symbol as S
}
public constructor(psi: KtElement, expectedClass: KClass<S>) : this(psi.createSmartPointer(), expectedClass)
public companion object {
public fun <S : KtSymbol> createForSymbolFromSource(symbol: S): KtPsiBasedSymbolPointer<S>? {
public inline fun <reified S : KtSymbol> createForSymbolFromSource(symbol: S): KtPsiBasedSymbolPointer<S>? {
return createForSymbolFromSource(symbol, S::class)
}
public fun <S : KtSymbol> createForSymbolFromSource(symbol: S, expectedClass: KClass<S>): KtPsiBasedSymbolPointer<S>? {
ifDisabled { return null }
if (symbol.origin != KtSymbolOrigin.SOURCE) return null
@@ -47,13 +60,18 @@ public class KtPsiBasedSymbolPointer<S : KtSymbol> private constructor(private v
else -> return null
}
return KtPsiBasedSymbolPointer(psi.createSmartPointer())
return KtPsiBasedSymbolPointer(psi, expectedClass)
}
public fun <S : KtSymbol> createForSymbolFromPsi(ktElement: KtElement): KtPsiBasedSymbolPointer<S>? {
public fun <S : KtSymbol> createForSymbolFromPsi(ktElement: KtElement, expectedClass: KClass<S>): KtPsiBasedSymbolPointer<S>? {
ifDisabled { return null }
return KtPsiBasedSymbolPointer(ktElement.createSmartPointer())
return KtPsiBasedSymbolPointer(ktElement, expectedClass)
}
public inline fun <reified S : KtSymbol> createForSymbolFromPsi(ktElement: KtElement): KtPsiBasedSymbolPointer<S>? {
return createForSymbolFromPsi(ktElement, S::class)
}
@TestOnly
@@ -0,0 +1,53 @@
KtKotlinPropertySymbol:
annotationsList: []
callableIdIfNonLocal: /A.abc
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: false
hasStableParameterNames: true
isDefault: true
isExtension: false
isInline: false
isOverride: false
modality: FINAL
origin: SOURCE
receiverType: null
returnType: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getDispatchReceiver(): A
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
hasBackingField: true
hasGetter: true
hasSetter: false
initializer: null
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: true
modality: FINAL
name: abc
origin: SOURCE
receiverType: null
returnType: kotlin/Int
setter: null
symbolKind: CLASS_MEMBER
typeParameters: []
visibility: Public
getDispatchReceiver(): A
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getAbc
javaSetterName: null
setterDeprecationStatus: null
@@ -0,0 +1,7 @@
// DO_NOT_CHECK_SYMBOL_RESTORE
class A(val abc: Int) {
fun check() {
a<caret>bc
}
}
@@ -0,0 +1,53 @@
KtKotlinPropertySymbol:
annotationsList: []
callableIdIfNonLocal: /A.abc
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: false
hasStableParameterNames: true
isDefault: true
isExtension: false
isInline: false
isOverride: false
modality: FINAL
origin: SOURCE
receiverType: null
returnType: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getDispatchReceiver(): A
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
hasBackingField: true
hasGetter: true
hasSetter: false
initializer: KtNonConstantInitializerValue(val abc: Int)
isConst: false
isDelegatedProperty: false
isExtension: false
isFromPrimaryConstructor: true
isLateInit: false
isOverride: false
isStatic: false
isVal: true
modality: FINAL
name: abc
origin: SOURCE
receiverType: null
returnType: kotlin/Int
setter: null
symbolKind: CLASS_MEMBER
typeParameters: []
visibility: Public
getDispatchReceiver(): A
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getAbc
javaSetterName: null
setterDeprecationStatus: null