1

In another post (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.

Community
  • 1
  • 1
Ralph
  • 31,584
  • 38
  • 145
  • 282

2 Answers2

2

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
  • 33,639
  • 11
  • 75
  • 118
0

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
  • 73,784
  • 33
  • 194
  • 347