tags:

views:

68

answers:

2

Is there any type of callback mechanism within the Java compiler? I'd like to be a able to register a listener to act when certain tokens are found within the source.

Annotations let you do this, but unfortunately this won't work for what I am trying to do.

A: 

Project Lombok does attach to the compiler. It is using Annotations to trigger it (which is what you want to avoid, I know), but within the source code you will find how it is attaching to the compiler. There is special code for the Eclipse compiler and the Sun compiler, there seems to be no common 'public compiler API' (except for starting the compiler of course).

Possibly javax.script works for you. In this case you can use Bindings, which let you define a callback mechanism for unknown variables. But without details what exactly you want to do it's hard to tell if this is an option for you.

Thomas Mueller
A: 

If you don't mind doing it programmatically, you can investigate the 1.6 Java Compiler API and Compiler Tree API. Both of those, plus other info, are linked from the javac guide. Here's how you might start:

JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
// setup params for the compilation task
JavacTask jct = (JavacTask)jc.getTask(...); // com.sun.source.util.JavacTask
TaskListener tl = ...;
jct.setTaskListener(tl);
kschneid