Java 字节码变更框架实现 JVM 热加载

导入

Instrument介绍:https://www.ibm.com/developerworks/cn/java/j-lo-jse61/index.html

https://m.aliyun.com/yunqi/articles/60539

Javassist

http://erhu.party/2016/11/17/javassist-turorial-1/

Java Agent 字节码热加载

https://www.tandi.wiki/%E5%9F%BA%E4%BA%8EJava%E6%8E%A2%E9%92%88%E4%B8%8E%E5%AD%97%E8%8A%82%E7%A0%81%E6%A1%86%E6%9E%B6%E6%9D%A5%E5%8A%A8%E6%80%81%E4%BF%AE%E6%94%B9%E4%BB%A3%E7%A0%81%E8%BF%90%E8%A1%8C%E6%97%B6%E8%A1%8C%E4%B8%BA/

添加注解

public void addAnnotation(CtClass ctClass){
        ConstPool constPool = ctClass.getClassFile().getConstPool();
        try {
                Object[] annotations = ctClass.getAnnotations();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            AnnotationsAttribute attribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
            Annotation annotation = new Annotation("org.testng.annotations.Listeners", constPool);
            annotation.addMemberValue(0, new ClassMemberValue("TestNgHook.class", constPool));
            attribute.addAnnotation(annotation);
            ctClass.getClassFile().addAttribute(attribute);

使用原有类和方法中字段

使用类中定义的属性(字段)


0是代表this也就是对象自己,所以直接使用0是代表this也就是对象自己,所以直接使用0.字段 进行赋值或者使用即可。

例如字段名字叫 status 就可以写成 $0.status=1;

使用方法中定义属性

Javassist也提供了一些特殊的变量来代表方法参 数:1,1,2,$args…要注意的是,插入的source文本中不能引用方法本地变量的声明,但是可以允许声明一个新的方法本地变量,除非在程序 编译时加入-g选项。

0代表的是this,0代表的是this,1代表方法参数的第一个参数、2代表方法参数的第二个参数,以此类推,2代表方法参数的第二个参数,以此类推,N代表是方法参数的第N个。

使用字段遇到问题

javassist [source error] bad filed access

The insertBefore, insertAfter and other such methods cannot access any local variable but can access method parameter of the method by their names. no such field error occurs if attempt is made to access local variable.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kingairy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值