Maven: <dependency> <groupId>javax.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>1.3.5</version> </dependency> Gradle: implementation 'javax.annotation:jakarta.annotation-api:1.3.5' import jakarta.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Cacheable { } public class SampleClass { @Cacheable(cacheTime = 120) public void getData() { } } import jakarta.annotation.Annotation; import jakarta.annotation.processing.*; import java.lang.reflect.*; import java.util.*; public class AnnotationParser { public static void parseAnnotations(Class<?> clazz) { Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(Cacheable.class)) { Cacheable cacheableAnnotation = method.getAnnotation(Cacheable.class); int cacheTime = cacheableAnnotation.cacheTime(); System.out.println("Found @Cacheable annotation with cache time: " + cacheTime); } } } } public class Main { public static void main(String[] args) { SampleClass sample = new SampleClass(); AnnotationParser.parseAnnotations(sample.getClass()); } } Found @Cacheable annotation with cache time: 120


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