gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:47:47 25 4
gpt4 key购买 nike

我现在正试图找出这个错误,因为几个小时以来我真的不知道是什么原因导致的。我正在尝试将代码注入(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com