diff options
author | Jiaxiang Chen <jiaxiang@google.com> | 2023-01-25 13:25:03 -0800 |
---|---|---|
committer | KSP Auto Pick <kotlin-symbol-processing@google.com> | 2023-01-26 00:39:26 +0000 |
commit | 354e861d5837a16a780f6beb17f902b77c4aff18 (patch) | |
tree | 30f137e19322d58bbd7cbe769c38bdcb21043ffa | |
parent | 0bad1aa1955d2c9bd81033a5a3d4d50fea3daee6 (diff) | |
download | ksp-354e861d5837a16a780f6beb17f902b77c4aff18.tar.gz |
handle more implicit annotation use site targets for value parameter
(cherry picked from commit e3ce61c4a1de91026a1c5b94ae15ef5458148509)
3 files changed, 30 insertions, 13 deletions
diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSPropertyDeclarationParameterImpl.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSPropertyDeclarationParameterImpl.kt index 03674f73..1e69d243 100644 --- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSPropertyDeclarationParameterImpl.kt +++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSPropertyDeclarationParameterImpl.kt @@ -40,14 +40,17 @@ class KSPropertyDeclarationParameterImpl private constructor(val ktParameter: Kt ktParameter.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) } .filterNot { valueParameterAnnotation -> valueParameterAnnotation.useSiteTarget == AnnotationUseSiteTarget.PARAM || - valueParameterAnnotation.annotationType.resolve().declaration.annotations.any { metaAnnotation -> - metaAnnotation.annotationType.resolve().declaration.qualifiedName - ?.asString() == "kotlin.annotation.Target" && - (metaAnnotation.arguments.singleOrNull()?.value as? ArrayList<*>)?.any { - (it as? KSType)?.declaration?.qualifiedName - ?.asString() == "kotlin.annotation.AnnotationTarget.VALUE_PARAMETER" - } ?: false - } + ( + valueParameterAnnotation.annotationType.resolve() + .declaration.annotations.any { metaAnnotation -> + metaAnnotation.annotationType.resolve().declaration.qualifiedName + ?.asString() == "kotlin.annotation.Target" && + (metaAnnotation.arguments.singleOrNull()?.value as? ArrayList<*>)?.any { + (it as? KSType)?.declaration?.qualifiedName + ?.asString() == "kotlin.annotation.AnnotationTarget.VALUE_PARAMETER" + } ?: false + } && valueParameterAnnotation.useSiteTarget == null + ) } } diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSValueParameterImpl.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSValueParameterImpl.kt index c8ed1ebf..162d77fa 100644 --- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSValueParameterImpl.kt +++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSValueParameterImpl.kt @@ -25,6 +25,7 @@ import com.google.devtools.ksp.symbol.KSAnnotation import com.google.devtools.ksp.symbol.KSName import com.google.devtools.ksp.symbol.KSNode import com.google.devtools.ksp.symbol.KSPropertyDeclaration +import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.symbol.KSTypeReference import com.google.devtools.ksp.symbol.KSValueParameter import com.google.devtools.ksp.symbol.KSVisitor @@ -75,9 +76,19 @@ class KSValueParameterImpl private constructor(val ktParameter: KtParameter) : K annotation.useSiteTarget?.getAnnotationUseSiteTarget()?.let { it != AnnotationUseSiteTarget.PROPERTY_GETTER && it != AnnotationUseSiteTarget.PROPERTY_SETTER && - it != AnnotationUseSiteTarget.SETTER_PARAMETER + it != AnnotationUseSiteTarget.SETTER_PARAMETER && + it != AnnotationUseSiteTarget.FIELD } ?: true - }.map { KSAnnotationImpl.getCached(it) } + }.map { KSAnnotationImpl.getCached(it) }.filterNot { valueParameterAnnotation -> + valueParameterAnnotation.annotationType.resolve().declaration.annotations.any { metaAnnotation -> + metaAnnotation.annotationType.resolve().declaration.qualifiedName + ?.asString() == "kotlin.annotation.Target" && + (metaAnnotation.arguments.singleOrNull()?.value as? ArrayList<*>)?.none { + (it as? KSType)?.declaration?.qualifiedName + ?.asString() == "kotlin.annotation.AnnotationTarget.VALUE_PARAMETER" + } ?: false + } + } .plus(this.findAnnotationFromUseSiteTarget()).memoized() } diff --git a/test-utils/testData/api/annotationInDependencies.kt b/test-utils/testData/api/annotationInDependencies.kt index 9c959512..705bc560 100644 --- a/test-utils/testData/api/annotationInDependencies.kt +++ b/test-utils/testData/api/annotationInDependencies.kt @@ -56,15 +56,13 @@ // class main.DataClass : annotations.NoTargetAnnotation{[value = onDataClass]} // getter of property constructorParam : annotations.PropertyGetterTarget{[value = get:]} // parameter <set-?> : annotations.ValueParameterTarget{[value = onConstructorParam]} -// parameter constructorParam : annotations.FieldTarget2{[value = field:]} -// parameter constructorParam : annotations.FieldTarget{[value = onConstructorParam]} // parameter constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]} -// parameter constructorParam : annotations.PropertyTarget{[value = onConstructorParam]} // parameter constructorParam : annotations.ValueParameterTarget{[value = onConstructorParam]} // property constructorParam : annotations.FieldTarget2{[value = field:]} // property constructorParam : annotations.FieldTarget{[value = onConstructorParam]} // property constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]} // property constructorParam : annotations.PropertyTarget{[value = onConstructorParam]} +// property constructorParam : annotations.ValueParameterAndFieldTarget{[value = valueParameterAndField]} // setter of property constructorParam : annotations.PropertySetterTarget{[value = set:]} // lib.DataClass -> // class lib.DataClass : annotations.ClassTarget{[value = onDataClass]} @@ -104,6 +102,10 @@ annotation class FunctionTarget(val value:String) @Target(AnnotationTarget.VALUE_PARAMETER) annotation class ValueParameterTarget(val value:String) + +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FIELD) +annotation class ValueParameterAndFieldTarget(val value: String) + // MODULE: lib(annotations) // FILE: ClassInLib.kt package lib; @@ -187,6 +189,7 @@ class DataClass( @set:PropertySetterTarget("set:") @get:PropertyGetterTarget("get:") @field:FieldTarget2("field:") + @field:ValueParameterAndFieldTarget("valueParameterAndField") @setparam:ValueParameterTarget("onConstructorParam") @ValueParameterTarget("onConstructorParam") var constructorParam : String = "" |