annotation 用法示例

参考http://annotation.group.iteye.com/group/wiki/385-%E5%AE%9E%E6%88%98%E7%AF%87%EF%BC%9A%E8%AE%BE%E8%AE%A1%E8%87%AA%E5%B7%B1%E7%9A%84Annotation
参考上文,自己实现了一个简单的annotation 用法示例

1 Client.java代码如下:

package com.annotationTest;

import java.lang.reflect.Method;


/**
 * describe: annotation 用法示例
 * create on: 2011-06-07
 * @author sylor.liu
 * @version 1.0
 * @since jdk1.6
 */
public class Client {

    /**
     * @param args
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws SecurityException, NoSuchMethodException {
        // TODO Auto-generated method stub
        AnnotationClass  ac = new AnnotationClass();
        // ac.getMethod();
        Class cls = ac.getClass();
       
        //Method method = cls.getDeclaredMethod("getMethod",new Class[0]);  //方法无参数时
        Method method = cls.getDeclaredMethod("getMethod",String.class);
        boolean flag = method.isAnnotationPresent(Name.class);
        if (flag) {
            System.out.println("exec method use AnnotationWithOneArg!");
           
            // 获取注解参数
            Name ann = method.getAnnotation(Name.class);
            System.out.println(ann.author());
            System.out.println(ann.company());
        }
       
    }

}
2 Name .java代码如下:

package com.annotationTest;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/* Retention取值:SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,
 * Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
 * ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,
 * 并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS.
 * 第三个,是RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
 * 举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息;相反,
 * @Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated.
 */

/*  Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.说明一下:TYPE(类型), FIELD(属性),
 *  METHOD(方法), PARAMETER(参数), CONSTRUCTOR(构造函数),LOCAL_VARIABLE(局部变量), ANNOTATION_TYPE,PACKAGE(包),
 *  其中的TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上.
 *  另外,从1的源代码可以看出,@Target自己也用了自己来声明自己,只能用在ANNOTATION_TYPE之上.
 *  如果一个Annotation类型没有指明@Target使用在哪些元素上,那么它可以使用在任何元素之上,这里的元素指的是上面的八种类型.
 */
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
    String author();
    String company();
}

3 AnnotationClass .java代码如下:

package com.annotationTest;

public class AnnotationClass {
   
       @Name(author="Sylor",company="ailk")
       public void getMethod(String str){
           System.out.println("hi...");
       }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值