aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTing-Yuan Huang <laszio@google.com>2024-03-29 13:59:25 -0700
committerKSP Auto Pick <kotlin-symbol-processing@google.com>2024-03-29 23:35:02 +0000
commit18efd3349edf51486a3d69cd99ea06a5e7e99ee1 (patch)
treec91bc39e4c8e8b5c385c8ff83ebde5fe9487a71d
parentba9cc87bb35e949e8e3afc0831a0a077ac7bf594 (diff)
downloadksp-18efd3349edf51486a3d69cd99ea06a5e7e99ee1.tar.gz
More checks on synthesized properties for Java
(cherry picked from commit ffb13a69890fa7bdf4a251416bde9c8e39cce548)
-rw-r--r--compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt30
1 files changed, 17 insertions, 13 deletions
diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt
index 001fafcb..1cc8f53b 100644
--- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt
+++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt
@@ -517,24 +517,28 @@ class ResolverImpl(
}
}
+ private val synthesizedPropPrefix = mapOf<String, (PropertyDescriptor?) -> PropertyAccessorDescriptor?>(
+ "set" to { it?.setter },
+ "get" to { it?.getter },
+ "is" to { it?.getter },
+ )
+
// TODO: Resolve Java variables is not supported by this function. Not needed currently.
fun resolveJavaDeclaration(psi: PsiElement): DeclarationDescriptor? {
return when (psi) {
is PsiClass -> moduleClassResolver.resolveClass(JavaClassImpl(psi))
is PsiMethod -> {
- // TODO: get rid of hardcoded check if possible.
- val property = if (psi.name.startsWith("set") || psi.name.startsWith("get")) {
- val propName = psi.name.substring(3).replaceFirstChar(Char::lowercaseChar)
- moduleClassResolver
- .resolveContainingClass(psi)
- ?.findEnclosedDescriptor(
- kindFilter = DescriptorKindFilter.CALLABLES,
- name = propName
- ) {
- (it as? PropertyDescriptor)?.getter?.findPsi() == psi ||
- (it as? PropertyDescriptor)?.setter?.findPsi() == psi
- }
- } else null
+ val property = synthesizedPropPrefix.keys.firstOrNull {
+ psi.name.startsWith(it) && psi.name.length > it.length && psi.name[it.length].isUpperCase()
+ }?.let { prefix ->
+ val propName = psi.name.substring(prefix.length).replaceFirstChar(Char::lowercaseChar)
+ moduleClassResolver.resolveContainingClass(psi)?.findEnclosedDescriptor(
+ kindFilter = DescriptorKindFilter.VARIABLES,
+ name = propName
+ ) {
+ synthesizedPropPrefix[prefix]!!(it as? PropertyDescriptor)?.findPsi() == psi
+ }
+ }
property ?: moduleClassResolver
.resolveContainingClass(psi)?.let { containingClass ->
val filter = if (psi is SyntheticElement) {