Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can annotation processor be used for code generation?

Let's say I define an annotation called @MyAnnotation.

There is a class X which is declared as:

@MyAnnotation
class X { .... }

Now at compile time I want to inspect all classes annotated with @MyAnnotation and do some code generation to more java source files that need to be compiled as well in the same process.

Is this possible using java annotation processor or some other tool?

like image 231
user855 Avatar asked Jan 13 '13 09:01

user855


2 Answers

APT tool has been merged into javac in Java 6. This is a much better tutorial for annotation processing.

like image 105
pranith Avatar answered Oct 24 '22 11:10

pranith


You may take a look at the Java apt (Annotation Processing Tool) for such a thing.

You can find the Getting Started page, and a nice article (1, 2, 3) about how to use that to generate code.

like image 34
Alex Avatar answered Oct 24 '22 10:10

Alex