package agent; import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtBehavior; import javassist.CtClass; import java.io.*; public class Test { public static void main(String []f) throws IOException, CannotCompileException { ClassPool classPool = new ClassPool(true); InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("agent/Test.class"); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte [] bytes = new byte[1000]; int n = 0; while(-1 != (n=inputStream.read(bytes))) { byteArrayOutputStream.write(bytes, 0, n); } CtClass ctClass = classPool.makeClass(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); for(CtBehavior ctBehavior : ctClass.getDeclaredBehaviors()) { System.out.println("start handle medthod: " + ctBehavior.getLongName()); try { ctBehavior.insertBefore("System.out.println(\"b\");"); ctBehavior.insertAfter("System.out.println(\"a\");"); } catch (Exception e) { e.printStackTrace(); } } byte [] out = ctClass.toBytecode(); FileOutputStream fileOutputStream = new FileOutputStream("tmp.class"); fileOutputStream.write(out); fileOutputStream.close();
假如:
ClassPool classPool = new ClassPool();
start handle medthod: agent.Test()
javassist.CannotCompileException: [source error] no such class: System.out
at javassist.CtBehavior.insertBefore(CtBehavior.java:774)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at agent.Test.main(Test.java:28)
Caused by: compile error: no such class: System.out
at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:470)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:414)
at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:321)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:683)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
at javassist.compiler.Javac.compileStmnt(Javac.java:567)
at javassist.CtBehavior.insertBefore(CtBehavior.java:754)
... 2 more
start handle medthod: agent.Test.main(java.lang.String[])
javassist.CannotCompileException: cannot find java.lang.String[]
at javassist.CtBehavior.insertBefore(CtBehavior.java:771)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at agent.Test.main(Test.java:28)
Caused by: javassist.NotFoundException: java.lang.String[]
at javassist.ClassPool.get(ClassPool.java:445)
at javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
at javassist.bytecode.Descriptor.getParameterTypes(Descriptor.java:439)
at javassist.CtBehavior.getParameterTypes(CtBehavior.java:298)
at javassist.CtBehavior.insertBefore(CtBehavior.java:749)
... 2 more
start handle medthod: agent.Test.print(int)
javassist.CannotCompileException: [source error] no such class: System.out
at javassist.CtBehavior.insertBefore(CtBehavior.java:774)
at javassist.CtBehavior.insertBefore(CtBehavior.java:734)
at agent.Test.main(Test.java:28)
Caused by: compile error: no such class: System.out
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:402)
at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:321)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:683)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
at javassist.compiler.Javac.compileStmnt(Javac.java:567)
at javassist.CtBehavior.insertBefore(CtBehavior.java:754)
... 2 more