A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈斌 中级黑马   /  2012-3-1 22:25  /  2882 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package Study.Annotation;

import javax.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
public class MyAnnotation {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
     sayHello();
     if(MyAnnotation.class.isAnnotationPresent(Deprecated.class)==false)
     {
             System.out.println("ss");
     }
        }
        @Deprecated
        public static void sayHello()
        {
          System.out.println("hhhhh");
        }

}//判断类注解的是这样的MyAnnotation.class.isAnnotationPresent(Deprecated.class)==false

那如何判断类方法的注解?、、、例如如何sayhello()得到注解?

1 个回复

倒序浏览
你可以先得到类,然后得到类中的Method类,这个类提供了getDeclaredAnnotations()方法
public Annotation[] getDeclaredAnnotations()
Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
返回连接到此对象上所有的注解。不像这个接口的其他方法一样,这个方法忽视继承来的注解。(如果没有注解直接和此元素对应,那么返回的数组长度为0。)这个方法的调用者可以自由的修饰这个数组(返回的数组),它将不影响其他的调用者。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马