10

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?

assylias
  • 321,522
  • 82
  • 660
  • 783
user855
  • 19,048
  • 38
  • 98
  • 162
  • http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html. Take a look here. It is possible. – vels4j Jan 13 '13 at 10:21
  • 1
    That's what annotation processing is for. You're basically asking if it can do what it does. – Bohemian Jan 13 '13 at 11:21

2 Answers2

16

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

pranith
  • 869
  • 9
  • 24
10

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.

Alex
  • 25,147
  • 6
  • 59
  • 55
  • 1
    `warning: The apt tool and its associated API are planned to be removed in the next major JDK release. These features have been superseded by javac and the standardized annotation processing API, javax.annotation.processing and javax.lang.model.` – Sridhar Sarnobat Nov 07 '16 at 22:48