Java注解

Java 注解(jdk1.7)

java注解是java中的一种类型,是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。 
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。
存在于java.lang.annotation包下

一、Annontation的Type注释

annotation包下,有关于Annotation Type的主要有Documented,Inherited,Retention,Target,其作用如下:

Documented: 说明该注解将被包含在javadoc中

该类的代码如下:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

Inherited:说明子类可以继承父类中的该注解

该类的代码如下:

<span style="font-size:18px;">@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}</span>

Retention:定义注解的保留策略

该类代码如下:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();
}

RetentionPolicy 类也是存在于Annotation 包下,是一个枚举类,该类包含的值为:

<span style="font-size:18px;">public enum RetentionPolicy {
SOURCE,//注解仅存在于源码中,在class字节码文件中不包含
CLASS,// 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
RUNTIME //注解会在class字节码文件中存在,在运行时可以通过反射获取到
}</span>

Target:定义注解的作用目标

该类代码:

<span style="font-size:18px;">@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}</span>

ElementType 类也是存在于Annotation 包下,是一个枚举类,该类包含的值为:

<span style="font-size:18px;">public enum ElementType {
    TYPE,//接口、类、枚举、注解
    FIELD,//字段、枚举的常量
    METHOD,//方法
    PARAMETER,// 方法参数
    CONSTRUCTOR,//构造函数
    LOCAL_VARIABLE,// 局部变量
    ANNOTATION_TYPE,//  注解
    PACKAGE  //包   
}</span>

Target的elementType 可以有多个

二、自定义注释

自定义两个注释类

<span style="font-size:18px;">@Documented//被包含在javaDoc中
@Retention(RetentionPolicy.RUNTIME)//在编辑和运行时都保留
@Target(ElementType.METHOD)//作用在类、接口、枚举及方法上
public @interface Yts {
    public enum YtsType{util,entity,serivce,model};
    public YtsType classType() default YtsType.entity;
}
@Documented//被包含在javaDoc中
@Inherited //子类默认继承该注释
@Retention(RetentionPolicy.RUNTIME)//在编辑和运行时都保留
@Target(ElementType.METHOD)//作用在类、接口、枚举及方法上
public @interface HelloWorld {
    public String name() default "";//默认值为空!
}</span>


三、解析自定义的注解

java的反射机制可以帮助,得到注解,代码如下:

<span style="font-size:18px;">public class ParaseAnnotation {
    //解析得到方法注解
    public void paraseMethod(Class clazz)
            throws NoSuchMethodException, IllegalAccessException,
            InvocationTargetException, InstantiationException {
        Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
        for (Method method : clazz.getDeclaredMethods()) {
            HelloWorld say = method.getAnnotation(HelloWorld.class);
            String name = "";
            if (say != null) {
                name = say.name();
                System.out.println("name:"+name);
            }
            Yts yts = method.getAnnotation(Yts.class);
            if (yts != null) {
                if (YtsType.entity.equals(yts.classType())) {
                    System.out.println("this is a entity method");
                } else {
                    System.out.println("this is a " + yts.classType() + " method");
                }
            }
        }
    }
    //解析得到类注解
    public void paraseType(Class clazz) {
        Yts yts = (Yts) clazz.getAnnotation(Yts.class);
        if (YtsType.entity.equals(yts.classType())) {
            System.out.println("this is a entity calss");
        } else {
            System.out.println("this is a " + yts.classType() + " class");
        }
    }
}</span>

测试类:

@Yts(classType=YtsType.model)
public class SayHell {

    @Yts
    @HelloWorld(name = " johk")
    public void sayHello(String name){

    }
    public static void main(String args[]) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        ParaseAnnotation parase = new ParaseAnnotation();
        parase.paraseMethod(SayHell.class);
        parase.paraseType(SayHell.class);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值