通过Class进行反射取注解值

//简单的列举反射取注解/注解Value 常用方法

//通过  class路径、分类(类、方法、字段)

//packageFile:类全路径

    public static void test(String packageFile) {

        try {

            Class c = Class.forName(packageFile);//取到指定的Class类

            c.getMethods();//取到该Class类的全部方法

            c.isAnnotationPresent(Description.class);//查看该类上是符包含指定注解

            Method[] methods = c.getMethods();

            for (Method method : methods) {

                Boolean b = method.isAnnotationPresent(Description.class);//查看该方法上是符包含指定注解

                Annotation[] annotations = method.getAnnotations();//获取该类上的全部注解

                for(Annotation annotation:annotations){

                    Annotation annotation1 = (Description)annotation;

                    ((Description) annotation1).value();//查看注解的Value值

                }

            }

            Field[] fields = c.getFields();

            for (Field field : fields) {

                //与方法中代码一致

            }

        } catch (Exception e) {

            System.out.println(e.getMessage());

            throw new RuntimeException(e);

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值