2017-04-12 11 views
1

이제이 오류를 알아 내려고 노력하고 있습니다. 몇 시간 후부터 문자 그대로 생각할 수 없었습니다. 클래스 파일에 코드를 주입하려고합니다. 이상한 일은 ClassWriter.COMPUTE_MAXS에서 주입이 잘 작동하지만 ClassWriter.COMPUTE_FRAMES를 사용하면 ArrayIndexOutOfBoundsException을 던진다는 것입니다. 편집 된 클래스를 실행하려면 COMPUTE_FRAMES를 사용해야합니다. 나는 ASM 5.2을 사용하여 단지 밖으로 지금까지 COMPUTE_FRAMES 2의 값을 (아마도이 ​​어떻게 든 당신을 도와줍니다)ASM 5.2 ClassReader.accept throws 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) 
+0

컴파일 된 항아리를 사용하여 ASM의 SVN isntead에서 소스를 가져와야합니다. 'a' 나'b'와 같은 이름이없는 전체 스택 추적을 볼 수 있습니다. 이슈 자체에 관해서는 잘못된 바이트 코드로 수정을하면 프레임 생성이 실패하는 경향이 있습니다. '// InjectCodeHere, 오류를 일으킬 가능성이 가장 적기 때문에 제거했습니다. '이것은 실제로 그 원인이 될 가능성이 큽니다. –

답변

1

덕분에 @ display-name 오류가 발견되었습니다.

super.visitMaxs(4, 2); 

"ModifierMethodWriter"에서 오류가 발생했습니다. 실제로 여기에 표시되지 않은 코드입니다.