[AA] KtPsiBasedSymbolPointer: add class type check
^KT-54311
This commit is contained in:
committed by
Space Team
parent
4dc7b6dddc
commit
fe93f49ff7
+2
-2
@@ -47,9 +47,9 @@ internal class KtFe10DescAnonymousFunctionSymbol(
|
||||
get() = withValidityAssertion { descriptor.createContextReceivers(analysisContext) }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtAnonymousFunctionSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtAnonymousFunctionSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
override fun hashCode(): Int = calculateHashCode()
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ internal class KtFe10DescAnonymousObjectSymbol(
|
||||
get() = withValidityAssertion { descriptor.typeConstructor.supertypes.map { it.toKtType(analysisContext) } }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtAnonymousObjectSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtAnonymousObjectSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ internal class KtFe10DescConstructorSymbol(
|
||||
}
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtConstructorSymbol> = withValidityAssertion {
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource(this)
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource<KtConstructorSymbol>(this)
|
||||
if (pointerByPsi != null) {
|
||||
return pointerByPsi
|
||||
}
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ internal class KtFe10DescEnumEntrySymbol(
|
||||
get() = withValidityAssertion { descriptor.name }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtEnumEntrySymbol> = withValidityAssertion {
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource(this)
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource<KtEnumEntrySymbol>(this)
|
||||
if (pointerByPsi != null) {
|
||||
return pointerByPsi
|
||||
}
|
||||
|
||||
+1
-1
@@ -88,7 +88,7 @@ internal class KtFe10DescFunctionSymbol private constructor(
|
||||
get() = withValidityAssertion { descriptor.typeParameters.map { KtFe10DescTypeParameterSymbol(it, analysisContext) } }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtFunctionSymbol> = withValidityAssertion {
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource(this)
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource<KtFunctionSymbol>(this)
|
||||
if (pointerByPsi != null) {
|
||||
return pointerByPsi
|
||||
}
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ internal class KtFe10DescJavaFieldSymbol(
|
||||
get() = withValidityAssertion { descriptor.returnType.toKtType(analysisContext) }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtJavaFieldSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtJavaFieldSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -111,7 +111,7 @@ internal class KtFe10DescKotlinPropertySymbol(
|
||||
get() = withValidityAssertion { descriptor.isDelegated }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtKotlinPropertySymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtKotlinPropertySymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ internal class KtFe10DescLocalVariableSymbol(
|
||||
get() = withValidityAssertion { KtSymbolKind.LOCAL }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtLocalVariableSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtLocalVariableSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ internal class KtFe10DescNamedClassOrObjectSymbol(
|
||||
get() = withValidityAssertion { descriptor.declaredTypeParameters.map { KtFe10DescTypeParameterSymbol(it, analysisContext) } }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtNamedClassOrObjectSymbol> = withValidityAssertion {
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource(this)
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource<KtNamedClassOrObjectSymbol>(this)
|
||||
if (pointerByPsi != null) {
|
||||
return pointerByPsi
|
||||
}
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ internal class KtFe10DescPropertyGetterSymbol(
|
||||
get() = withValidityAssertion { descriptor.extensionReceiverParameter?.type?.toKtType(analysisContext) }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtPropertyGetterSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtPropertyGetterSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ internal class KtFe10DescPropertySetterSymbol(
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtPropertySetterSymbol> {
|
||||
withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtPropertySetterSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ internal class KtFe10DescSamConstructorSymbol(
|
||||
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtSamConstructorSymbol> = withValidityAssertion {
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource(this)
|
||||
val pointerByPsi = KtPsiBasedSymbolPointer.createForSymbolFromSource<KtSamConstructorSymbol>(this)
|
||||
if (pointerByPsi != null) {
|
||||
return pointerByPsi
|
||||
}
|
||||
|
||||
+1
-1
@@ -91,7 +91,7 @@ internal class KtFe10DescSyntheticJavaPropertySymbol(
|
||||
get() = withValidityAssertion { descriptor.typeParameters.map { it.toKtTypeParameter(analysisContext) } }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtSyntheticJavaPropertySymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtSyntheticJavaPropertySymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -90,7 +90,7 @@ internal class KtFe10DescSyntheticJavaPropertySymbolForOverride(
|
||||
get() = withValidityAssertion { descriptor.typeParameters.map { it.toKtTypeParameter(analysisContext) } }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtSyntheticJavaPropertySymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtSyntheticJavaPropertySymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ internal class KtFe10DescTypeAliasSymbol(
|
||||
get() = withValidityAssertion { descriptor.classId }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtTypeAliasSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtTypeAliasSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ internal class KtFe10DescTypeParameterSymbol(
|
||||
get() = withValidityAssertion { descriptor.isReified }
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtTypeParameterSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtTypeParameterSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ internal class KtFe10DescValueParameterSymbol(
|
||||
}
|
||||
|
||||
override fun createPointer(): KtSymbolPointer<KtValueParameterSymbol> = withValidityAssertion {
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
return KtPsiBasedSymbolPointer.createForSymbolFromSource<KtValueParameterSymbol>(this) ?: KtFe10NeverRestoringSymbolPointer()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean = isEqualTo(other)
|
||||
|
||||
+6
@@ -106,6 +106,12 @@ public class Fe10IdeNormalAnalysisSourceModuleSymbolByReferenceTestGenerated ext
|
||||
runTest("analysis/analysis-api/testData/symbols/symbolByReference/primaryConstructorValueParameter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("primaryConstructorValueParameterAsProperty.kt")
|
||||
public void testPrimaryConstructorValueParameterAsProperty() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/symbols/symbolByReference/primaryConstructorValueParameterAsProperty.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyReceiverParameter.kt")
|
||||
public void testPropertyReceiverParameter() throws Exception {
|
||||
|
||||
+6
@@ -106,6 +106,12 @@ public class FirIdeNormalAnalysisSourceModuleSymbolByReferenceTestGenerated exte
|
||||
runTest("analysis/analysis-api/testData/symbols/symbolByReference/primaryConstructorValueParameter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("primaryConstructorValueParameterAsProperty.kt")
|
||||
public void testPrimaryConstructorValueParameterAsProperty() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/symbols/symbolByReference/primaryConstructorValueParameterAsProperty.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyReceiverParameter.kt")
|
||||
public void testPropertyReceiverParameter() throws Exception {
|
||||
|
||||
+6
@@ -106,6 +106,12 @@ public class FirStandaloneNormalAnalysisSourceModuleSymbolByReferenceTestGenerat
|
||||
runTest("analysis/analysis-api/testData/symbols/symbolByReference/primaryConstructorValueParameter.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("primaryConstructorValueParameterAsProperty.kt")
|
||||
public void testPrimaryConstructorValueParameterAsProperty() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/symbols/symbolByReference/primaryConstructorValueParameterAsProperty.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyReceiverParameter.kt")
|
||||
public void testPropertyReceiverParameter() throws Exception {
|
||||
|
||||
+27
-9
@@ -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
|
||||
|
||||
+53
@@ -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
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// DO_NOT_CHECK_SYMBOL_RESTORE
|
||||
|
||||
class A(val abc: Int) {
|
||||
fun check() {
|
||||
a<caret>bc
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
val abc: kotlin.Int
|
||||
+53
@@ -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
|
||||
Reference in New Issue
Block a user