7

Is annotation processing still an active part of Java 6+, or is it something that has been deprecated/discouraged/obsolesced. If obsolesced, why (why is it no longer needed/useful)? And if it's still extremely useful and "active" (a new Java project developing against the Java 6+ JDK would still benefit from it), please confirm/correct my understanding of how annotation processors are used:

  1. You create your own annotation class, say @MyAnnotation
  2. You mark certain classes, methods, fields, etc. with @MyAnnotation
  3. During the build process, you invoke your custom MyAnnotationProcessor (how?)
  4. The processor scans your classpath for instances of @MyAnnotation
  5. Typically, an annotation processor does dynamic bytecode injection, modifying/enhancing your compiled classes on-the-fly
RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 8
    What ever gave you the notion that it's deprecated? – Kirk Woll Jan 28 '13 at 19:02
  • 1
    annotations are available since java 5, it would be quite a poor design decision, if they would deprecate them in the next major release of the platform – Peter Butkovic Jan 28 '13 at 19:08
  • Unless you are building a utility library (like Spring), you do not need to create annotations. That does not mean that there is no use for annotations, just that you (most java developers) will not need to develop annotations. – DwB Jan 28 '13 at 19:14
  • Everybody - thanks! Is my understanding of their basic usage (the steps outlined above) correct, or am I way off base? – IAmYourFaja Jan 28 '13 at 19:17
  • 1
    You can also use Annotations at runtime. See here for an example: http://www.kodejava.org/examples/503.html – Tansir1 Jan 28 '13 at 21:15
  • Thanks @Tansir1 (+1) - can you confirm that my general understanding and usage of Annotations is correct (as outlined above)? Thanks again! – IAmYourFaja Jan 29 '13 at 12:43
  • @KirkWoll, the Mirror API for Annotation Processing is deprecated since Java 7, think that's what the asker meant. – Ragunath Jawahar Aug 23 '13 at 07:57

1 Answers1

16
  1. Correct.
  2. Correct.
  3. Correct. Typically you will extend AbstractProcessor. You specify your MyAnnotationProcessor class using the ServiceLoader pattern - this makes it discoverable to the compiler. The Processor Javadoc contains some information on creating this class.
  4. Correct.
  5. This part is not correct. The annotation processor framework does not give you the ability to modify classes. You will need to use some post-compile process to do this as part of your build. What it does allow you to do is create new files. These may be simple resources, new Java source files (that will subsequently be required and eligible to the annotation processor during the same compile), or Java class files. You may also perform custom checks on source code and write errors to the log (causing the compile to fail).

I hope that addresses your understanding questions.

jbunting
  • 886
  • 5
  • 10
  • 1
    Also, I found this article a decent introduction to writing annotation processors: http://deors.wordpress.com/2011/10/08/annotation-processors/ . – jbunting Jan 29 '13 at 12:55