diff options
author | Ting-Yuan Huang <laszio@google.com> | 2024-03-29 13:59:25 -0700 |
---|---|---|
committer | laszio <ting-yuan@users.noreply.github.com> | 2024-03-29 15:38:18 -0700 |
commit | ffb13a69890fa7bdf4a251416bde9c8e39cce548 (patch) | |
tree | e15ce362b3f1187bd41411e51ace76736633fddf | |
parent | d8e11ba469ed4af4091c9ebe6e569af440b8be1a (diff) | |
download | ksp-ffb13a69890fa7bdf4a251416bde9c8e39cce548.tar.gz |
More checks on synthesized properties for Java
-rw-r--r-- | compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt | 30 |
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 fd82e626..d2f07a81 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) { |