0

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);
    }
Nick Dong
  • 3,638
  • 8
  • 47
  • 84
  • 1
    Did you check what `isRetransformClassesSupported()` returns? – Holger Sep 06 '13 at 12:18
  • yes, `isRedefineClassesSupported()`, `isRetransformClassesSupported()`, and `isModifiableClass(clazz)` all return ture. – Nick Dong Sep 07 '13 at 06:03
  • Is there something wrong with my ASM code? I wanna add a line in a method. It should be allowed to do that. can anyone give me any advice? @Eugene Kuleshov – Nick Dong Sep 09 '13 at 03:47
  • Well, I am a newbie of Java, and of course JVM, asm. And I made stupid mistake which I did add a field to target class in my ASM code that is not supported to retransform in oracle JDK 1.6 for now. So there was an exception of "UnsupportedOperationException". – Nick Dong Sep 09 '13 at 08:38
  • Actually, it is. I add a field, which is not allowed. – Nick Dong Sep 09 '13 at 10:56

0 Answers0