使用javassist的检测方法

时间:2012-06-06 22:58:07

标签: java javassist

我正在尝试通过在每个检测方法中添加几行代码将方法的名称写入文件。我正在使用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的值,因此打开文件并写入文件。

那么,请问您如何检索行的值?

由于

1 个答案:

答案 0 :(得分:1)

您可以使用static String并保留testName的最后一个值。对于单个线程,您将获得相同的结果。对于多个线程,这是一个不同的故事,但我想你正在尝试为你的测试获得堆栈跟踪并且你按顺序运行它,所以你应该是安全的。 E.g:

假设你有一个班级

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(); }");
    }
}