Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle AnnotationTarget.VALUE_PARAMETER for property declarations. #1216

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal fun KtAnnotated.filterUseSiteTargetAnnotations(): Sequence<KtAnnotation
return this.annotationEntries.asSequence().filter { property ->
property.useSiteTarget?.getAnnotationUseSiteTarget()?.let {
it != AnnotationUseSiteTarget.PROPERTY_GETTER && it != AnnotationUseSiteTarget.PROPERTY_SETTER &&
it != AnnotationUseSiteTarget.SETTER_PARAMETER
it != AnnotationUseSiteTarget.SETTER_PARAMETER && it != AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER
} ?: true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ class KSPropertyDeclarationParameterImpl private constructor(val ktParameter: Kt

override val annotations: Sequence<KSAnnotation> by lazy {
ktParameter.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) }
.filter { it.useSiteTarget != AnnotationUseSiteTarget.PARAM }
.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
}
}
}

override val parentDeclaration: KSDeclaration? by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.google.devtools.ksp.symbol.Location
import com.google.devtools.ksp.symbol.Origin
import com.google.devtools.ksp.symbol.impl.synthetic.KSTypeReferenceSyntheticImpl
import com.google.devtools.ksp.symbol.impl.toLocation
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.lexer.KtTokens.CROSSINLINE_KEYWORD
import org.jetbrains.kotlin.lexer.KtTokens.NOINLINE_KEYWORD
import org.jetbrains.kotlin.psi.KtAnnotationEntry
Expand Down Expand Up @@ -69,7 +70,13 @@ class KSValueParameterImpl private constructor(val ktParameter: KtParameter) : K
}

override val annotations: Sequence<KSAnnotation> by lazy {
ktParameter.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) }
ktParameter.annotationEntries.asSequence().filter { annotation ->
annotation.useSiteTarget?.getAnnotationUseSiteTarget()?.let {
it != AnnotationUseSiteTarget.PROPERTY_GETTER &&
it != AnnotationUseSiteTarget.PROPERTY_SETTER &&
it != AnnotationUseSiteTarget.SETTER_PARAMETER
} ?: true
}.map { KSAnnotationImpl.getCached(it) }
.plus(this.findAnnotationFromUseSiteTarget()).memoized()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo
ktPropertySymbol.annotations.asSequence()
.filter { !it.isUseSiteTargetAnnotation() }
.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<*>)?.any {
(it as? KSClassDeclaration)?.qualifiedName
?.asString() == "kotlin.annotation.AnnotationTarget.VALUE_PARAMETER"
} ?: false
}
}
}

override val getter: KSPropertyGetter? by lazy {
Expand Down Expand Up @@ -99,7 +109,8 @@ internal fun KtAnnotationApplication.isUseSiteTargetAnnotation(): Boolean {
return this.useSiteTarget?.let {
it == AnnotationUseSiteTarget.PROPERTY_GETTER ||
it == AnnotationUseSiteTarget.PROPERTY_SETTER ||
it == AnnotationUseSiteTarget.SETTER_PARAMETER
it == AnnotationUseSiteTarget.SETTER_PARAMETER ||
it == AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER
} ?: false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class KSValueArgumentImpl private constructor(
private fun KtAnnotationValue.toValue(): Any? = when (this) {
is KtArrayAnnotationValue -> this.values.map { it.toValue() }
is KtAnnotationApplicationValue -> KSAnnotationImpl.getCached(this.annotationValue)
// TODO: Enum entry should return a type, use declaration as a placeholder.
is KtEnumEntryAnnotationValue -> this.callableId?.classId?.let {
analyze {
it.toKtClassSymbol()?.let {
Expand Down
5 changes: 4 additions & 1 deletion test-utils/testData/api/annotationInDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
// parameter param1 : annotations.ValueParameterTarget{[value = onParam1]}
// parameter param2 : annotations.NoTargetAnnotation{[value = onParam2]}
// parameter param2 : annotations.ValueParameterTarget{[value = onParam2]}
// parameter propInConstructor : annotations.ValueParameterTarget{[value = propInConstructor]}
// property prop : annotations.FieldTarget2{[value = field:]}
// property prop : annotations.FieldTarget{[value = onProp]}
// property prop : annotations.NoTargetAnnotation{[value = onProp]}
Expand All @@ -59,6 +60,7 @@
// 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]}
Expand Down Expand Up @@ -108,7 +110,7 @@ package lib;
import annotations.*;
@NoTargetAnnotation("onClass")
@ClassTarget("onClass")
class KotlinClass {
class KotlinClass(@ValueParameterTarget("propInConstructor") val propInConstructor: String ) {
@NoTargetAnnotation("onProp")
@FieldTarget("onProp")
@PropertyTarget("onProp")
Expand Down Expand Up @@ -186,6 +188,7 @@ class DataClass(
@get:PropertyGetterTarget("get:")
@field:FieldTarget2("field:")
@setparam:ValueParameterTarget("onConstructorParam")
@ValueParameterTarget("onConstructorParam")
var constructorParam : String = ""
)
// FILE: main/JavaClassInModule2.java
Expand Down