注解 解析注解

package cn.itcast.Junit;


import java.lang.reflect.Method;

@Myanno1(name = "cn.itcast.Junit.add",Method = "method")//传入指定类名路径和方法名称
public class add1 {
    public static void main(String[] args) throws Exception{
        Class<add1> add1Class = add1.class;//创建对应的字节码对象  为了获取该注解的里面的name和Method
        Myanno1 annotation = add1Class.getAnnotation(Myanno1.class);//传入指定注解的Class文件返回此注解类型的对象
        String method = annotation.Method();//获取对象里面的方法
        String name = annotation.name();//获取对象里面的包路径
        System.out.println(method);//打印输出
        System.out.println(name);//打印输出

        Class aClass = Class.forName(name);//根据此字符串名称的路径返回此类的Class对象
        Method method1 = aClass.getMethod(method);//根据字节码对象调用指定名称的方法
        Object o = aClass.newInstance();//创建字节码对应类的对象
        Object invoke = method1.invoke(o);//调用实现方法 打印输出此类的方法||对带有指定参数的指定对象调用由此 Method 对象表示的底层方法

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值