Skip to content

Commit

Permalink
Do not use compiler hard-coded use-site target
Browse files Browse the repository at this point in the history
K2 attaches PROPERTY/FIELD unconditionally when deserializing
properties/fields in JvmBinaryAnnotationDeserializer.

They cannot be used to determine whether there is a use-site target,
or the annotation is immediately on the property/element in the source.
  • Loading branch information
ting-yuan committed Aug 27, 2024
1 parent 57f54c8 commit ffc4f9d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
43 changes: 42 additions & 1 deletion test-utils/testData/api/implicitElements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,48 @@
// GetAnno
// <init>
// 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
Expand Down Expand Up @@ -66,4 +107,4 @@ public class JavaClass {

public class ImplictConstructorJava {

}
}

0 comments on commit ffc4f9d

Please sign in to comment.