Reflection – Accessing Annotations in Java


Reflection allows you to inspect and manipulate classes, methods, and interfaces at runtime.

Source Code

import java.lang.reflect.Method;

public class AnnotationExample {
    public static void main(String[] args) throws Exception {
        Method method = MyClass.class.getMethod("myMethod");

        if (method.isAnnotationPresent(MyCustomAnnotation.class)) {
            MyCustomAnnotation myAnnotation = method.getAnnotation(MyCustomAnnotation.class);
            System.out.println("Value: " + myAnnotation.value());
        }
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments