Skip to content

Commit ffb13a6

Browse files
committed
More checks on synthesized properties for Java
1 parent d8e11ba commit ffb13a6

File tree

1 file changed

+17
-13
lines changed
  • compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl

1 file changed

+17
-13
lines changed

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt

+17-13
Original file line numberDiff line numberDiff line change
@@ -517,24 +517,28 @@ class ResolverImpl(
517517
}
518518
}
519519

520+
private val synthesizedPropPrefix = mapOf<String, (PropertyDescriptor?) -> PropertyAccessorDescriptor?>(
521+
"set" to { it?.setter },
522+
"get" to { it?.getter },
523+
"is" to { it?.getter },
524+
)
525+
520526
// TODO: Resolve Java variables is not supported by this function. Not needed currently.
521527
fun resolveJavaDeclaration(psi: PsiElement): DeclarationDescriptor? {
522528
return when (psi) {
523529
is PsiClass -> moduleClassResolver.resolveClass(JavaClassImpl(psi))
524530
is PsiMethod -> {
525-
// TODO: get rid of hardcoded check if possible.
526-
val property = if (psi.name.startsWith("set") || psi.name.startsWith("get")) {
527-
val propName = psi.name.substring(3).replaceFirstChar(Char::lowercaseChar)
528-
moduleClassResolver
529-
.resolveContainingClass(psi)
530-
?.findEnclosedDescriptor(
531-
kindFilter = DescriptorKindFilter.CALLABLES,
532-
name = propName
533-
) {
534-
(it as? PropertyDescriptor)?.getter?.findPsi() == psi ||
535-
(it as? PropertyDescriptor)?.setter?.findPsi() == psi
536-
}
537-
} else null
531+
val property = synthesizedPropPrefix.keys.firstOrNull {
532+
psi.name.startsWith(it) && psi.name.length > it.length && psi.name[it.length].isUpperCase()
533+
}?.let { prefix ->
534+
val propName = psi.name.substring(prefix.length).replaceFirstChar(Char::lowercaseChar)
535+
moduleClassResolver.resolveContainingClass(psi)?.findEnclosedDescriptor(
536+
kindFilter = DescriptorKindFilter.VARIABLES,
537+
name = propName
538+
) {
539+
synthesizedPropPrefix[prefix]!!(it as? PropertyDescriptor)?.findPsi() == psi
540+
}
541+
}
538542
property ?: moduleClassResolver
539543
.resolveContainingClass(psi)?.let { containingClass ->
540544
val filter = if (psi is SyntheticElement) {

0 commit comments

Comments
 (0)