运行时注解解析

运行时 Annotation 指 @Retention 为 RUNTIME 的 Annotation,可手动调用下面常用 API 解析
method.getAnnotation(AnnotationName.class);
method.getAnnotations();
method.isAnnotationPresent(AnnotationName.class);
运行期注解解析案例:

定义一个自定义注解

package org.vincent.maven.annotation.anno;

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;

/**
 * 自定义注解
 * 
 * @author PengRong
 *
 */
@Documented
@Retention(RetentionPolicy.RUNTIME) // 这个自定义注解生命到运行期
@Target(ElementType.METHOD)
@Inherited
public @interface MethodInfo {

    String author() default "Vicent";

    String date() default "2017/03/03";

    int version() default 1;
}

在一个类中使用上面定义的注解:

package org.vincent.maven.annotation;

import org.vincent.maven.annotation.anno.MethodInfo;

/**
 * 使用自定义的注解
 * 
 * @ClassName: App
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author PengRong
 * @date 2017年3月5日 下午9:52:55
 *
 */
public class App {
    @MethodInfo(author = "Cindy", date = "day", version = 2)
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

    @Override
    public java.lang.String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

    @Deprecated
    private void xx() {
        // TODO Auto-generated method stub

    }

    @SuppressWarnings(value = { "deprecation", "unchecked" })
    private void y() {
        // TODO Auto-generated method stub

    }
}

测试:

package org.vincent.maven.annotation;

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

public class AnnotationParser {

    public static void main(String[] args) throws ClassNotFoundException {

        // 测试AnnotationTest类,得到被注解的类对象
        Class c = Class.forName("org.vincent.maven.annotation.App");
        // 获取该类所有声明的方法
        Method[] methods = c.getDeclaredMethods();
        // 声明注解集合
        Annotation[] annotations;
        // 遍历所有的方法得到各方法上面的注解信息
        for (Method method : methods) {
            // 获取每个方法上面所声明的所有注解信息
            annotations = method.getDeclaredAnnotations();
            // 再遍历所有的注解,打印其基本信息
            System.out.println("--------------------------------------------------");
            System.out.println("方法: " + method.getName());
            for (Annotation an : annotations) {
                System.out.println("方法名为:" + method.getName() + " 其上面的注解为:" + an.annotationType().getSimpleName());
                Method[] meths = an.annotationType().getDeclaredMethods();// 通过反射获取到注解类声明的所有成员变量
                // 遍历注解类的所有成员变量
                for (Method meth : meths) {
                    System.out.println("注解的变量名为:" + meth.getName());
                }
            }
            System.out.println("--------------------------------------------------");
        }

    }

}

输出结果:


--------------------------------------------------
方法: main
方法名为:main 其上面的注解为:MethodInfo
注解的变量名为:version
注解的变量名为:date
注解的变量名为:author
--------------------------------------------------
--------------------------------------------------
方法: toString
--------------------------------------------------
--------------------------------------------------
方法: xx
方法名为:xx 其上面的注解为:Deprecated
--------------------------------------------------
--------------------------------------------------
方法: y
--------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值