黑马程序员技术交流社区

标题: 那如何判断类方法的注解?、、、 [打印本页]

作者: 陈斌    时间: 2012-3-1 22:25
标题: 那如何判断类方法的注解?、、、
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()得到注解?
作者: 冯旭君    时间: 2012-3-11 13:43
你可以先得到类,然后得到类中的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。)这个方法的调用者可以自由的修饰这个数组(返回的数组),它将不影响其他的调用者。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2