1
我現在正在試圖找出這個錯誤我們,因爲幾個小時,我硬是不知道可能會導致它。我正在嘗試將代碼注入類文件。 奇怪的是,注射工作正常ClassWriter.COMPUTE_MAXS,但拋出,如果我用ClassWriter.COMPUTE_FRAMES一個ArrayIndexOutOfBoundsException。我需要使用COMPUTE_FRAMES才能運行編輯過的類。我使用ASM 5.2,只出這麼遠的是COMPUTE_FRAMES具有爲2的值(也許這可以幫助你以某種方式)ASM 5.2 ClassReader.accept拋出ArrayIndexOutOfBoundsException異常: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
}
}
ModifierClassWriter:
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)
你應該使用他們的編譯罐子ASM的SVN isntead源。它會讓你看到完整的堆棧軌跡,沒有像「a」或「b」這樣的名字。對於問題本身,幀生成往往在修改您在無效的字節碼結果失敗。 '// InjectCodeHere,去掉它,因爲它很可能不導致的error'這實際上可能是什麼原因造成的。 –