classPool.get(className) 抛出 RuntimeException 找不到 class

classPool.get(className) throws RuntimeException cannot find class

我正在尝试使用 Javassist 编写一个简单的检测代理。

public class Agent implements ClassFileTransformer {

    protected Instrumentation instrumentation;
    protected ClassPool classPool;

    public Agent(Instrumentation instrumentation){

        this.instrumentation = instrumentation;
        this.classPool = ClassPool.getDefault();
        this.instrumentation.addTransformer(this);
    }

    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

        try {

            System.out.printf("%s.transform: %s\n", this.getClass().getCanonicalName(), className);

            classPool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer));

            CtClass ctClass = classPool.get(className);  /* <- throws error */
        }
        catch (Exception ex){
            System.out.println(ex);
            return null;
        }
    }
}

但我收到以下错误:

java.lang.RuntimeException: cannot find java/lang/invoke/MethodHandleImpl: java.lang.invoke.MethodHandleImpl found in java/lang/invoke/MethodHandleImpl.class

唯一不抛出此异常的 class 是我在 IDE (IntelliJ IDEA) 项目中的那个。

我尝试手动添加 class 路径中的路径,但这也无济于事。即

String[] cpEntries = System.getProperty("java.class.path").split(File.pathSeparator);
for (String cpEntry : classpathEntries)
    classPool.insertClassPath(cpEntry);

我错过了什么?谢谢!

问题是 className 的分隔符是 /,而 classPool.get(className) 需要 . 分隔符,因此解决方法是替换这些字符,即:

String classNameDotted = className.replace('/', '.');
CtClass ctClass = classPool.get(classNameDotted);