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