自定义注解

自定义注解:
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;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

接口

package com.description.demo;

public interface Person {

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

实现类

package com.description.demo;

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

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

    @Override
    public int age() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void say() {
        // TODO Auto-generated method stub

    }

}

测试类:

/**
 * 
 */
package com.description.demo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.junit.Test;

/**
 * @author Administrator
 *    解析注解
 */
public class MainTest {
    
    @SuppressWarnings("unchecked")
    @Test
    public void run(){
        try {
//            通过反射加载实体类
            Class c = Class.forName("com.description.demo.Child");
//            找到类上面的注解
            boolean isExist= c.isAnnotationPresent(Description.class);
            if (isExist) {
//                拿到实体类,一定强转为注解类型,反则找不到
                Description d = (Description) c.getAnnotation(Description.class);
                System.out.println(d.desc());
            }
//            找到方法上的注解
            Method[] method = c.getMethods();
            for (Method ms : method) {
                isExist = ms.isAnnotationPresent(Description.class);
                if (isExist) {
                    Description d = ms.getAnnotation(Description.class);
                    System.out.println(d.desc());
                }
//                另一种方法
                Annotation[] annotation = ms.getAnnotations();
                for (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();
        }
    }

}

 

posted @ 2017-02-07 22:38  久曲健  阅读(234)  评论(0编辑  收藏  举报