Got "UnsupportedOperationException" when try to retransformClasses
Asked Answered
B

0

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);
    }
Bedard answered 6/9, 2013 at 11:46 Comment(5)
Did you check what isRetransformClassesSupported() returns?Unthinking
yes, isRedefineClassesSupported(), isRetransformClassesSupported(), and isModifiableClass(clazz) all return ture.Bedard
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 KuleshovBedard
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".Bedard
Actually, it is. I add a field, which is not allowed.Bedard

© 2022 - 2024 — McMap. All rights reserved.