views:

29

answers:

2

In another post (http://stackoverflow.com/questions/3396074/scala-maven-and-preprocessors) I asked about preprocessing Java and Scala using a tool like m4. I need to add __FILE__ and __LINE__ capabilities (please, no "use cases" questions). Someone suggested checking out Java compiler plugins (javax.annotation.processing.Processor).

How would one go about doing this using special annotations (@File, @Line, or @FileLine maybe)? Any examples similar to this would be greatly appreciated.

+1  A: 

In a comment on your previous question you mentioned http://www.gallot.be/?p=85, which uses a javaagent. It should be relatively easy to modify that code to run the same transformation in a preprocessing step. You would need to extract the CodeLocationClassAdapter into its own toplevel class and call it like this for each of your class files:

String name = "com/stackoverflow/Test.class";
byte[] bytes = // read bytes of the classfile from disk

ClassReader cr = new ClassReader(bytes);
ClassWriter cw = new ClassWriter(cr, 0);
ClassVisitor cv = new CodeLocationClassAdapter(cw);

cr.accept(cv, 0);

// write modified class file
OutputStream out = new FileOutputStream(name);
out.write(cw.toByteArray());
out.close();
Jörn Horstmann
@Jörn: Thanks. I'll look into that.
Ralph
A: 

If I understand this correctly, the standard way to do this is with JSR-45 like it is done for JSP-pages to allow debugging.

Would that be an option for your chosen preprocessor?

Thorbjørn Ravn Andersen