2012-05-18 49 views
0

在帖子的底部是測試用例。它給出了以下錯誤。但我已經設置new ClassWriter(ClassWriter.COMPUTE_MAXS)所以不應該自動計算最大堆棧並正確設置它?ASM COMPUTE_MAXS不適用於基本測試用例嗎?

Exception in thread "main" java.lang.RuntimeException: Error at instruction 2: Insufficient maximum stack size. testMethod()Ljava/lang/Object; 
00000 : : L0 
00001 : :  LINENUMBER 22 L0 
00002 : :  ACONST_NULL 
00003 ? :  ARETURN 

測試用例:

public static void main(String[] args) { 
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); 
    CheckClassAdapter cv = new CheckClassAdapter(cw); 
    cv.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "path/Cls", null, "java/lang/Object", null); 
    { 
     MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 
     mv.visitCode(); 
     Label l0 = new Label(); 
     mv.visitLabel(l0); 
     mv.visitVarInsn(ALOAD, 0); 
     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); 
     mv.visitInsn(RETURN); 
     Label l1 = new Label(); 
     mv.visitLabel(l1); 
     mv.visitLocalVariable("this", "L" + "path/Cls" + ";", null, l0, l1, 0); 
     mv.visitMaxs(1, 1); 
     mv.visitEnd(); 
    } 
    { 
     MethodVisitor mv = cv 
       .visitMethod(ACC_PUBLIC + ACC_STATIC, "testMethod", "()Ljava/lang/Object;", null, null); 
     mv.visitCode(); 
     Label l0 = new Label(); 
     mv.visitLabel(l0); 
     mv.visitLineNumber(22, l0); 
     mv.visitInsn(ACONST_NULL); 
     mv.visitInsn(ARETURN); 
     mv.visitMaxs(0, 0); // Same error even if this is commented out 
     mv.visitEnd(); 
    } 

    byte[] byteArray = cw.toByteArray(); 
} 

回答

4

的問題是不是在ASM,但在您的測試。基本上,CheckClassAdapter在計算最大堆棧和var值之前會看到字節碼。

您可以這樣更改代碼的東西:

ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); 
    cw.visit... 

    byte[] byteArray = cw.toByteArray(); 
    ClassReader cr = new ClassReader(byteArray); 
    cr.accept(new CheckClassAdapter(new ClassWriter(0)), 0); 
0

可以配置CheckClassAdapter不檢查堆棧大小:

CheckClassAdapter cv = new CheckClassAdapter(cw, false); 
相關問題