JDK1.6, modify class loaded in jvm dynamically. When I comment the code:classReader.accept(classAdapter, ClassReader.SKIP_DEBUG);
, the exception "UnsupportedOperationException"
disapear.
Actually, for testing my code, I didnt modify any field or method. But the program catch exception "UnsupportedOperationException"
, after calling retransformClasses()
. Anyone has similar exception? Can any give me some advices? thx
Codes are as follow:
public byte[] modifySleepMethod() throws Exception {
System.out.println("Call modifySleepMethod");
ClassReader classReader = new ClassReader(classfileBuffer);
System.out.println("new classreader");
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
System.out.println("new classwriter");
ClassAdapter classAdapter = new ModifyMethodClassAdapter(classWriter);
classReader.accept(classAdapter, ClassReader.SKIP_DEBUG);
byte[] classFile = null;
classFile = classWriter.toByteArray();
FileOutputStream fos;
try {
fos = new FileOutputStream("D:\\testfos\\b\\Example2.class");
System.out.println("ddddddmodifymethodtest");
fos.write(classFile);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return classFile;
}
}
|
protected void transform(Class<?> clazz, ClassLoader classLoader) {
DemoTransformer dt = new DemoTransformer(clazz.getName(), classLoader);
instrumentation.addTransformer(dt, true);
try {
instrumentation.retransformClasses(clazz);
} catch (Exception ex) {
throw new RuntimeException("Failed to transform [" + clazz.getName() + "]", ex);
}
isRetransformClassesSupported()
returns? – UnthinkingisRedefineClassesSupported()
,isRetransformClassesSupported()
, andisModifiableClass(clazz)
all return ture. – Bedard