我正在尝试使用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;
}
}
}
但我发现以下错误:
我试图手动从类路径添加路径,但也没有帮助。即
String[] cpEntries = System.getProperty("java.class.path").split(File.pathSeparator);
for (String cpEntry : classpathEntries)
classPool.insertClassPath(cpEntry);
我错过了什么?谢谢