Annotation documentation and Java class implementations for Java SE 22, with the following improvements:
- Structured package use.
- (No child elements for this item)
- Modern Java syntax with generic safety (
Class<?>
).
- (No child elements for this item)
- Clarified retention policies with examples.
- (No child elements for this item)
- Organized comments and method-level JavaDoc.
- (No child elements for this item)
- Added use of
@Target
for better clarity.
- (No child elements for this item)
- Enhanced formatting and readability for training or documentation purposes.
- (No child elements for this item)
📁 `src/annotations/MyClassAnnotation.java`
package annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Annotation retained in the class file but not available at runtime.
*/
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
public @interface MyClassAnnotation {
}
📁 `src/annotations/MyRuntimeAnnotation.java`
package annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Runtime annotation accessible via reflection.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
public @interface MyRuntimeAnnotation {
}
📁 `src/annotations/MySourceAnnotation.java`
package annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
/**
* Source-only annotation, discarded by the compiler.
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
public @interface MySourceAnnotation {
}
📁 `TestingAnnotatedClasses.java` (default package)
import annotations.MyClassAnnotation;
import annotations.MyRuntimeAnnotation;
import annotations.MySourceAnnotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Demonstrates usage of annotations with different retention policies.
*/
@MyRuntimeAnnotation
@MyClassAnnotation
@MySourceAnnotation
public class TestingAnnotatedClasses {
@MyRuntimeAnnotation
private String field;
@MyClassAnnotation
public void annotatedMethod() {
}
/**
* Print runtime-visible annotations on the class and its members.
*/
public void printRuntimeAnnotations() {
Class> clazz = this.getClass();
System.out.println("Inspecting annotations for: " + clazz.getName());
showAnnotations(clazz);
for (Method method : clazz.getDeclaredMethods()) {
showAnnotations(method);
}
for (Field field : clazz.getDeclaredFields()) {
showAnnotations(field);
}
}
/**
* Displays all declared annotations on a reflective element.
*
* @param element a class, method, or field
*/
private void showAnnotations(Object element) {
Annotation[] annotations = switch (element) {
case Class> cls -> cls.getDeclaredAnnotations();
case Method m -> m.getDeclaredAnnotations();
case Field f -> f.getDeclaredAnnotations();
default -> new Annotation[0];
};
for (Annotation annotation : annotations) {
System.out.println("Annotation: " + annotation);
}
}
public static void main(String[] args) {
new TestingAnnotatedClasses().printRuntimeAnnotations();
}
}
✅ Sample Output at Runtime
Inspecting annotations for: TestingAnnotatedClasses
Annotation: @annotations.MyRuntimeAnnotation()
Annotation: @annotations.MyRuntimeAnnotation()
🔍 Note: Only annotations with `RetentionPolicy.RUNTIME` are visible at runtime. `@MyClassAnnotation` is stored in the `.class` file but **not** visible via reflection, and `@MySourceAnnotation` is discarded during compilation.