[ https://issues.apache.org/jira/browse/MCOMPILER-424?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17800016#comment-17800016 ]
Alexander Kriegisch edited comment on MCOMPILER-424 at 12/23/23 8:17 AM: ------------------------------------------------------------------------- The bug was accepted as [JDK-8322706|https://bugs.java.com/bugdatabase/view_bug?bug_id=JDK-8322706] into the Oracle Java bug tracker and under the same ID [JDK-8322706|https://bugs.openjdk.org/browse/JDK-8322706] in the actual OpenJDK bug tracker. The latter is where you can watch the progress. was (Author: kriegaex): The bug was accepted as [JDK-8322706|https://bugs.java.com/bugdatabase/view_bug?bug_id=JDK-8322706] into the Oracle Java tracker and under the same ID [JDK-8322706|https://bugs.openjdk.org/browse/JDK-8322706] in the actual OpenJDK bug tracker. The latter is where you can watch the progress. > Annotation processor fails with AnnotationTypeMismatchException on Java 11 > which targets Java 8 > ----------------------------------------------------------------------------------------------- > > Key: MCOMPILER-424 > URL: https://issues.apache.org/jira/browse/MCOMPILER-424 > Project: Maven Compiler Plugin > Issue Type: Bug > Affects Versions: 3.8.1 > Environment: Apache Maven 3.6.3 > Java 11.0.7 > Reporter: Marian Macik > Priority: Major > > When I configure the Maven Compiler Plugin running on Java 11 to target Java > 8: > {code:xml} > <plugin> > <groupId>org.apache.maven.plugins</groupId> > <artifactId>maven-compiler-plugin</artifactId> > <version>3.8.1</version> > <configuration> > <source>1.8</source> > <target>1.8</target> > </configuration> > </plugin> > {code} > My compilation will fail with > {{java.lang.annotation.AnnotationTypeMismatchException}} when I use an > annotation processor. > I investigated it a little bit and found out that this happens only in a very > specific case which has to fulfil these criteria: > 1. One annotation which has the second annotation as a parameter. > 2. The second annotation has a parameter of type {{String[]}}. > 3. Java 11 has the compilation target set to Java 8. > The same scenario works without any issues when I configure the Maven > Compiler Plugin with {{<release>11</release>}}. > I debugged the code and when I set the Maven Compiler Plugin to source/target > 1.8, I noticed changes in the *AnnotationProxyMaker$ValueVisitor#getValue()* > method. In this case, on the line in the *getValue()* method > {code:java} > attr.accept(this); > {code} > the *attr* variable holds a *ClassReader$ArrayAttributeProxy* object (so the > second annotation's String[] parameter is being handled) and then I can see a > *ClassCastException* thrown inside the > *ClassReader$ArrayAttributeProxy#accept()* method: > {code:java} > public void accept(Visitor v) { > ((ProxyVisitor)v).visitArrayAttributeProxy(this); } > {code} > which says > {noformat} > class com.sun.tools.javac.model.AnnotationProxyMaker$ValueVisitor cannot be > cast to class com.sun.tools.javac.jvm.ClassReader$ProxyVisitor > (com.sun.tools.javac.model.AnnotationProxyMaker$ValueVisitor and > com.sun.tools.javac.jvm.ClassReader$ProxyVisitor are in module jdk.compiler > of loader 'app') > {noformat} > so the *Visitor v* (of type *AnnotationProxyMaker$ValueVisitor*) cannot be > cast to *ClassReader$ProxyVisitor*, which is correct by looking at the source > code, and then the exception is caught at the > *{{AnnotationProxyMaker$ValueVisitor#visitCompound()}}* method which handles > the whole second annotation (which the String[] parameter is a part of). > After that, it fails with the *AnnotationTypeMismatchException*. > Now, when I execute this with {{<release>11</release>}}, the *attr* variable > holds an *Attribute$Array* object and its *accept()* method looks different: > {code:java} > public void accept(Visitor v) { v.visitArray(this); } > {code} > and the code executes perfectly fine. > There is a workaround for this and it is pretty simple. You just need to add > an import of the second annotation on the annotated class being processed by > the processor, even if just the default value is used. Or, in case the > annotated class is in the same package as the annotation itself, you just > need to provide the default value explicitly, see: > {code:java} > package org.example; > /* > * Uncomment one of these lines and the annotation processor starts working. > */ > //import org.example.MySecondAnnotation; > @MyFirstAnnotation( > //secondAnnotation = @MySecondAnnotation > ) > public class AnnotatedClass { > } > {code} > Now I don't know if the issue is with the Java compiler or the Maven Compiler > Plugin, but if it is with the former, you can at least help me to contact the > Java compiler developers or forward me to the correct Java compiler issue > tracking system. > I have also prepared a reproducer which shows this issue. It is available on > GitHub [1] and includes a short README file with instructions on how to run > it. > Thanks for help! > [1] [https://github.com/MarianMacik/JDK11-annotation-processor-reproducer] -- This message was sent by Atlassian Jira (v8.20.10#820010)