package sample;import javassist.*;/* A very simple sample program This program overwrites sample/Test.class (the class file of this class itself) for adding a method g(). If the method g() is not defined in class Test, then this program adds a copy of f() to the class Test with name g(). Otherwise, this program does not modify sample/Test.class at all. To see the modified class definition, execute: % javap sample.Test after running this program. */ public class Test { public int f(int i) { return i + 1; } public static void main(String[] args) throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("sample.Test"); try { cc.getDeclaredMethod("g"); System.out.println("g() is already defined in sample.Test."); } catch (NotFoundException e) { /* getDeclaredMethod() throws an exception if g() * is not defined in sample.Test. */ CtMethod fMethod = cc.getDeclaredMethod("f"); CtMethod gMethod = CtNewMethod.copy(fMethod, "g", cc, null); cc.addMethod(gMethod); cc.writeFile(); // update the class file System.out.println("g() was added."); } } }去下载一个javassist来,玩了一下这个Test,很不错的说
相对简单一点的做法,你可以用AOP;复杂一点的做法,当然自由发挥的余地更大,你可以用java汇编。
解答完毕!
-----
牛
/*
for(int mm=0;mm<9;mm++){
System.out.println(mm+"--okok");
}
*/
Label ml01 = new Label();
Label ml02 = new Label();
forInsn(ICONST_0);
forVarInsn(ISTORE, 4);
forLabel(ml01);
forVarInsn(ILOAD, 4);
forIntInsn(BIPUSH, 9);
forJumpInsn(IF_ICMPGE, ml02);
forFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
forTypeInsn(NEW, "java/lang/StringBuffer");
forInsn(DUP);
forMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "()V");
forVarInsn(ILOAD, 4);
forMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(I)Ljava/lang/StringBuffer;");
forLdcInsn("--okok");
forMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
forMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
forMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
forIincInsn(4, 1);
forJumpInsn(GOTO, ml01);
forLabel(ml02);
详情请google之,因为我也不会
呵呵
有兴趣的可以去看看hb的src嘛
A very simple sample program This program overwrites sample/Test.class (the class file of this
class itself) for adding a method g(). If the method g() is not
defined in class Test, then this program adds a copy of
f() to the class Test with name g(). Otherwise, this program does
not modify sample/Test.class at all. To see the modified class definition, execute: % javap sample.Test after running this program.
*/
public class Test {
public int f(int i) {
return i + 1;
} public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("sample.Test");
try {
cc.getDeclaredMethod("g");
System.out.println("g() is already defined in sample.Test.");
}
catch (NotFoundException e) {
/* getDeclaredMethod() throws an exception if g()
* is not defined in sample.Test.
*/
CtMethod fMethod = cc.getDeclaredMethod("f");
CtMethod gMethod = CtNewMethod.copy(fMethod, "g", cc, null);
cc.addMethod(gMethod);
cc.writeFile(); // update the class file
System.out.println("g() was added.");
}
}
}去下载一个javassist来,玩了一下这个Test,很不错的说
另外Apache有BECL, 也可以