diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt index 7e3b57f638..afc2629ad9 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSAnnotationResolvedImpl.kt @@ -114,6 +114,11 @@ class KSAnnotationResolvedImpl private constructor( } override val useSiteTarget: AnnotationUseSiteTarget? by lazy { + // Do not use compiler hard-coded use-site target. + // FIXME: use origin after it is fixed. + if (parent?.origin == Origin.KOTLIN_LIB || parent?.origin == Origin.JAVA_LIB) + return@lazy null + when (annotationApplication.useSiteTarget) { null -> null FILE -> AnnotationUseSiteTarget.FILE @@ -128,6 +133,7 @@ class KSAnnotationResolvedImpl private constructor( } } + // FIXME: use parent.origin override val origin: Origin = Origin.KOTLIN_LIB override val location: Location by lazy { diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSTypeReferenceResolvedImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSTypeReferenceResolvedImpl.kt index e86a966944..6e21c88f8d 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSTypeReferenceResolvedImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/resolved/KSTypeReferenceResolvedImpl.kt @@ -58,6 +58,7 @@ class KSTypeReferenceResolvedImpl private constructor( } override val element: KSReferenceElement? by lazy { + // FIXME: synthetic elements can have non-synthetic annotations via use-site targets if (parent == null || parent.origin == Origin.SYNTHETIC) { null } else { diff --git a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/ImplicitElementProcessor.kt b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/ImplicitElementProcessor.kt index 4273671c8e..7eb31d02d1 100644 --- a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/ImplicitElementProcessor.kt +++ b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/ImplicitElementProcessor.kt @@ -20,6 +20,7 @@ package com.google.devtools.ksp.processor import com.google.devtools.ksp.getClassDeclarationByName import com.google.devtools.ksp.getConstructors import com.google.devtools.ksp.getDeclaredFunctions +import com.google.devtools.ksp.getDeclaredProperties import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSDeclaration @@ -89,6 +90,18 @@ class ImplicitElementProcessor : AbstractTestProcessor() { ) val ImplictConstructorJava = resolver.getClassDeclarationByName("ImplictConstructorJava")!! result.add(ImplictConstructorJava.getConstructors().map { it.toString() }.joinToString(",")) + + listOf("Test", "lib.Test").forEach { clsName -> + resolver.getClassDeclarationByName(clsName)!!.let { cls -> + cls.getDeclaredProperties().single().let { annotated -> + result.add( + "$clsName, $annotated: ${annotated.annotations.toList().map { + "${it.shortName.asString()}: ${ it.useSiteTarget }" + }}" + ) + } + } + } return emptyList() } } diff --git a/test-utils/testData/api/implicitElements.kt b/test-utils/testData/api/implicitElements.kt index 88d670c541..6402d59393 100644 --- a/test-utils/testData/api/implicitElements.kt +++ b/test-utils/testData/api/implicitElements.kt @@ -33,7 +33,48 @@ // GetAnno // // synthetic constructor for ImplictConstructorJava +// Test, p: [MyKotlinAnnotation: null, MyJavaAnnotation: null] +// lib.Test, p: [MyKotlinAnnotation: null, MyJavaAnnotation: null] // END +// MODULE: lib +// FILE: lib/MyJavaAnnotation.java +package lib; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.LOCAL_VARIABLE; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.MODULE; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({FIELD}) +public @interface MyJavaAnnotation {} + +// FILE: lib/MyKotlinAnnotation.kt +package lib +@Target(AnnotationTarget.PROPERTY) +annotation class MyKotlinAnnotation + +// FILE: lib/Test.kt +package lib +class Test( + @MyKotlinAnnotation + @MyJavaAnnotation + val p: Int +) + +// MODULE: main(lib) +// FILE: Test.kt +import lib.* +class Test( + @MyKotlinAnnotation + @MyJavaAnnotation + val p: Int +) // FILE: a.kt annotation class GetAnno annotation class SetAnno @@ -66,4 +107,4 @@ public class JavaClass { public class ImplictConstructorJava { -} \ No newline at end of file +}