0

JDK1.6,动态修改jvm中加载的类。当我评论 code:classReader.accept(classAdapter, ClassReader.SKIP_DEBUG);时,异常"UnsupportedOperationException"消失了。实际上,为了测试我的代码,我没有修改任何字段或方法。但程序"UnsupportedOperationException"在调用后捕获异常retransformClasses()。有人有类似的例外吗?任何人都可以给我一些建议吗?thx 代码如下:

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

0 回答 0