I'm trying to understand JavaCompiler.getTask()
. I understand all the parameters except for the second to last one called classes
. The Javadoc read:
class names (for annotation processing), null means no class names
but I don't understand what they mean. I found plenty of websites referring to JavaCompiler online, but none of them explain this parameter. Any ideas?
I believe this can be used when you want to run annotation processors on binaries. The classes would be the types you want to process.
Demo code:
public class MyProcessor extends AbstractProcessor {
public static @interface X { String value(); }
@X("Hello") public static class Y {}
@Override public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (Element element : roundEnv.getRootElements()) {
X x = element.getAnnotation(X.class);
if (x != null) System.out.println(x.value());
}
return true;
}
@Override public Set<String> getSupportedAnnotationTypes() {
return new HashSet<String>(Arrays.asList(X.class.getCanonicalName()));
}
@Override public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_6;
}
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<String> classes = Arrays.asList(Y.class.getCanonicalName());
List<String> options = Arrays.asList("-processor", MyProcessor.class
.getCanonicalName());
CompilationTask task = compiler.getTask(null, null, null, options, classes,
null);
task.call();
}
}
The above code prints out "Hello"
.