Java 17: Cannot read value from annotation
timonzi opened this issue · 3 comments
We currently switch to Java 17 and have the problem with the following constellation:
An object Obj
is annotated with Annotation @ObjectAnnotation
. This is processed by our annotation processor.
A property within the object is annotated with @AnnotationA
.
AnnotationA
contains a property b
with type AnnotationB
and default value @AnnotationB
.
AnnotationB
contains a property c
with type AnnotationC
and default value @AnnotationC
.
When not trying to access the property b
, I get an exception:
java.lang.annotation.AnnotationTypeMismatchException: Incorrectly typed data found for annotation element public abstract org.example2.AnnotationB org.example2.AnnotationA.b() (Found data of type org.example2.AnnotationB)
at jdk.compiler/com.sun.tools.javac.model.AnnotationProxyMaker$ValueVisitor$1AnnotationTypeMismatchExceptionProxy.generateException(AnnotationProxyMaker.java:271)
at java.base/sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:89)
at jdk.proxy5/jdk.proxy5.$Proxy28.b(Unknown Source)
at org.example.processor.ObjectProcessor.parseBo(ObjectProcessor.java:62)
at org.example.processor.ObjectProcessor.process(ObjectProcessor.java:45)
at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:1023)
at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:939)
at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1267)
at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1382)
This does not happen when:
- We use OpenJDK 11
- Add an (unused) import of
AnnotationB
in the class (Obj
) - Remove
c
fromAnnotationB
When I use the "normal" reflection mechanism I don't have any problem to resolve the values. But this is not possible in the annotation processor.
The strange thing is also: When I would add another object which looks like the same like Obj
and I would catch the exception for the first processed object, it works like expected for the second one. Here I don't get an exception.
Is there any advice how to handle the exception? Is there a compatibility issue with Java 17?
I created reproducer projects for this: https://github.com/timonzi/annotation-processor-jdk17
Reproduction steps: Execute a (clean) install on...
- annotation-holder
- annotation-processor
- java (here the exceptions occurs)
If anyone has the same problem: It can be solved, when the a value of the default value annotation is overridden.
Example:
AnnotationB b() default @AnnotationB(dummyValue = "someValue");
It does not work with
AnnotationB b() default @AnnotationB;
I'm not clear what JavaPoet has to do with all of this.
Yeah, me neither in the meantime. I thought JavaPoet would do more here, but after looking at the code more closely I don't think JavaPoet has anything to do with it either.
That's why I'm closing the issue again.