Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modern Java Annotation Processing

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
like image 718
IAmYourFaja Avatar asked Jan 28 '13 19:01

IAmYourFaja


People also ask

What is Java annotation processing?

"Annotation Processing" is a hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then (by producing compiler errors, compiler warning, emitting source code, byte code ...). API reference: javax. annotation.

How does annotation processing work?

Annotation processing takes place at compile time (compile time = the time when the java compiler compiles your java source code). Annotation processing is a tool build in javac for scanning and processing annotations at compile time. You can register your own annotation processor for certain annotations.

What is annotation processing tool?

Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.

Which annotation is newly introduced in Java 8?

Java 8 has included two new features repeating and type annotations in its prior annotations topic. In early Java versions, you can apply annotations only to declarations. After releasing of Java SE 8 , annotations can be applied to any type use. It means that annotations can be used anywhere you use a type.


1 Answers

  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.

like image 171
jbunting Avatar answered Oct 05 '22 21:10

jbunting