insertBefore example

insertBefore examplepublicclassPoint{intx,y;voidmove(intdx,intdy){x+=dx;y+=dy;}}publicstaticvoidmain(String[]args)throwsException{ClassPoolpool=ClassPool.getDefault()…

 

public class Point {
    int x, y;
    void move(int dx, int dy) { x += dx; y += dy; }
}
public static void main(String[] args) throws Exception{
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get("com.kq.javassist.entity.Point");
        CtMethod m = cc.getDeclaredMethod("move");
        m.insertBefore("{ System.out.println($1); System.out.println($2); }");
        cc.writeFile("D:\\javassist\\");
}

 

生成class


package com.kq.javassist.entity;

import java.io.PrintStream;

public class Point
{
  int x;
  int y;
  
  void move(int dx, int dy)
  {
    System.out.println(dx);System.out.println(dy);this.x += dx;this.y += dy;
  }
}

$1 and $2 are replaced with dx and dy, respectively.

$1, $2, $3 … are updatable. If a new value is assigend to one of those variables, then the value of the parameter represented by that variable is also updated

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/38564.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注