Skip to content

Commit

Permalink
remove findAnnotationFromUseSiteTarget public function, integrate rel…
Browse files Browse the repository at this point in the history
…ated logic into KSPropertyAccessor and KSPropertyDeclaration
  • Loading branch information
neetopia committed Mar 9, 2021
1 parent 448910d commit cd9ad2d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
8 changes: 0 additions & 8 deletions api/src/main/kotlin/com/google/devtools/ksp/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,6 @@ fun KSDeclaration.closestClassDeclaration(): KSClassDeclaration? {
}
}

fun KSAnnotated.findAnnotationFromUseSiteTarget(): Collection<KSAnnotation> {
return when (this) {
is KSPropertyGetter -> this.receiver.annotations.filter { it.useSiteTarget == AnnotationUseSiteTarget.GET }
is KSPropertySetter -> this.receiver.annotations.filter { it.useSiteTarget == AnnotationUseSiteTarget.SET }
else -> emptyList()
}
}


// TODO: cross module visibility is not handled
fun KSDeclaration.isVisibleFrom(other: KSDeclaration): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,11 @@ private inline fun ClassDescriptor.findEnclosedDescriptor(
kindFilter.accepts(it) && filter(it)
}
}

internal fun KSAnnotated.findAnnotationFromUseSiteTarget(): Collection<KSAnnotation> {
return when (this) {
is KSPropertyGetter -> (this.receiver as? KSDeclarationImpl)?.let { it.originalAnnotations.filter { it.useSiteTarget == AnnotationUseSiteTarget.GET } }
is KSPropertySetter -> (this.receiver as? KSDeclarationImpl)?.let { it.originalAnnotations.filter { it.useSiteTarget == AnnotationUseSiteTarget.SET } }
else -> emptyList()
} ?: emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.google.devtools.ksp.processor

import com.google.devtools.ksp.findAnnotationFromUseSiteTarget
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.getConstructors
import com.google.devtools.ksp.getDeclaredFunctions
Expand Down Expand Up @@ -46,13 +45,13 @@ class ImplicitElementProcessor : AbstractTestProcessor() {
result.add(JavaClass.primaryConstructor?.simpleName?.asString() ?: "<null>")
result.add(JavaClass.getDeclaredFunctions().map { it.simpleName.asString() }.joinToString(","))
val readOnly = ClsClass.declarations.single { it.simpleName.asString() == "readOnly" } as KSPropertyDeclaration
readOnly.getter?.let { result.add("readOnly.get(): ${it.origin} annotations from property: ${it.findAnnotationFromUseSiteTarget().map { it.shortName.asString() }.joinToString(",")}") }
readOnly.getter?.let { result.add("readOnly.get(): ${it.origin} annotations from property: ${it.annotations.map { it.shortName.asString() }.joinToString(",")}") }
readOnly.getter?.receiver?.let { result.add("readOnly.getter.owner: " + nameAndOrigin(it)) }
readOnly.setter?.let { result.add("readOnly.set(): ${it.origin}") }
readOnly.setter?.receiver?.let { result.add("readOnly.setter.owner: " + nameAndOrigin(it)) }
val readWrite = ClsClass.declarations.single { it.simpleName.asString() == "readWrite" } as KSPropertyDeclaration
readWrite.getter?.let { result.add("readWrite.get(): ${it.origin}") }
readWrite.setter?.let { result.add("readWrite.set(): ${it.origin} annotations from property: ${it.findAnnotationFromUseSiteTarget().map { it.shortName.asString() }.joinToString(",")}") }
readWrite.setter?.let { result.add("readWrite.set(): ${it.origin} annotations from property: ${it.annotations.map { it.shortName.asString() }.joinToString(",")}") }
val dataClass = resolver.getClassDeclarationByName(resolver.getKSNameFromString("Data"))!!
result.add(dataClass.getConstructors().map { it.toString() }.joinToString(","))
val comp1 = dataClass.declarations.single { it.simpleName.asString() == "comp1" } as KSPropertyDeclaration
Expand All @@ -61,8 +60,7 @@ class ImplicitElementProcessor : AbstractTestProcessor() {
val comp2 = dataClass.declarations.single { it.simpleName.asString() == "comp2" } as KSPropertyDeclaration
comp2.getter?.let { result.add("comp2.get(): ${it.origin}") }
comp2.setter?.let { result.add("comp2.set(): ${it.origin}") }
val annotationType = comp1.annotations[0].annotationType.resolve().declaration.qualifiedName!!.asString()
result.add(annotationType)
val annotationType = comp1.getter?.let { result.add(it.annotations[0].annotationType.resolve().declaration.qualifiedName!!.asString()) }
val ClassWithoutImplicitPrimaryConstructor = resolver.getClassDeclarationByName("ClassWithoutImplicitPrimaryConstructor")!!
result.add(ClassWithoutImplicitPrimaryConstructor.getConstructors().map { it.toString() }.joinToString(","))
val ImplictConstructorJava = resolver.getClassDeclarationByName("ImplictConstructorJava")!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ abstract class KSDeclarationImpl(ktDeclaration: KtDeclaration) : KSDeclaration {
override fun toString(): String {
return this.simpleName.asString()
}

internal val originalAnnotations: List<KSAnnotation> by lazy {
ktDeclaration.annotationEntries.map { KSAnnotationImpl.getCached(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.symbol.impl.kotlin

import com.google.devtools.ksp.processing.impl.findAnnotationFromUseSiteTarget
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.toKSModifiers
import com.google.devtools.ksp.symbol.impl.toLocation
Expand All @@ -29,7 +30,7 @@ abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor
KSPropertyDeclarationImpl.getCached(ktPropertyAccessor.property as KtProperty)
}
override val annotations: List<KSAnnotation> by lazy {
ktPropertyAccessor.annotationEntries.map { KSAnnotationImpl.getCached(it) }
ktPropertyAccessor.annotationEntries.map { KSAnnotationImpl.getCached(it) }.plus(this.findAnnotationFromUseSiteTarget())
}

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import com.google.devtools.ksp.symbol.impl.synthetic.KSPropertyGetterSyntheticIm
import com.google.devtools.ksp.symbol.impl.synthetic.KSPropertySetterSyntheticImpl
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.psi.KtAnnotated
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration

Expand All @@ -42,6 +45,10 @@ class KSPropertyDeclarationImpl private constructor(val ktProperty: KtProperty)
ResolverImpl.instance.resolveDeclaration(ktProperty) as? PropertyDescriptor
}

override val annotations: List<KSAnnotation> by lazy {
ktProperty.filterAccessorAnnotation().map { KSAnnotationImpl.getCached(it) }
}

override val extensionReceiver: KSTypeReference? by lazy {
if (ktProperty.isExtensionDeclaration()) {
KSTypeReferenceImpl.getCached(ktProperty.receiverTypeReference!!)
Expand Down Expand Up @@ -96,4 +103,12 @@ class KSPropertyDeclarationImpl private constructor(val ktProperty: KtProperty)
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyDeclaration(this, data)
}
}

internal fun KtAnnotated.filterAccessorAnnotation(): List<KtAnnotationEntry> {
return this.annotationEntries.filter { property ->
property.useSiteTarget?.getAnnotationUseSiteTarget()?.let {
it != AnnotationUseSiteTarget.PROPERTY_GETTER && it != AnnotationUseSiteTarget.PROPERTY_SETTER
} ?: true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.google.devtools.ksp.symbol.impl.binary.KSPropertyGetterDescriptorImpl
import com.google.devtools.ksp.symbol.impl.binary.KSPropertySetterDescriptorImpl
import com.google.devtools.ksp.symbol.impl.synthetic.KSPropertyGetterSyntheticImpl
import com.google.devtools.ksp.symbol.impl.synthetic.KSPropertySetterSyntheticImpl
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtStubbedPsiUtil
Expand All @@ -41,6 +42,10 @@ class KSPropertyDeclarationParameterImpl private constructor(val ktParameter: Kt
fun getCached(ktParameter: KtParameter) = cache.getOrPut(ktParameter) { KSPropertyDeclarationParameterImpl(ktParameter) }
}

override val annotations: List<KSAnnotation> by lazy {
ktParameter.filterAccessorAnnotation().map { KSAnnotationImpl.getCached(it) }
}

override val parentDeclaration: KSDeclaration? by lazy {
ktParameter.findParentDeclaration()!!.parentDeclaration
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package com.google.devtools.ksp.symbol.impl.synthetic

import com.google.devtools.ksp.processing.impl.findAnnotationFromUseSiteTarget
import com.google.devtools.ksp.symbol.*

abstract class KSPropertyAccessorSyntheticImpl(ksPropertyDeclaration: KSPropertyDeclaration) : KSPropertyAccessor {
override val annotations: List<KSAnnotation> = emptyList()
override val annotations: List<KSAnnotation> by lazy {
this.findAnnotationFromUseSiteTarget().toList()
}

override val location: Location by lazy {
ksPropertyDeclaration.location
Expand Down
6 changes: 2 additions & 4 deletions compiler-plugin/testData/api/annotationInDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
// property prop : annotations.FieldTarget2{[value = field:]}
// property prop : annotations.FieldTarget{[value = onProp]}
// property prop : annotations.NoTargetAnnotation{[value = onProp]}
// property prop : annotations.PropertyGetterTarget{[value = get:]}
// property prop : annotations.PropertySetterTarget{[value = set:]}
// property prop : annotations.PropertyTarget{[value = onProp]}
// setter of property prop : annotations.PropertySetterTarget{[value = set:]}
// lib.KotlinClass ->
Expand All @@ -53,6 +51,7 @@
// main.DataClass ->
// class main.DataClass : annotations.ClassTarget{[value = onDataClass]}
// class main.DataClass : annotations.NoTargetAnnotation{[value = onDataClass]}
// getter of property constructorParam : annotations.PropertyGetterTarget{[value = get:]}
// parameter constructorParam : annotations.FieldTarget2{[value = field:]}
// parameter constructorParam : annotations.FieldTarget{[value = onConstructorParam]}
// parameter constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]}
Expand All @@ -62,9 +61,8 @@
// property constructorParam : annotations.FieldTarget2{[value = field:]}
// property constructorParam : annotations.FieldTarget{[value = onConstructorParam]}
// property constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]}
// property constructorParam : annotations.PropertyGetterTarget{[value = get:]}
// property constructorParam : annotations.PropertySetterTarget{[value = set:]}
// property constructorParam : annotations.PropertyTarget{[value = onConstructorParam]}
// setter of property constructorParam : annotations.PropertySetterTarget{[value = set:]}
// lib.DataClass ->
// class lib.DataClass : annotations.ClassTarget{[value = onDataClass]}
// class lib.DataClass : annotations.NoTargetAnnotation{[value = onDataClass]}
Expand Down

0 comments on commit cd9ad2d

Please sign in to comment.