gpt4 book ai didi

java - 使用 javassist 检测方法

转载 作者:太空宇宙 更新时间:2023-11-04 08:12:40 25 4
gpt4 key购买 nike

我试图通过在每个检测方法中添加几行代码来将方法的名称写入文件中。我正在使用 Javassist。

这是我的代码:

public static void addInstrumentation(CtClass ctClass, CtMethod ctMethod) throws NotFoundException, CannotCompileException {
if (ctClass.getName().startsWith("com.application.bookstore.entities.test")) {
String testName = ctClass.getName() + "." + ctMethod.getName();
String fileToWrite = "C:/profiler/root.txt";
ctMethod.insertBefore("{ FileWriter fw = new FileWriter(\"" + fileToWrite + "\", true); BufferedWriter out = new BufferedWriter(fw); out.write(\"" + testName + "\"); out.newLine(); out.close(); }");
}
else {
String methodName = ctClass.getName() + "." + ctMethod.getName() + "()";
String fileToRead = "C:/profiler/root.txt";
ctMethod.insertBefore("{ FileReader fr = new FileReader(\"" + fileToRead + "\"); BufferedReader in = new BufferedReader(fr); String line; while((line = in.readLine()) != null); in.close(); FileWriter fw = new FileWriter(line, true); BufferedWriter out = new BufferedWriter(fw); out.write(\"" + methodName + "\"); out.newLine(); out.close(); }");
}
}



/**
* Get current classname and for each methods inside it, call the addInstrumentation method.
*/
@Override
public void onLoad(ClassPool pool, String classname) throws NotFoundException, CannotCompileException {
if (classToImplement(classname))
{
pool.importPackage("java.io");
CtClass ctClass = pool.get(classname);


for (CtMethod ctMethod : ctClass.getDeclaredMethods()) {
if (!Modifier.isNative(ctMethod.getModifiers())) {
addInstrumentation(ctClass, ctMethod);
}
}
}
}

我的问题出在其他部分。我无法获取 line 的值,因此打开文件并写入其中。

那么,您知道如何检索 line 的值吗?

谢谢

最佳答案

您可以使用静态字符串并保留testName的最后一个值。对于单个线程,您将得到相同的结果。对于多个线程,这是一个不同的故事,但我想您正在尝试获取测试的堆栈跟踪并按顺序运行它,所以您应该是安全的。例如:

假设你有一个类(class)

com.application.bookstore.entities.test.ClassWithTheStaticField

其中有:

public static String lastTestMethod;

您可以像这样修改addInstrumentation:

public static void addInstrumentation(CtClass ctClass, CtMethod ctMethod) throws NotFoundException, CannotCompileException {
if (ctClass.getName().startsWith("com.application.bookstore.entities.test")) {
String testName = ctClass.getName() + "." + ctMethod.getName();
String fileToWrite = "C:/profiler/root.txt";
ctMethod.insertBefore("{ com.application.bookstore.entities.test.ClassWithTheStaticField.lastTestMethod = " + testName + "; FileWriter fw = new FileWriter(\"" + fileToWrite + "\", true); BufferedWriter out = new BufferedWriter(fw); out.write(\"" + testName + "\"); out.newLine(); out.close(); }");
}
else {
String methodName = ctClass.getName() + "." + ctMethod.getName() + "()";
String fileToRead = "C:/profiler/root.txt";
ctMethod.insertBefore("{ FileWriter fw = new FileWriter(com.application.bookstore.entities.test.ClassWithTheStaticField.lastTestMethod, true); BufferedWriter out = new BufferedWriter(fw); out.write(\"" + methodName + "\"); out.newLine(); out.close(); }");
}
}

关于java - 使用 javassist 检测方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10923475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com