JDK-6764597 : MirroredTypesException never thrown
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u10
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2008-10-28
  • Updated: 2010-04-03
  • Resolved: 2008-10-28
Related Reports
Duplicate :  
Description
Write

---%<--- Ann.java
public @interface Ann {
    Class clazz();
    Class[] clazzes();
}
---%<--- Sample.java
@Ann(clazz=Number.class, clazzes={Object.class, String.class})
public class Sample {}
---%<--- AnnProcessor.java
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.MirroredTypesException;
import javax.tools.Diagnostic.Kind;
@SupportedAnnotationTypes("Ann")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class AnnProcessor extends AbstractProcessor {
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if (roundEnv.processingOver()) {
            return false;
        }
        for (Element e : roundEnv.getElementsAnnotatedWith(Ann.class)) {
            try {
                e.getAnnotation(Ann.class).clazz();
            } catch (MirroredTypeException x) {
                processingEnv.getMessager().printMessage(Kind.NOTE, "Found clazz: " + x.getTypeMirror());
            }
            try {
                e.getAnnotation(Ann.class).clazzes();
            } catch (MirroredTypesException x) {
                processingEnv.getMessager().printMessage(Kind.NOTE, "Found clazzes: " + x.getTypeMirrors());
            }
        }
        return true;
    }
}
---%<--- META-INF/services/javax.annotation.processing.Processor
AnnProcessor
---%<---

and after compiling, recompile Sample.java with the same sourcepath, using the dest in the javac bootcp.

Expected output:

---%<---
Note: Found clazz: java.lang.Number
Note: Found clazzes: java.lang.Object,java.lang.String
---%<---

Under either JDK 6 or 7, I get

---%<---
Note: Found clazz: java.lang.Number


An annotation processor threw an uncaught exception.
Consult the following stack trace for details.
javax.lang.model.type.MirroredTypeException: Attempt to access Class object for TypeMirror java.lang.Object
        at com.sun.tools.javac.model.AnnotationProxyMaker$MirroredTypeExceptionProxy.generateException(AnnotationProxyMaker.java:297)
        at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:56)
        at $Proxy5.clazzes(Unknown Source)
        at AnnProcessor.process(AnnProcessor.java:26)
        at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:746)
        at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:675)
        at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:820)
        at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1029)
        at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:772)
        at com.sun.tools.javac.main.Main.compile(Main.java:393)
        at com.sun.tools.javac.main.Main.compile(Main.java:313)
        at com.sun.tools.javac.main.Main.compile(Main.java:304)
        at com.sun.tools.javac.Main.compile(Main.java:82)
        at com.sun.tools.javac.Main.main(Main.java:67)
---%<---

Generally, MirroredTypesException is never thrown when it is supposed to be; rather, MirroredTypeException is thrown on the first element of the array.

Comments
SUGGESTED FIX Besides elemName being unqualified (always just "Class"), the current code would not even work - throws UnsupportedOperationException from add. What works for me: diff --git a/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java b/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java --- a/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java +++ b/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java @@ -179,14 +179,14 @@ } public void visitArray(Attribute.Array a) { - Name elemName = ((ArrayType) a.type).elemtype.tsym.name; + Name elemName = ((ArrayType) a.type).elemtype.tsym.getQualifiedName(); if (elemName == elemName.table.names.java_lang_Class) { // Class[] // Construct a proxy for a MirroredTypesException List<TypeMirror> elems = List.nil(); - for (Attribute value : a.values) { - Type elem = ((Attribute.Class) value).type; - elems.add(elem); + for (Attribute v : a.values) { + Type elem = ((Attribute.Class) v).type; + elems = elems.append(elem); } value = new MirroredTypesExceptionProxy(elems);
28-10-2008