技术中心

这里象征着我们的态度和能力

>java反射Annotation
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-28      浏览次数:5775
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Target(ElementType.CONSTRUCTOR)
// 用于构造方法
@Retention(RetentionPolicy.RUNTIME)
// 在运行时加载到Annotation到JVM中(这个一定不能丢掉,否则在getAnnotation的时候取不到!!!)
@interface Constructor_Annotation {
    String value() default "默认构造方法"; // 定义一个具有默认值的String型成员
}

@Target( { ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
// 用于字段,方法,参数
@Retention(RetentionPolicy.RUNTIME)
// 在运行时加载到Annotation到JVM中
@interface Field_Method_Parameter_Annotation {
    Class type() default void.class; // 定义一个具有默认值的Class型成员

    String describ(); // 定义一个没有默认值的String成员
}

public class AnnotationTest {

    @Field_Method_Parameter_Annotation(describ = "字段编号", type = int.class)
    // 注释字段
    int id;
    @Field_Method_Parameter_Annotation(describ = "字段姓名", type = String.class)
    // 注释字段
    String name;

    @Constructor_Annotation()
    // 采用默认构造方法
    public AnnotationTest() {

    }

    @Constructor_Annotation("立即初始化构造方法.")
    // 注释构造方法
    public AnnotationTest(
    // 注释构造方法参数
            @Field_Method_Parameter_Annotation(describ = "编号", type = int.class) int id, @Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
        this.id = id;
        this.name = name;
    }

    @Field_Method_Parameter_Annotation(describ = "获得编号", type = int.class)
    public int getId() {
        return id;
    }

    @Field_Method_Parameter_Annotation(describ = "设置编号")
    // 成员type,采用默认注释方法
    public void setId(
    // 注释参数
            @Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id) {
        this.id = id;
    }

    @Field_Method_Parameter_Annotation(describ = "获得姓名", type = String.class)
    public String getName() {
        return name;
    }

    @Field_Method_Parameter_Annotation(describ = "设置姓名")
    public void setName(@Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
        this.name = name;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 构造方法:
        Constructor[] declaredConstructor = AnnotationTest.class.getDeclaredConstructors(); // 获得所有的构造方法
        for (int i = 0; i < declaredConstructor.length; i++) {
            Constructor constructor = declaredConstructor[i]; // 遍历构造方法
            if (constructor.isAnnotationPresent(Constructor_Annotation.class)) // 查看是否指定类型的注释
            {
                Constructor_Annotation ca = (Constructor_Annotation) constructor.getAnnotation(Constructor_Annotation.class);
                System.out.println("ca.value()=: " + ca.value());
            }

            Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();// 获得参数注释
            for (int j = 0; j < parameterAnnotations.length; j++) {
                int length = parameterAnnotations[j].length;
                if (length == 0) // 如果为0,则表示没有为该参数添加注释
                {
                    System.out.println("没有为该参数添加注释");
                } else {
                    for (int k = 0; k < length; k++) {
                        // 获得参数注释
                        Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation) parameterAnnotations[j][k];
                        System.out.print(" " + pa.describ()); // 参数描述
                        System.out.println(" " + pa.type()); // 参数类型
                    }
                }
            }
            System.out.println("****************");
        }

        // 字段:
        System.out.println("********字段的Annotation*************");
        Field[] declaredFields = AnnotationTest.class.getDeclaredFields(); // 获得所有的字段
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            // 查看是否具有指定类型的注释:
            if (field.isAnnotationPresent(Field_Method_Parameter_Annotation.class)) {
                Field_Method_Parameter_Annotation fa = (Field_Method_Parameter_Annotation) field.getAnnotation(Field_Method_Parameter_Annotation.class);
                System.out.print(" " + fa.describ()); // 获得字段描述
                System.out.println(" " + fa.type()); // 获得字段类型
            }
        }

        // 方法
        System.out.println("********方法的Annotation*************");
        Method[] methods = AnnotationTest.class.getDeclaredMethods(); // 获得所有的方法
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            // 查看是否指定注释:
            if (method.isAnnotationPresent(Field_Method_Parameter_Annotation.class))

            {
                Field_Method_Parameter_Annotation ma = (Field_Method_Parameter_Annotation) method.getAnnotation(Field_Method_Parameter_Annotation.class);
                System.out.print(" " + ma.describ()); // 获得方法描述
                System.out.println(" " + ma.type()); // 获得方法类型
            }

            Annotation[][] parameterAnnotations = method.getParameterAnnotations(); // 获得所有参数
            for (int j = 0; j < parameterAnnotations.length; j++) {
                int length = parameterAnnotations[j].length;
                if (length == 0) {
                    System.out.println("没有添加Annotation参数");
                } else {
                    for (int k = 0; k < length; k++) {
                        // 获得指定的注释:
                        Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation) parameterAnnotations[j][k];
                        System.out.print(" " + pa.describ()); // 获得参数描述
                        System.out.println(" " + pa.type()); // 获得参数类型
                    }
                }
            }
            System.out.println("********************");

        }
    }

}

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730
Baidu
map