3

I need a help on java custom annotations, I know how to create annotations but I do not know how to process that.
I have gone through some information where I saw APT which is com.sun.mirror.* and another javax.annotation.processing.*, I got confused between two.

Can any one please guide me to process custom annotations and provide useful link.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Madhavi
  • 41
  • 1
  • 5

2 Answers2

4

There is a difference between the old apt (annotation processing tool, in com.sun.mirror) and its successor, the Pluggable Annotation API (which is a part of javac since 1.6). The new API used for processing is in javax.annotation.processing.

The API used for analysing declaration elements of the source code is the Mirror API, its package is in javax.lang.model, that API has similarities with the Reflection API.

Many sources will probably talk about apt, but are still valid for the processor tool in javac. Just the packages and the way to run the processing tool have changed. Here is a tutorial.

kapex
  • 28,903
  • 6
  • 107
  • 121
2

If you need to process annotations at/before compile time (i.e. for producing "side files") then use apt.

OTOH if you need to process annotations at runtime then just use java reflection to get annotation on particular class.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154