1- What is Annotation Processing Tool (APT)
The situation is that:
You create your own Annotations and use them in your Java application. You set using rules for these Annotations. You want the Java Compiler to notify the error of applying the rules wrongly, if any, at the compile-time. If you use Eclipse for writing codes, you want Eclipse will notify the using error right on IDE.
This request is feasible to APT (Annotation Processing Tool).
Java annotation processing tool is a tool you can use to process annotations in the sourcecode. All you need is to implement an annotation processor.
@PublicFinal is your annotation. Your rule is that it only can annotate on the method or field whose modifier is public and final. If you use it wrongly, there will be a notice displayed at the compile-time, and a notice on IDE at once.
This is the example model I will introduce in this document:
- @PublicFinal is used for method or field whose modifiers are public and final.
- @Controller only uses for class, and the name of class must have the Controller suffix.
- @Action only use for method, returning to String.
Processors including PublicFinalProcessor, ControllerProcessor, ActionProcesser are responsible for having notifications if there are errors at the compile-time. It includes the display of notifying errors on IDE Eclipse, too.
First, create a Project.
Create file javax.annotation.processing.Processor located in folder META-INF/services as shown below:
Packing APTProcessor project into jar file:
Right-click Project and select Export:
Create Project APTTutorial:
Right-click APTTutorial select property.
Declare use APTProccessor library that you created earlier.
Declare use your Annotation Processor with Compiler.
Declaring Processor library location:
You can click on Advanced... to see Processors registered with Compiler.
Click OK to complete:
Create a test class using your Annotation and Processor:
The error message seen on the IDE:
log.txt created by DevLog, using for developer.