4

Even after spending a good time, I am unable to understand the purpose of Annotation Processing.

I understand why annotations are required for run-time, simplest example I can think are:

  1. Replacement of marker interface.
  2. Replacement of market properties of a type (e.g. transient)
  3. In general, any usefulness that can be done at runtime.

But unfortunately, i could not understand any practical example/reason of using annotation at compile time(except for default annotations provided by JDK e.g. @Override, etc).

I could not understand what is the purpose/need of 'generating code' using Annotation Processors.

Edit: Javadoc/Custom Java doc is one utility I can think of as a purpose of using annotation processors.

2
  • Is this question about the general concept of consuming annotations at compile-time, or about the javax.annotation.processing APIs and what they're used for? Commented Nov 10, 2014 at 5:36
  • General processing of annotations at compile time. Commented Nov 10, 2014 at 12:30

2 Answers 2

4

This can be used for all sorts of things.

Two simple examples

  1. The Lombock project. Tired of writing thousands of getters and setters? Why not let an annotation processor do it at compile time.
  2. AOP. You can use something like AspectJ to weave in code dependent on annotations. This would be done post compile but as part of the compilation process. For example Spring AOP uses the @Transactional annotation in combination with AspectJ to weave transaction code around methods marked with the annotation.

There are many other uses, but they generally break down into two categories

  1. To reduce boiler plate code.
  2. For cross-cutting concerns.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. "Lombock project" makes a lot of sense. But I thought @Transactional is a "Runtime property". Did you mean Spring AOP performs Annotation Processing on the source code and update the code to make it transactional?
It can, yes. Strictly speaking the alternative is load time weaving. So when the class is loaded.
1

There are two main purposes of the annotation processing environment - analysis and code generation.

The analysis permits you to extend the capabilities of the java compiler, analyzing program elements as they are being compiled, possibly adding additional constraints, validations, and reporting errors and warnings for violations of those constraints.

Code generation permits you to generate additional supplementary code from signals in your existing hand-written code, primarily (though not exclusively) keyed off of Annotations.

Some examples include Dagger, which is a system for compile-time-analyzed dependency injection, reporting errors and warnings normally found at runtime instead during the compilation of the code. Dagger also generates all the code that would normally be done with reflection, or by hand-writing glue-code, providing substantial performance benefits (in some cases) as well as infrastructure code which is available for step-through debugging, etc.

Another example is the Checker Framework which evaluates a variety of checks against your code, including null safety, etc.

A third example is Auto-Value intended to make small value types nearly trivial to write.

One thing the annotation processing environment is decidedly not suited for is mutation of existing code in place, or modification of code currently under compilation. While some projects do this, they are not actually using the annotation processor APIs but casting to internal compiler types to do so. While this is clearly possible, it's potentially brittle, and may not work reliably from version to version, or compiler to compiler, requiring custom handling for each version and compiler vendor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.