Java Reference
In-Depth Information
public class App {
@Unfinished(message = "Make sure that this
element is not null")
String size;
@Unfinished
public static void main(String[] args) {
System.out.println("Hello World
annotation!");
}
}
Although our annotation already looks like a standard annotation, it is not yet oper-
ational. For this, a class called processor must be available to the compiler. This
class will describe the action to take when an item is annotated with our custom an-
notation.
To achieve a custom processor for Java 6 annotation, we mainly need to implement
the process() methodofthe javax.annotation.processing.Processor in-
terface and define the annotations supported by this processor with the @Suppor-
tedAnnotationTypes annotation. The following code shows the processor of our
custom Unfinished annotation. As you can see, for the implementation of the
process() method, we used the abstract class AbstractProcessor that imple-
ments the Processor interface. (This prevents us from having to implement all the
methods defined in this interface.)
@SupportedAnnotationTypes("com.packt.ch07.annotations.Unfinished")
public class UnfinishedProcessor extends
AbstractProcessor {
/**
* For the ServiceLoader
*/
public UnfinishedProcessor() {
}
@Override
Search WWH ::




Custom Search