Looking for some feedback on how to handle repeate...
# ksp
y
Looking for some feedback on how to handle repeated annotations in XProcessing (but relevant to any user of KSP). (in thread)
Java repeated annotations work by declaring a wrapper annotation:
Copy code
@Repeatable(RepeatableJavaAnnotation.List.class)
public @interface RepeatableJavaAnnotation {
    String value();
    @interface List {
        RepeatableJavaAnnotation[] value();
    }
}
Copy code
@RepeatableJavaAnnotation("x")
@RepeatableJavaAnnotation("y")
class SomeClass
when you read this in JavaAP, you only see the
List
version. In KSP, you see the
RepeatableJavaAnnotation
if it is in source and
List
version if it is in
.class
files. That being said, kotlin compiler does not support non-source retention repeatable annotations so the
.class
case can only ever happen for java source that were compiled to
.class
. https://youtrack.jetbrains.com/issue/KT-12794 My initial plan was to make people use the
List
version in XProcessing to query those but that cannot work because the
Repeatable
annotation in Kotlin does not have a container parameter (hence even if i can detect it is a repeatable annotation when parsing annotations from ksp, i cannot find its container). So what I'm considering right now is to provide:
Copy code
XAnnotated.getAnnotations(baseClass, containerClass = null): List<XAnnotationBox>
and also
remove
toAnntoationBox(annotationClass):XAnnotationBox?
it is an ugly API but not sure what else to do. Looking for any feedback but also wanted to raise this here as more people might be hitting this repeated annotation issue.
hmm actually, because we ask you to pass the annotation, we can actually discover the container via reflection hmm
ok yea that works.
need to figure out behavior for
hasAnnotation
though...
tl;dr; i was able to keep
getAnnotations(KCLass)
and just find the container via reflection (and handle inconsistencies between ksp javap internally)
👍 1
it just won't work consistently if someone passes the container annotation instead of the repeatable annotation but there is no way to detect that because container annotation does not have a reference to the contained annotation 🤷 (it kind of does via value but that seemed a bit too much work to do just to detect the error)
a
Just looked around at a surface level, but I was wondering how does
XAnnotated.getAnnotations
relate to
AnnotatedConstruct.getAnnotationsByType
? I currently have a repeatable annotation (defined in Kotlin) that I am generating code with with
kapt
and
ksp
(I’ve written the processing code twice for those, with a shared code generation portion). On the
kapt
side, I’m using
getAnnotationsByType
to get consistent behavior with
ksp