K2 UAST: use function symbol for accessors of Java synthetic property

...which are literally closer to the resolution result as PsiMethod
than KtProperty(G|S)etterSymbol, which seems to be subject to change
depending on the underlying frontend change.

^KT-66785

GitOrigin-RevId: cce265b6dad74db82dae1c45262a78270c5e3afe
This commit is contained in:
Jinseong Jeon
2024-03-26 11:11:33 +00:00
committed by intellij-monorepo-bot
parent 780106c7b0
commit cf666d61ff
4 changed files with 57 additions and 10 deletions
@@ -1311,7 +1311,7 @@ interface UastResolveApiFixtureTestBase : UastPluginSelection {
)
}
fun checkResolveSyntheticJavaPropertyAccessor(myFixture: JavaCodeInsightTestFixture) {
fun checkResolveSyntheticJavaPropertyAccessor_setter(myFixture: JavaCodeInsightTestFixture) {
myFixture.addClass(
"""public class X {
|String getFoo();
@@ -1340,6 +1340,44 @@ interface UastResolveApiFixtureTestBase : UastPluginSelection {
TestCase.assertEquals("setFoo", (resolvedPsiElements.single() as PsiMethod).name)
}
fun checkResolveSyntheticJavaPropertyAccessor_getter(myFixture: JavaCodeInsightTestFixture) {
myFixture.addClass(
"""public class X {
|String getFoo();
|void setFoo(String s);
|}""".trimMargin()
)
myFixture.addClass(
"""public interface I {
|String getFoo();
|}""".trimMargin()
)
myFixture.configureByText(
"main.kt", """
class Y : X(), I {
}
fun box(y : Y): Boolean {
val yo = y.foo ?: return false
return yo == "42"
}
""".trimIndent()
)
val visitor = PropertyAccessorVisitor { it.endsWith("foo") || it.endsWith("getFoo") }
myFixture.file.toUElement()!!.accept(visitor)
TestCase.assertEquals(2, visitor.resolvedElements.size)
val nodes = visitor.resolvedElements.keys
TestCase.assertTrue(nodes.any { it is USimpleNameReferenceExpression })
// Will create on-the-fly accessor call for Java synthetic property
TestCase.assertTrue(nodes.any { it is UCallExpression})
// Both simple name reference (`foo`) and its on-the-fly accessor call are resolved to the same Java synthetic property accessor.
val resolvedPsiElements = visitor.resolvedElements.values.toSet()
TestCase.assertEquals(1, resolvedPsiElements.size)
TestCase.assertEquals("getFoo", (resolvedPsiElements.single() as PsiMethod).name)
}
fun checkResolveKotlinPropertyAccessor(myFixture: JavaCodeInsightTestFixture) {
myFixture.configureByText(
"Foo.kt", """
@@ -268,11 +268,12 @@ interface FirKotlinUastResolveProviderService : BaseKotlinUastResolveProviderSer
val variableAccessCall = ktSimpleNameExpression.resolveCall()?.singleCallOrNull<KtSimpleVariableAccessCall>() ?: return null
val propertySymbol = variableAccessCall.symbol as? KtSyntheticJavaPropertySymbol ?: return null
when (variableAccessCall.simpleAccess) {
is KtSimpleVariableAccess.Read ->
toPsiMethod(propertySymbol.getter, ktSimpleNameExpression)
is KtSimpleVariableAccess.Write ->
toPsiMethod(propertySymbol.setter ?: return null, ktSimpleNameExpression)
is KtSimpleVariableAccess.Read -> {
toPsiMethod(propertySymbol.javaGetterSymbol, ktSimpleNameExpression)
}
is KtSimpleVariableAccess.Write -> {
toPsiMethod(propertySymbol.javaSetterSymbol?: return null, ktSimpleNameExpression)
}
}
}
}
@@ -196,8 +196,12 @@ class FirUastResolveApiFixtureTest : KotlinLightCodeInsightFixtureTestCase(), Ua
checkResolveSyntheticJavaPropertyCompoundAccess(myFixture)
}
fun testResolveSyntheticJavaPropertyAccessor() {
checkResolveSyntheticJavaPropertyAccessor(myFixture)
fun testResolveSyntheticJavaPropertyAccessor_setter() {
checkResolveSyntheticJavaPropertyAccessor_setter(myFixture)
}
fun testResolveSyntheticJavaPropertyAccessor_getter() {
checkResolveSyntheticJavaPropertyAccessor_getter(myFixture)
}
fun testResolveKotlinPropertyAccessor() {
@@ -151,8 +151,12 @@ class FE1UastResolveApiFixtureTest : KotlinLightCodeInsightFixtureTestCase(), Ua
checkResolveSyntheticJavaPropertyCompoundAccess(myFixture, isK2 = false)
}
fun testResolveSyntheticJavaPropertyAccessor() {
checkResolveSyntheticJavaPropertyAccessor(myFixture)
fun testResolveSyntheticJavaPropertyAccessor_setter() {
checkResolveSyntheticJavaPropertyAccessor_setter(myFixture)
}
fun testResolveSyntheticJavaPropertyAccessor_getter() {
checkResolveSyntheticJavaPropertyAccessor_getter(myFixture)
}
fun testResolveKotlinPropertyAccessor() {