你的代码
- System.out.println(AnnotationTest.class.isAnnotationPresent(ItAnnotation.class));
复制代码
打印是AnnotationTest 这个类上是否有ItAnnotation注解,需将代码改为判断main方法是否加注解
代码如下
- //@ItAnnotation(num = 6)
- public class AnnotationTest {
- /**
- * @param args
- * @throws NoSuchMethodException
- * @throws SecurityException
- */
- @ItAnnotation(num = 6)
- public static void main(String[] args) throws SecurityException, NoSuchMethodException {
- System.out.println(AnnotationTest.class.getMethod("main", String[].class).isAnnotationPresent(ItAnnotation.class));
- if(AnnotationTest.class.getMethod("main", String[].class).isAnnotationPresent(ItAnnotation.class)){
- ItAnnotation ano = (ItAnnotation)(AnnotationTest.class.getMethod("main", String[].class).getAnnotation(ItAnnotation.class));
- System.out.println(ano.num());
- }
- }
- }
复制代码
|