Java Reference
In-Depth Information
The annotation processor object is instantiated by the compiler using a no-args constructor. You must have a
no-args constructor for your processor class, so that the compiler can instantiate it. The default constructor for your
VersionProcessor class will meet this requirement.
The next step is to add two pieces of information to the processor class. The first one is about what kind of
annotations processing are supported by this processor. You can specify the supported annotation type using
@SupportedAnnotationTypes annotation at class level. The following snippet of code shows that the VersionProcessor
supports processing of com.jdojo.annotation.Version annotation type:
@SupportedAnnotationTypes({"com.jdojo.annotation.Version"})
public class VersionProcessor extends AbstractProcessor {
// Code goes here
}
You can use an asterisk ( * ) by itself or as part of the annotation name of the supported annotation types. The
asterisk works as a wild card. For example, "com.jdojo.*" means any annotation types whose names start with
"com.jdojo." . An asterisk only ( "*") means all annotation types. Note that when an asterisk is used as part of
the name, the name must be of the form PartialName.* . For example, "com*" and "com.*jdojo" are invalid uses
of an asterisk in the supported annotation types. You can pass multiple supported annotation types using the
SupportedAnnotationTypes annotation. The following snippet of code shows that the processor supports processing
for the com.jdojo.Ann1 annotation and any annotations whose name begins with com.jdojo.annotation :
@SupportedAnnotationTypes({"com.jdojo.Ann1", "com.jdojo.annotation.*"})
You need to specify the latest source code version that is supported by your processor using a
@SupportedSourceVersion annotation. The following snippet of code specifies the source code version 8 as the
supported source code version for the VersionProcessor class:
@SupportedAnnotationTypes({"com.jdojo.annotation.Version"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class VersionProcessor extends AbstractProcessor {
// Code goes here
}
The next step is to provide the implementation for the process() method in the processor class. Annotation
processing is performed in rounds. An instance of the RoundEnvironment interface represents a round. The javac
compiler calls the process() method of your processor by passing all annotations that the processor declares to
support and a RoundEnvironment object. The return type of the process() method is boolean . If it returns true , the
annotations passed to it are considered to be claimed by the processor. The claimed annotations are not passed to
other processors. If it returns false , the annotations passed to it are considered as not claimed and other processor
will be asked to process them. The following snippet of code shows the skeleton of the process() method:
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// The processor code goes here
}
The code you write inside the process() method depends on your requirements. In your case, you want to look
at the major and minor values for each @Version annotation in the source code. If either of them is less than zero, you
want to print an error message. To process each Version annotation, you will iterate through all Version annotation
instances passed to the process() method as
for (TypeElement currentAnnotation : annotations) {
// Code to validate each Version annotation goes here
}
 
Search WWH ::




Custom Search