Skip to content

Commit 2360958

Browse files
yigitting-yuan
authored andcommitted
Use property descriptor impl for java properties
When a java class implements a kotlin interface that declared a property, the descriptor could resolve to a PsiMethod for which we don't have a property implementation (because there is another matching method so single method is not enough). For those cases, fall back to the descriptor implementation. Test: allFunctions.kt Fixes: #216
1 parent 8f043c2 commit 2360958

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processor/AllFunctionsProcessor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AllFunctionsProcessor : AbstractTestProcessor() {
4040
return "${this.simpleName.asString()}" +
4141
"(${this.parameters.map {
4242
buildString {
43-
append(it.type?.resolve()?.declaration?.qualifiedName?.asString())
43+
append(it.type.resolve().declaration.qualifiedName?.asString())
4444
if (it.hasDefault) {
4545
append("(hasDefault)")
4646
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/utils.kt

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ internal fun PropertyDescriptor.toKSPropertyDeclaration(): KSPropertyDeclaration
275275
is KtProperty -> KSPropertyDeclarationImpl.getCached(psi)
276276
is KtParameter -> KSPropertyDeclarationParameterImpl.getCached(psi)
277277
is PsiField -> KSPropertyDeclarationJavaImpl.getCached(psi)
278+
is PsiMethod -> {
279+
// happens when a java class implements a kotlin interface that declares properties.
280+
KSPropertyDeclarationDescriptorImpl.getCached(this)
281+
}
278282
else -> throw IllegalStateException("unexpected psi: ${psi.javaClass}")
279283
}
280284
}

compiler-plugin/testData/api/allFunctions.kt

+26-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
// WITH_RUNTIME
1919
// TEST PROCESSOR: AllFunctionsProcessor
2020
// EXPECTED:
21-
// class: Foo
21+
// class: KotlinInterfaceWithProperty
2222
// <init>(): C
23+
// <init>(): JavaImplOfKotlinInterface
2324
// a
2425
// aFromC
2526
// aFromC
@@ -30,18 +31,24 @@
3031
// cFromC
3132
// class: C
3233
// class: Data
34+
// class: Foo
35+
// class: JavaImplOfKotlinInterface
3336
// component1(): kotlin.String
3437
// contains(kotlin.Number): kotlin.Boolean
3538
// containsAll(kotlin.collections.Collection): kotlin.Boolean
3639
// copy(kotlin.String(hasDefault)): Data
3740
// equals(kotlin.Any): kotlin.Boolean
3841
// equals(kotlin.Any): kotlin.Boolean
3942
// equals(kotlin.Any): kotlin.Boolean
43+
// equals(kotlin.Any): kotlin.Boolean
44+
// equals(kotlin.Any): kotlin.Boolean
4045
// forEach(java.util.function.Consumer): kotlin.Unit
4146
// get(kotlin.Int): kotlin.Number
4247
// hashCode(): kotlin.Int
4348
// hashCode(): kotlin.Int
4449
// hashCode(): kotlin.Int
50+
// hashCode(): kotlin.Int
51+
// hashCode(): kotlin.Int
4552
// indexOf(kotlin.Number): kotlin.Int
4653
// isEmpty(): kotlin.Boolean
4754
// iterator(): kotlin.collections.Iterator
@@ -61,6 +68,10 @@
6168
// toString(): kotlin.String
6269
// toString(): kotlin.String
6370
// toString(): kotlin.String
71+
// toString(): kotlin.String
72+
// toString(): kotlin.String
73+
// x
74+
// x
6475
// END
6576
// FILE: a.kt
6677
abstract class Foo : C(), List<out Number> {
@@ -102,3 +113,17 @@ class C {
102113
return "str"
103114
}
104115
}
116+
117+
// FILE: KotlinInterfaceWithProperty.kt
118+
interface KotlinInterfaceWithProperty {
119+
var x:Int
120+
}
121+
122+
// FILE: JavaImplOfKotlinInterface.java
123+
class JavaImplOfKotlinInterface implements KotlinInterfaceWithProperty {
124+
public int getX() {
125+
return 1;
126+
}
127+
public void setX(int value) {
128+
}
129+
}

0 commit comments

Comments
 (0)