import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value(); } public class MyClass { @MyAnnotation("Hello, World!") public void myMethod() { } } import java.lang.reflect.Method; public class MyAnnotationProcessor { public static void process(Object object) { Class<?> clazz = object.getClass(); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); String value = annotation.value(); System.out.println("Processing annotation: " + value); } } } }


上一篇:
下一篇:
切换中文