Here's a practical Eclipse IDE Java Annotation Processing demo using AnnotationMirror:
Project Structure:
AnnotationMirrorDemo/
├── src/
│ ├── annotations/
│ │ └── DataClass.java
│ ├── processors/
│ │ └── DataProcessor.java
│ └── demo/
│ └── User.java
├── resources/
│ └── META-INF/
│ └── services/
│ └── javax.annotation.processing.Processor
└── pom.xml (for Maven) or .classpath
Step 1: Create the Annotation
// File: src/annotations/DataClass.java
package annotations;
import java.lang.annotation.*;
@Retention(RetentionPolicy.SOURCE) // Only needed at compile time
@Target(ElementType.TYPE) // Can only be applied to classes
public @interface DataClass {
String tableName() default "";
boolean persistent() default true;
String[] tags() default {};
Class<?> repository() default Void.class;
}
Step 2: Create the Annotation Processor
// File: src/processors/DataProcessor.java
package processors;
import annotations.DataClass;
import javax.annotation.processing.*;
javax.lang.model.SourceVersion;
javax.lang.model.element.*;
javax.lang.model.type.DeclaredType;
javax.lang.model.util.*;
javax.tools.Diagnostic;
javax.tools.JavaFileObject;
java.io.Writer;
java.util.*;
java.util.stream.Collectors;
{
ProcessingEnvironment processingEnv;
Elements elementUtils;
Types typeUtils;
Filer filer;
Messager messager;
{
.init(processingEnv);
.processingEnv = processingEnv;
.elementUtils = processingEnv.getElementUtils();
.typeUtils = processingEnv.getTypeUtils();
.filer = processingEnv.getFiler();
.messager = processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE, );
}
{
(TypeElement annotation : annotations) {
Set<? > annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
(Element element : annotatedElements) {
(element.getKind() == ElementKind.CLASS) {
processDataClass((TypeElement) element);
}
}
}
;
}
{


