如何测试ClassFileTransformer / javaagent?

时间:2011-07-05 17:42:12

标签: java instrumentation javaagents

我使用ASM为javaagent实现了ClassFileTransformer。因为它有一些bug,我想为它编写一个JUnit测试用例。我该怎么做?

使用伪代码我想的是:

// Have a test class as subject
public static class Subject {
  public void doSomething(){...}
}
// Manually load and transform the subject
...?
// Normally execute some now transformed methods of the subject
new Subject().doSomething();
// Check the result of the call (i.e. whether the correct attached methods were called)
Assert.assertTrue(MyClassFileTransformer.wasCalled());

现在的问题是:如何手动加载和转换主题并使JVM / Classloader使用我的操作版本?或者我完全错过了什么?

1 个答案:

答案 0 :(得分:6)

我明白了。我们需要实现一个自己的ClassLoader,它与ClassFileTransformer的测试主题进行相同的转换(例如调用它)。当然,主题类可能尚未加载,因此可能没有直接使用它。所以我使用Java反射API来执行主题类的方法。

在另一个文件中:

public static class Subject {
    public void doSomething(){...}
}

在测试中:

private static class TransformingClassLoader extends ClassLoader {

    private final String className;

    public TransformingClassLoader(String className) {
        super();
        this.className = className;
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        if (name.equals(className)) {
            byte[] byteBuffer = instrumentByteCode(fullyQualifiedSubjectClass);
            return defineClass(className, byteBuffer, 0, byteBuffer.length);
        }
        return super.loadClass(name);
    }
}

@Test
public void testSubject(){
    ClassLoader classLoader = new TransformingClassLoader(fullyQualifiedSubjectClass);
    Class<?> subjectClass = classLoader.loadClass(fullyQualifiedSubjectClass);
    Constructor<?> constructor = subjectClass.getConstructor();
    Object subject = constructor.newInstance();
    Method doSomething = subjectClass.getMethod("doSomething");
    doSomething.invoke(subject);
    Assert.assertTrue(MyClassFileTransformer.wasCalled());
}