Got “UnsupportedOperationException” when try to retransformClasses

谁说我不能喝 提交于 2019-12-02 16:13:35

问题


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);
    }

来源:https://stackoverflow.com/questions/18657095/got-unsupportedoperationexception-when-try-to-retransformclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!