Annotation Processor
I downloaded the source and called it AnnotationProcessor. Our class AnnotationProcessor extends AbstractProcessor.
@SupportedAnnotationTypes(
{
"annotations.MyClassAnnotation",
"annotations.MySourceAnnotation",
"annotations.MyRuntimeAnnotation"
})
The following commands worked at the command prompt.
D:\AnnotationsProject\out\production\AnnotationsProject>javac -d . -cp . -processor AnnotationProcessor ../../../src/TestingAnnotatedClasses.java
// output at the Terminal
CLASS unnamed package.TestingAnnotatedClasses is annotated with annotations.MyRuntimeAnnotation
CLASS unnamed package.TestingAnnotatedClasses is annotated with annotations.MyClassAnnotation
CLASS unnamed package.TestingAnnotatedClasses is annotated with annotations.MySourceAnnotation
D:\AnnotationsProject\out\production\AnnotationsProject>
javac -d . -cp . -processor AnnotationProcessor ../../../src/TestingAnnotatedClasses.java
The AnnotationProcessor can leverage annotations of every type of Retention Policy while it executes.
You can build an AnnotationProcessor to accomplish any task.
- generate log files
- Build XML
Look at the AnnotationTarget used to annotate Annotations.
Go back to the terminal. After we have made the changes, this is the output of the terminal session.
D:\AnnotationsProject\out\production\AnnotationsProject>javac -d . -cp . -processor AnnotationProcessor ../../../src/TestingAnnotatedClasses.java
CLASS unnamed package.TestingAnnotatedClasses is annotated with annotations.MyRuntimeAnnotation
FIELD TestingAnnotatedClasses.myField is annotated with annotations.MyClassAnnotation
METHOD TestingAnnotatedClasses.printRuntimeAnnotations() is annotated with annotations.MySourceAnnotation
D:\AnnotationsProject\out\production\AnnotationsProject>
Here is the related problem:
'author', 'version', 'description' missing though required
After adding this code
@MyRuntimeAnnotation(
author = "Tim",
version = 1.0,
description = "This class is used for testing annotations"
)
to TestingAnnotatedClasses.java
the errors in MyRuntimeAnnotation.java disappeared.
When you run the class TestingAnnotatedClasses.java, this is the output that will appear.
Class Annotation: @annotations.MyRuntimeAnnotation(author="Tom", version=1.0, description="This class is used for testing annotations")
D:\AnnotationsProject\out\production\AnnotationsProject>javap annotations\MyRuntimeAnnotation
Warning: File .\annotations\MyRuntimeAnnotation.class does not contain class annotations\MyRuntimeAnnotation
Compiled from "MyRuntimeAnnotation.java"
public interface annotations.MyRuntimeAnnotation extends java.lang.annotation.Annotation {
public abstract java.lang.String author();
public abstract double version();
public abstract java.lang.String description();
}
D:\AnnotationsProject\out\production\AnnotationsProject>
Under the package annotations select MyRuntimeAnnotation
From the IntelliJ menu select
View --> Show ByteCode
Create a new directory under the AnnotationsProject and call it docs.
Under the src folder select TestingAnnotatedClasses
This is the output folder for the General HTML created through Java Docs.
file:///D:/AnnotationsProject/docs/TestingAnnotatedClasses.html
@MyRuntimeAnnotation(author="Thomas Hauck",
version=1.1,
description="This class is used for testing annotations")
public class TestingAnnotatedClasses
extends java.lang.Object