java instrument_java Instrument修改字节码实现aop功能

本文介绍了一个Java实现的ClassFileTransformer,它监控特定类中的方法执行,并在前后添加性能计时代码,用于测量每个方法的执行时间。重点在于如何通过ClassPool获取字节码、替换方法并重写其行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MyTransformer implementsClassFileTransformer {final static String prefix = "\nlong startTime = System.currentTimeMillis();\n";final static String postfix = "\nlong endTime = System.currentTimeMillis();\n";//被处理的方法列表

final static Map> methodMap = new HashMap>();publicMyTransformer() {

add("com.hwtest.demo.MyProgram.sayHello");

add("com.hwtest.demo.MyProgram.sayHello2");

}private voidadd(String methodString) {

String className= methodString.substring(0, methodString.lastIndexOf("."));

String methodName= methodString.substring(methodString.lastIndexOf(".") + 1);

List list =methodMap.get(className);if (list == null) {

list= new ArrayList();

methodMap.put(className, list);

}

list.add(methodName);

}

@Overridepublic byte[] transform(ClassLoader loader, String className, Class>classBeingRedefined,

ProtectionDomain protectionDomain,byte[] classfileBuffer) throwsIllegalClassFormatException {

className= className.replace("/", ".");if (methodMap.containsKey(className)) {//判断加载的class的包路径是不是需要监控的类

CtClass ctclass = null;try{

ctclass= ClassPool.getDefault().get(className);//使用全称,用于取得字节码类

for(String methodName : methodMap.get(className)) {

String outputStr= "\nSystem.out.println(\"this method " +methodName+ " cost:\" +(endTime - startTime) +\"ms.\");";

CtMethod ctmethod= ctclass.getDeclaredMethod(methodName);//得到这方法实例

String newMethodName = methodName + "$old";//新定义一个方法叫做比如sayHello$old

ctmethod.setName(newMethodName);//将原来的方法名字修改//创建新的方法,复制原来的方法,名字为原来的名字

CtMethod newMethod = CtNewMethod.copy(ctmethod, methodName, ctclass, null);//构建新的方法体

StringBuilder bodyStr = newStringBuilder();

bodyStr.append("{");

bodyStr.append(prefix);

bodyStr.append(newMethodName+ "($$);\n");//调用原有代码,类似于method();($$)表示所有的参数

bodyStr.append(postfix);

bodyStr.append(outputStr);

bodyStr.append("}");

newMethod.setBody(bodyStr.toString());//替换新方法

ctclass.addMethod(newMethod);//增加新方法

}returnctclass.toBytecode();

}catch(Exception e) {

System.out.println(e.getMessage());

e.printStackTrace();

}

}return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值