如何判断局部变量是否是“final”?来自 Java 字节码? (与BCEL相关)
Java 字节码中诸如局部变量是否为“final”之类的信息存储在哪里?我知道对于字段(全局变量)和方法,这些可以在访问标志位中找到,但似乎无法在局部变量表中找到等效项。
我对这个问题很感兴趣,因为我正在使用 BCEL 来检查局部变量是否是最终变量,并且在 AccessFlags 类中找到了等效的字段、方法和类。
提前致谢。
Where is information such as if a local variable is "final" stored in Java bytecode? I know that for fields (global variables) and methods these are found in the access flag bits, but cannot seem to find the equivalent in the local variable table.
I am interested in this question as I am using BCEL to check if a local variable is final, and have found the equivalent for fields, methods and classes in the class AccessFlags.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
局部变量的最终性由编译器检查,并且不会进入字节码。运行时不需要此信息,因此不会存储在字节码中。
JVM 以相同的方式处理最终和非最终局部变量。
The finality of local variables is checked by the compiler and doesn't make it to the bytecode. This information isn't required at runtime, and hence isn't stored in the bytecode.
The JVM treats final and non-final local variables in the same way.
简短的回答 - 你不能。局部变量的“最终”访问标志仅告诉编译器不能重新分配变量值。请参阅JVM 规范的第 4.7.13 节。
Short answer - you can't. The 'final' access flag for local variables only tells compiler that variable value can't be reassigned. See section 4.7.13 of the JVM specification.
我不相信你可以确定局部变量的最终性;这可以通过编写一个带有和不带有
final
关键字的小方法并比较字节码来证明。I don't believe you can determine the
final
ity of a local variable; this can be proven by writing a small method with and without thefinal
keyword and comparing the bytecode.