我正在尝试生成一个简单的条件跳转指令。这是班级:
public static Class<?> getKlass2(){
String className = "TestClass";
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classWriter.visit(V1_8, ACC_PUBLIC, className, null, getInternalName(Object.class), null);
MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC + ACC_STATIC, "m", "()Z",null, null);
Label trueLable = new Label();
Label afterFalseLable = new Label();
mv.visitFieldInsn(GETSTATIC, getInternalName(Boolean.class), "TRUE", "Ljava/lang/Boolean;");
mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(Boolean.class), "booleanValue", "()Z", false);
mv.visitJumpInsn(IFEQ, trueLable);
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(GOTO, afterFalseLable);
mv.visitLabel(trueLable);
mv.visitInsn(ICONST_0);
mv.visitFrame(F_APPEND, 0, null, 0, null);
mv.visitLabel(afterFalseLable);
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
//converting classWriter.toByteArray() to Class<?> instance
}
加载课程时出现以下错误:
Expecting a stackmap frame at branch target 13
Exception Details:
Location:
TestClass.m()Z @6: ifeq
Reason:
Expected stackmap frame at this location.
Bytecode:
0x0000000: b200 0cb6 000f 9900 0704 a700 0403 ac
Stackmap Table:
same_frame_extended(@14)
但是我的课程代码似乎没问题:
public class TestClass {
public static boolean m();
Code:
0: getstatic #12 // Field java/lang/Boolean.TRUE:Ljava/lang/Boolean;
3: invokevirtual #15 // Method java/lang/Boolean.booleanValue:()Z
6: ifeq 13
9: iconst_1
10: goto 14
13: iconst_0
14: ireturn
}
所以我试着手工添加一个框架:
mv.visitFrame(F_APPEND, 0, new Object[]{ }, 0, new Object[]{ trueLabel });
但它也失败了同样的例外。他们是否希望重建具有相同操作数堆栈的帧? 但这似乎没有意义,因为我无法从Java代码直接访问opearand堆栈。
我做错了什么?
答案 0 :(得分:1)
您可以简单地将COMPUTE_FRAMES
指定给ClassWriter
的构造函数,让ASM为您计算两个,最大堆栈和本地以及堆栈映射表框架条目。正如文档所述,“ ... computeFrames意味着computeMaxs ”。
但是,我总是建议尝试理解堆栈映射,因为从头开始的计算不仅昂贵,而且存在基本限制(如this answer中详述)。既然你已经知道堆栈框架应该是什么样子,那么编码这些知识应该不会太难。因为这也意味着知道局部变量和操作数堆栈条目的最大数量,所以手动指定它们也是一致的。
然而,你尝试过的解决方案却很遥远:
mv.visitFrame(F_APPEND, 0, new Object[]{ }, 0, new Object[]{ trueLabel });
F_APPEND
表示已添加新的变量,这与您添加堆栈条目的明显意图不符。此外,将标签指定为堆栈条目仅在您引用NEW
指令的位置时才有效,以表示未初始化的对象。但在这里,你推了一个INTEGER
值。
正确的代码如下:
String className = "TestClass";
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC, className, null, getInternalName(Object.class), null);
MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()Z",null, null);
Label trueLabel = new Label();
Label afterFalseLabel = new Label();
mv.visitFieldInsn(GETSTATIC, getInternalName(Boolean.class),"TRUE","Ljava/lang/Boolean;");
mv.visitMethodInsn(INVOKEVIRTUAL,getInternalName(Boolean.class),"booleanValue","()Z",false);
mv.visitJumpInsn(IFEQ, trueLabel);
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(GOTO, afterFalseLabel);
// target of IFEQ, the frame matches the initial frame (no variables, no stack entries)
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitLabel(trueLabel);
mv.visitInsn(ICONST_0);
// merge point of the two branches, now having an INTEGER on the stack
mv.visitFrame(F_SAME1, 0, null, 1, new Object[]{ INTEGER });
mv.visitLabel(afterFalseLabel);
mv.visitInsn(IRETURN);
// no variable at all, at most one stack entry (the integer)
mv.visitMaxs(1, 0);
mv.visitEnd();
//converting classWriter.toByteArray() to Class<?> instance
请注意,对于特殊的压缩帧类型,大多数参数都是隐含的,对于F_SAME
,所有其他参数都不相关,对于F_SAME1
,只有最后一个参数中指定的新堆栈条目类型的事项。
但是您不需要处理不同类型的压缩帧。如果有疑问,您可以始终使用假定堆栈帧布局的完整描述来指定F_NEW
。唯一的区别是(略微)更大的类文件。对于动态类生成,这可能完全不相关,即使对于在部署之前添加到应用程序的生成类,差异也可以忽略不计:
String className = "TestClass";
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC, className, null, getInternalName(Object.class), null);
MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()Z",null, null);
Label trueLabel = new Label();
Label afterFalseLabel = new Label();
mv.visitFieldInsn(GETSTATIC, getInternalName(Boolean.class),"TRUE","Ljava/lang/Boolean;");
mv.visitMethodInsn(INVOKEVIRTUAL,getInternalName(Boolean.class),"booleanValue","()Z",false);
mv.visitJumpInsn(IFEQ, trueLabel);
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(GOTO, afterFalseLabel);
// target of IFEQ, the frame state is "no variables, no stack entries"
mv.visitFrame(F_NEW, 0, null, 0, null);
mv.visitLabel(trueLabel);
mv.visitInsn(ICONST_0);
// merge point of the two branches, frame state is "no variables, one INTEGER on the stack"
mv.visitFrame(F_NEW, 0, null, 1, new Object[]{ INTEGER });
mv.visitLabel(afterFalseLabel);
mv.visitInsn(IRETURN);
// no variable at all, at most one stack entry (the integer)
mv.visitMaxs(1, 0);
mv.visitEnd();
顺便说一下,我发现将抽象名称生成(如getInternalName( Boolean.class)
)与像"Ljava/lang/Boolean;"
这样的硬编码签名相结合有点奇怪。两者都是有效的,但最好始终以任何一种方式做出决定。