微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义注解

自定义注解:
1.成员类型是受限的,合法的类型包括原始类型及String,Calss,Anootation,Enumreation
2.如果注解只有一个成员,则成员名必须取名为Value(),在使用的时可以忽略成员名和赋值号(=)
3.没有成员的注解称为标识注解

public @interface Description{//使用@interface关键字注解
String name();//成员以无参无异常方式声明
String author();
int age() default 19;//可以用default为成员变量指定一个认值
}

元注解
@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD})
// Target 注解的作用域 CONSTRUCTOR 构造方法声明,FIELD 字段声明,LOCAL_VARIABLE 局部变量声明 ,METHOD 方法声明,PACKAGE 包声明,ParaMETER 参数声明,TYPE 类接口。
@Retention(RetentionPolicy.RUNTIME)
//Retention 生命周期——SOURCE 只在源码显示,编译时会丢弃;CLASS 编译时会记录到class中,运行时忽略;RUNTIME 运行时存在,可以通过反射读取。
@Inherited
//Inherited 允许子类继承
@Documented
//Documented 生成javadoc的时候包含注解

下面以一个demo演示解析注解

package com.description.demo;

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

//注解的作用域
@Target({ElementType.METHOD,ElementType.TYPE})
注解的生命周期
@Retention(RetentionPolicy.RUNTIME)
注解是否允许被继承
@Inherited
是否生成注解Javadoc文档子注解
@Documented
public @interface Description {
    String desc();
    String auth()default"rongrong";
}

接口

public  Person {

    String name();
    int age();
    void say();
}

实现类

 com.description.demo;

@Description(desc = "class annotation")
class Child implements Person {

    @Override
    @Description(desc = "method annotation")
    public String name() {
         Todo Auto-generated method stub
        return null;
    }

    @Override
     age() {
        return 0 say() {
         Todo Auto-generated method stub

    }

}

测试类:

/**
 * 
 */
 java.lang.annotation.Annotation;
 java.lang.reflect.Method;

 org.junit.Test;


 * @author Administrator
 *    解析注解
 class MainTest {
    
    @SuppressWarnings("unchecked")
    @Test
     run(){
        try {
            通过反射加载实体类
            Class c = Class.forName("com.description.demo.Child");
            找到类上面的注解
            boolean isExist= c.isAnnotationPresent(Description.);
            if (isExist) {
                拿到实体类,一定强转为注解类型,反则找不到
                Description d = (Description) c.getAnnotation(Description.);
                System.out.println(d.desc());
            }
            找到方法上的注解
            Method[] method = c.getmethods();
            for (Method ms : method) {
                isExist = ms.isAnnotationPresent(Description.);
                 (isExist) {
                    Description d = ms.getAnnotation(Description.);
                    System.out.println(d.desc());
                }
                另一种方法
                Annotation[] annotation = ms.getAnnotations();
                 (Annotation as : annotation) {
                    if (as instanceof Description) {
                        String desc = ((Description) as).desc();
                        System.out.println(desc);
                    }
                }
            }
        } catch (ClassNotFoundException e) {
             Todo Auto-generated catch block
            e.printstacktrace();
        }
    }

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐