Skip to content

Commit

Permalink
handle more implicit annotation use site targets for value parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Jan 26, 2023
1 parent 8d41698 commit e3ce61c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down
9 changes: 6 additions & 3 deletions test-utils/testData/api/annotationInDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = ""
Expand Down

0 comments on commit e3ce61c

Please sign in to comment.