java - ASM 5.2 ClassReader.accept 抛出 ArrayIndexOutOfBoundsException :2

标签 java indexoutofboundsexception java-bytecode-asm

我现在正试图找出这个错误,因为几个小时以来我真的不知道是什么原因导致的。我正在尝试将代码注入(inject)到类文件中。 奇怪的是,注入(inject)对于 ClassWriter.COMPUTE_MAXS 工作正常,但如果我使用 ClassWriter.COMPUTE_FRAMES,则会抛出 ArrayIndexOutOfBoundsException。我需要使用 COMPUTE_FRAMES 才能运行编辑后的类。我正在使用 asm 5.2,到目前为止只发现 COMPUTE_FRAMES 的值为 2(也许这对您有帮助)

我的代码:

    InputStream in = new FileInputStream("Paht/To/Class.class");
    ClassReader classReader = new ClassReader(in);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES){
        @Override
        protected String getCommonSuperClass(final String type1, final String type2){
            return "java/lang/Object";
        }
    };

    ClassVisitor mcw = new ModifierClassWriter(Opcodes.ASM5, cw);
    classReader.accept(mcw, 0);

    File outputDir = new File("Path/To/Output/dir");
    outputDir.mkdirs();
    DataOutputStream dout = new DataOutputStream(new FileOutputStream(new File(outputDir, "NameOfFile.class")));
    dout.write(cw.toByteArray());
    dout.close();

修饰符方法编写器:

    public static class ModifierMethodWriter extends MethodVisitor{

    private String methodName;

    public ModifierMethodWriter(int api, MethodVisitor mv, String methodName) {
        super(api, mv);
        this.methodName = methodName;
    }
    @Override
    public void visitCode() {
        super.visitCode();
    //InjectCodeHere, removed it because it most likely doesnt cause the error
    }
}

修改器类编写器:

    public static class ModifierClassWriter extends ClassVisitor{
    private int api;
    public ModifierClassWriter(int api, ClassWriter cv) {
        super(api, cv);
        this.api = api;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        MethodVisitor mv =  super.visitMethod(access, name, desc, signature, exceptions);
        ModifierMethodWriter mvw = new ModifierMethodWriter(api, mv, name);
        return mvw;
    }
}

错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitMaxs(Unknown Source)
at org.objectweb.asm.ClassReader.a(Unknown Source)
at org.objectweb.asm.ClassReader.b(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at package.main.ClassMaker.main(ClassMaker.java:28)

最佳答案

感谢@display-name,我发现了错误。

super.visitMaxs(4, 2);

在“ModifierMethodWriter”中导致了错误,我实际上没有在此处显示代码。

关于java - ASM 5.2 ClassReader.accept 抛出 ArrayIndexOutOfBoundsException :2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43369312/

相关文章:

java - eclipse中的叉号是什么意思

java - JSF AJAX - 类没有属性

android - 使用 ArrayIndexOutOfBoundsException 将 TextView 文本设置为静态文本崩溃(很少)

java - 字符串索引越界异常?

java - JVM 语言互操作性

java - 如何获取 Facebook 中特定页面的所有帖子 ID

java - SQL查询错误导致异常

java - 金字塔最小和路径2D ArrayList IndexOutOfBounds

java - 有什么方法可以从字节码重新生成堆栈图?

java - ASM之前看一下maxStack指令吗?