How to use the "classes" parameter for JavaCompiler.getTask ()?

I am trying to understand JavaCompiler.getTask() . I understand all the parameters except the second, last, called classes . Javadok read:

class names (for annotation processing), null means no class names

but I don’t understand what they mean. I found many sites linking to JavaCompiler online, but none of them explain this parameter. Any ideas?

+4
source share
1 answer

I believe this can be used when you want to run annotation handlers in binary files. Classes will be the types you want to handle.

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 displays "Hello" .

+2
source

All Articles