我有这个简单的功能:
public int id() {
return 0;
}
我具有此测试功能:
void test() {
int a = id();
int b = id();
int c = id();
int d = id();
int e = id();
int f = id();
System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f);
}
我希望输出为1, 2, 3, 4, 5, 6
;
现在我在instrument
上调用CtMethod
,它工作正常。
call to id! on line: 57
call to id! on line: 58
call to id! on line: 59
call to id! on line: 60
call to id! on line: 61
call to id! on line: 62
但是最后,所有转换都没有任何效果。我不知道该怎么办,因为那里的信息很少。
这里是完整的代码:
package doeke.method_call_test;
import java.lang.instrument.Instrumentation;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.expr.ExprEditor;
import javassist.expr.MethodCall;
public class MethodCallTest {
static int id = 1;
public static void premain(String agentArgs, Instrumentation inst) {
try {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.getCtClass("doeke.method_call_test.MethodCallTest");
CtMethod[] methods = ctClass.getDeclaredMethods();
for (CtMethod cm : methods) {
cm.instrument(
new ExprEditor() {
public void edit(MethodCall m) throws CannotCompileException {
if (m.getMethodName().equals("id")) {
// m.replace("{ $_ = "+id+"; }");
m.replace("$_ = 1; System.out.println(\"hello?\");");
System.out.println("call to id! on line: "+m.getLineNumber());
id++;
}
}
}
);
}
inst.retransformClasses(MethodCallTest.class);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MethodCallTest mct = new MethodCallTest();
mct.test();
}
void test() {
int a = id();
int b = id();
int c = id();
int d = id();
int e = id();
int f = id();
System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f);
}
public int id() {
return 0;
}
}
[不确定您如何运行代码,因为通常您需要在命令行上使用javaagent启动JVM才能注入Instrumentation实例。在下面的示例中,我使用ByteBuddy在运行时获取Instrumentation实例。
您还必须创建一个ClassFileTransformer以返回修改后的类的字节码,您可以通过调用CtClass.toByteCode()
来获取该代码>
下面的代码显示此输出:
call to id! on line: 84 call to id! on line: 85 call to id! on line: 86 call to id! on line: 87 call to id! on line: 88 call to id! on line: 89 Transforming [doeke/method_call_test/MethodCallTest] hello? hello? hello? hello? hello? hello? 1 1 1 1 1 1
基本代码:
package doeke.method_call_test;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.expr.ExprEditor;
import javassist.expr.MethodCall;
import net.bytebuddy.agent.ByteBuddyAgent;
public class MethodCallTest {
static int id = 1;
public static void premain(String agentArgs, Instrumentation inst) {
try {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.getCtClass("doeke.method_call_test.MethodCallTest");
CtMethod[] methods = ctClass.getDeclaredMethods();
for (CtMethod cm : methods) {
cm.instrument(
new ExprEditor() {
public void edit(MethodCall m) throws CannotCompileException {
if (m.getMethodName().equals("id")) {
// m.replace("{ $_ = "+id+"; }");
m.replace("$_ = 1; System.out.println(\"hello?\");");
System.out.println("call to id! on line: "+m.getLineNumber());
id++;
}
}
}
);
}
final byte[] byteCode = ctClass.toBytecode();
final String rezName = MethodCallTest.class.getName().replace('.', '/');
final ClassFileTransformer transformer = new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if (rezName.equals(className)) {
System.out.println("Transforming [" + className + "]");
return byteCode;
}
return null;
}
};
inst.addTransformer(transformer, true);
try {
inst.retransformClasses(MethodCallTest.class);
} finally {
inst.removeTransformer(transformer);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MethodCallTest mct = new MethodCallTest();
Instrumentation inst = ByteBuddyAgent.install();
MethodCallTest.premain("", inst);
mct.test();
}
void test() {
int a = id();
int b = id();
int c = id();
int d = id();
int e = id();
int f = id();
System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f);
}
public int id() {
return 0;
}
}