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.