Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top