3

I am trying to write an annotation processor in Java 6. I wrote a sample implementation, which creates a new source file in the process method and it works fine.

@SupportedAnnotationTypes(value = {"*"}) @SupportedSourceVersion(SourceVersion.RELEASE_6) public class BrownfieldAnnotationProcessor extends AbstractProcessor{ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { try { JavaFileObject f = processingEnv.getFiler(). createSourceFile("in.test.ExtraClass"); processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Creating " + f.toUri()); Writer w = f.openWriter(); try { PrintWriter pw = new PrintWriter(w); pw.println("package in.test;"); pw.println("public class ExtraClass implements TestInterface{"); pw.println(" public void print() {"); pw.println(" System.out.println(\"Hello boss!\");"); pw.println(" }"); pw.println("}"); pw.flush(); } finally { w.close(); } } catch (IOException x) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, x.toString()); } return true; } 

}

But in my case, i do not want another java file to be created and instead i want to generate a class file directly. How to create a class file? Should i use a dynamic compiler to compile this source in string to create the class? In that scenario, i can directly store the class files in the file system. What is the usage of processingEnv.getFiler().createClassFile() method?

I tried googling around, but could never find an example using this method.

2
  • 1
    Unrelated to the concrete question, I suggest using a library like JavaPoet (github.com/square/javapoet) to generate the code instead of hardcoding things like curly brackets, reserved words, semicolons, etc. Commented Jun 13, 2016 at 14:14
  • Filer.createClassFile(..) is broken in JDK 1.8 (build 1.8.0_121-b13). I have tested using it in many ways, and it never worked. Nevertheless, such functionality would be useful to copy previously generated files, instead of making statements to generate the source. Commented Mar 15, 2017 at 23:02

1 Answer 1

2

I see only few reasons for creating class files instead of source code files. Actually this are the same reasons why you would ever need to create bytecode by hand in any situation.

I think you rarely need to do any of the above. If this is really what you want to do, you should know a lot about the format of class files and bytecode of course. You would basically be creating some kind of compiler at this point.

You could argue that by shipping precompiled classes, or by generating bytcode directly, you can skip a step in the built process and the user's program will compiler faster. That may be true true but I'm sure the speed will be neglectable. It would be easier to just work with source code and pass it to the filer for compilation.

Should i use a dynamic compiler to compile this source in string to create the class?

No, I see no advantage here. If you create a source code file with the filer it will be compiled automatically. You only need to do this if your source code is not Java but some other language that you want to compile for the JVM.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, agreed. Created src files instead of class files now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.