使用javassist检测方法
我试图通过在每个插入指令的方法中添加几行代码,将方法的名称写入一个文件。我正在使用Javassist 这是我的密码:使用javassist检测方法,java,javassist,Java,Javassist,我试图通过在每个插入指令的方法中添加几行代码,将方法的名称写入一个文件。我正在使用Javassist 这是我的密码: public static void addInstrumentation(CtClass ctClass, CtMethod ctMethod) throws NotFoundException, CannotCompileException { if (ctClass.getName().startsWith("com.application.bookstore.en
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的值了吗
谢谢您可以使用
静态字符串
并保留testName的最后一个值
。对于单个线程,您将得到相同的结果。对于多线程来说,情况就不同了,但我想您是在尝试获取测试的堆栈跟踪,然后按顺序运行,所以应该是安全的。
例如:
假设你有一门课
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(); }");
}
}
您可以使用
静态字符串
,并保留testName的最后一个值
。对于单个线程,您将得到相同的结果。对于多线程来说,情况就不同了,但我想您是在尝试获取测试的堆栈跟踪,然后按顺序运行,所以应该是安全的。
例如:
假设你有一门课
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(); }");
}
}
谢谢,事实上,我的代码运行得很好。Juste行为null,因为nextLine()放在。。。只需要更正while()。谢谢好吧,我想你不喜欢每次都要解析整个文件才能得到最后一行的事实。如果遇到性能问题,我会记住你的代码。再次感谢!谢谢,事实上,我的代码运行得很好。Juste行为null,因为nextLine()放在。。。只需要更正while()。谢谢好吧,我想你不喜欢每次都要解析整个文件才能得到最后一行的事实。如果遇到性能问题,我会记住你的代码。再次感谢!