Step 1) create a interface as below
A)
package com.myapp.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/*
* Annotations can be greatly used by the Build-time
* Tools and Run-time Environments to do a bunch of
* useful tasks like Code Generation, Validation.Java 6 has introduced a
* new JSR called JSR 269, which is the Pluggable Annotation Processing API.
* With this API, now it is possible for the Application Developers to write a
* Customized Annotation Processor which can be plugged-in to
* the code to operate on the set of Annotations that appear in a Source File.
* */
@Target(value = { ElementType.TYPE })
public @interface ClassLevelAnnotationInterface {
}
B)
package com.myapp.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(value = { ElementType.METHOD })
public @interface MethodLevelAnnotationInterface {
}
Stpe 2) Now apply your above annotation on class and method as follows
package com.myapp.annotation;
/*
* Annotations can be greatly used by the Build-time
* Tools and Run-time Environments to do a bunch of
* useful tasks like Code Generation, Validation.
* Java 6 has introduced a new JSR called JSR 269,
* which is the Pluggable Annotation Processing API.
* With this API, now it is possible for the Application Developers to write a
* Customized Annotation Processor which can be plugged-in to
* the code to operate on the set of Annotations that appear in a Source File.
* */
@ClassLevelAnnotationInterface()
public class AnnotationApplliedOnClassAndMethod {
@MethodLevelAnnotationInterface()
public void myAnnotatedMethod() {
}
}
Step 3) Now create a class like below that extends AbstractProcessor and implement process method
package com.myapp.annotation;
import java.util.Iterator;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
@SupportedAnnotationTypes(value = ("*"))
@SupportedSourceVersion(SourceVersion.RELEASE_0)
public class TestAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (Iterator iterator = annotations.iterator(); iterator.hasNext();) {
TypeElement typeElement = (TypeElement) iterator.next();
System.out.println(typeElement.getQualifiedName());
}
System.out.println(roundEnv.getRootElements().toString());
return true;
}
}
Step 4) Now Test your above written classes as follows
package com.myapp.annotation;
import java.util.Iterator;
import java.util.Set;
public class TestMyAnnonatedClasses {
public static void main(String[] args) {
TestAnnotationProcessor procesor = new TestAnnotationProcessor();
Set<String> supportedAnnotationTypes = procesor
.getSupportedAnnotationTypes();
for (Iterator<String> iterator = supportedAnnotationTypes.iterator(); iterator
.hasNext();) {
String annotationType = iterator.next();
System.out.println(annotationType);
}
Set<String> supportedOptions = procesor.getSupportedOptions();
for (Iterator iterator = supportedOptions.iterator(); iterator
.hasNext();) {
String supportedOption = (String) iterator.next();
System.out.println(supportedOption);
}
System.out.println(procesor.getSupportedSourceVersion().name());
}
}
A)
package com.myapp.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/*
* Annotations can be greatly used by the Build-time
* Tools and Run-time Environments to do a bunch of
* useful tasks like Code Generation, Validation.Java 6 has introduced a
* new JSR called JSR 269, which is the Pluggable Annotation Processing API.
* With this API, now it is possible for the Application Developers to write a
* Customized Annotation Processor which can be plugged-in to
* the code to operate on the set of Annotations that appear in a Source File.
* */
@Target(value = { ElementType.TYPE })
public @interface ClassLevelAnnotationInterface {
}
B)
package com.myapp.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(value = { ElementType.METHOD })
public @interface MethodLevelAnnotationInterface {
}
Stpe 2) Now apply your above annotation on class and method as follows
package com.myapp.annotation;
/*
* Annotations can be greatly used by the Build-time
* Tools and Run-time Environments to do a bunch of
* useful tasks like Code Generation, Validation.
* Java 6 has introduced a new JSR called JSR 269,
* which is the Pluggable Annotation Processing API.
* With this API, now it is possible for the Application Developers to write a
* Customized Annotation Processor which can be plugged-in to
* the code to operate on the set of Annotations that appear in a Source File.
* */
@ClassLevelAnnotationInterface()
public class AnnotationApplliedOnClassAndMethod {
@MethodLevelAnnotationInterface()
public void myAnnotatedMethod() {
}
}
Step 3) Now create a class like below that extends AbstractProcessor and implement process method
package com.myapp.annotation;
import java.util.Iterator;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
@SupportedAnnotationTypes(value = ("*"))
@SupportedSourceVersion(SourceVersion.RELEASE_0)
public class TestAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (Iterator iterator = annotations.iterator(); iterator.hasNext();) {
TypeElement typeElement = (TypeElement) iterator.next();
System.out.println(typeElement.getQualifiedName());
}
System.out.println(roundEnv.getRootElements().toString());
return true;
}
}
Step 4) Now Test your above written classes as follows
package com.myapp.annotation;
import java.util.Iterator;
import java.util.Set;
public class TestMyAnnonatedClasses {
public static void main(String[] args) {
TestAnnotationProcessor procesor = new TestAnnotationProcessor();
Set<String> supportedAnnotationTypes = procesor
.getSupportedAnnotationTypes();
for (Iterator<String> iterator = supportedAnnotationTypes.iterator(); iterator
.hasNext();) {
String annotationType = iterator.next();
System.out.println(annotationType);
}
Set<String> supportedOptions = procesor.getSupportedOptions();
for (Iterator iterator = supportedOptions.iterator(); iterator
.hasNext();) {
String supportedOption = (String) iterator.next();
System.out.println(supportedOption);
}
System.out.println(procesor.getSupportedSourceVersion().name());
}
}