Modern Java Annotation Processing
annotation-processing, annotations, java
Solution
- Correct.
- Correct.
- 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.
- Correct.
- 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.
Problem
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: - You create your own annotation class, say `@MyAnnotation` - You mark certain classes, methods, fields, etc. with `@MyAnnotation` - During the build process, you invoke your custom `MyAnnotationProcessor` (how?) - The processor scans your classpath for instances of `@MyAnnotation` - Typically, an annotation processor does dynamic bytecode injection, modifying/enhancing your compiled classes on-the-fly