1
给出一个简单的try-catch-终于方法:LocalVariableTable中缺少什么?
public void t() {
try {
f1();
}catch(Exception e) {
f2();
}finally {
f3();
}
}
随着 “javac的-g”,它编译成:
public void t();
descriptor:()V
flags: ACC_PUBLIC
Code:
stack=1, locals=3, args_size=1
0: aload_0
1: invokespecial #15 // Method f1:()V
4: goto 26
7: astore_1
8: aload_0
9: invokespecial #18 // Method f2:()V
12: aload_0
13: invokespecial #21 // Method f3:()V
16: goto 30
19: astore_2
20: aload_0
21: invokespecial #21 // Method f3:()V
24: aload_2
25: athrow
26: aload_0
27: invokespecial #21 // Method f3:()V
30: return
Exception table:
from to target type
0 4 7 Class java/lang/Exception
0 12 19 any
LineNumberTable:
line 7: 0
line 8: 4
line 9: 8
line 11: 12
line 10: 19
line 11: 20
line 12: 24
line 11: 26
line 13: 30
LocalVariableTable:
Start Length Slot Name Signature
0 31 0 this Lsample/Sample;
8 4 1 e Ljava/lang/Exception;
StackMapTable: number_of_entries = 4
frame_type = 71 /* same_locals_1_stack_item */
stack = [ class java/lang/Exception ]
frame_type = 75 /* same_locals_1_stack_item */
stack = [ class java/lang/Throwable ]
frame_type = 6 /* same */
frame_type = 3 /* same */
通知当地人= 3,但也有只有两个项目LocalVariableTable。字节码行19(astore_2)和24(aload_2)表示第三个局部变量确实存在。
问题是:什么是第三个局部变量,以及为什么它在LocalVariableTable中被遗漏?
有趣的设计。如果在调用'f3()'之前,变量仅留在操作数堆栈上,它是否也能工作? (这可能会增加堆栈的最大深度?) – dejvuth
在这种情况下,第三个局部变量应该是Throwable类型吗?但为什么它不会出现在LocalVariableTable中? – ziyi
在这种情况下,它可能已经留在操作数堆栈中,但Javac的代码根据各种原因试图尽可能避免这种情况。如果我不得不猜测它为什么不出现在表中,那是因为它不对应于任何源级别变量。 LocalVariableTable仅用于调试。 – Antimony