3

Is it possible to create a preprocessor like functionality that is available in C and provided by Antenna. Can we use the APT tool to achieve this functionality? Are there any articles or links on similar topics?

1
  • -1 for being unclear what you want. Commented Sep 23, 2009 at 8:32

2 Answers 2

2

Annotations are not meant as a tool to transform code; they just add metadata to code. You can't use annotations for conditional compilation, for example.

As Sun's tutorial on annotations says:

Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.

Wikipedia says:

When Java source code is compiled, annotations can be processed by compiler plug-ins called annotation processors. Processors can produce informational messages or create additional Java source files or resources, which in turn may be compiled and processed, but processors cannot modify the annotated code itself.

So an annotation processor plug-in is not going to be able to give you all of the functionality that the C preprocessor has.

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

Comments

2

You can perform compile-time tasks using the annotation processing framework. It's not as powerful as a preprocessor, since you can't do things like:

@RunOnlyOn(OS.Mac) public void someMethod() { ... } 

Some good use cases for annotation processors are:

  • creating mapping files from annotated classes, e.g. create a hibernate mapping file;
  • creating indexes of classes which have certain annotation, e.g. create testng xml files from a source folder of test classes;
  • enforce compile-time constraints not usually available, e.g. having a no-arg constructor.

Please note that as of Java 6 APT is no longer needed, since all properly declared annotation processors take part in the compilation.

4 Comments

Actually you can if you make your own annotation checking classloader and implement the annotation yourself; however it's not worth the time most of the times.
@Esko : that sounds distressingly interesting :-) Do you have a link for a proof-of-concept?
Erm, I guess I could make one (haven't really written ClassLoaders myself) but it would take some time. Damn, now you got me thinking of actually doing it :) I'll report back eventually if I come up with something useful, just don't hold your breath :)
I know this is over 2 years later, but just to continue the thoughts - using something like Soot, you could take your annotations for conditional compilation, and then transform the byte code based on if a given method / class / statement has a particular annotation. You'd still need to write the Soot transformations to do your conditional compilation, but the Soot toolkit provides a lot of functionality for doing this kind of analysis / transformations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.