问题描述
在尝试对服务器应用程序启动进行故障排除时,我在旧版 java字节码中发现了以下结构。
我的IDE反编译了一些第三方库,我很好奇它如何有效
-在关键字用作字段名称之前从未见过
I found the following construction in legacy java bytecode while trying to troubleshoot server application startup. My IDE decompiled some third party libraries and I'm curious how this can be valid - never saw before keywords can be used as field names in bytecode.
public final class f implements UserContext{
private final String try;
private final UserInfo do;
// a lot of code here
public UserInfo getUserInfo(){
return this.do;
}
public String getViewName(){
return this.try;
}
}
UPDATE :正确的getter名称对于 UserInfo
字段为 getUserInfo
-很抱歉,每个人都对具有不同返回值的方法进行歧义命名,这是一个副本粘贴问题,因为代码位于没有直接访问该站点的远程计算机上。
UPDATE : the correct getter name for UserInfo
field is getUserInfo
-- sorry for confusing everyone with ambiguous naming for methods with different return values, it's a copy-paste issue as code is located on a remote machine w/o direct access to the site.
推荐答案
Java虚拟机规范(负责定义字节码的外观)放入。
The Java Virtual Machine Specification (which is responsible for defining what the bytecode looks like) puts no constraints related to keywords on the names of things.
这些约束仅存在上的oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9 rel = nofollow noreferrer>。您使用的字节码不会反编译为有效的Java,因为它使用的名称在Java语言中无效,而 对JVM有效。
Those constraints only exist on the Java Language level. The bytecode you used does not decompile into valid Java, because it uses names that are not valid in the Java language, but that are valid to the JVM.
类似的逻辑适用于两个 getViewName
方法,仅在返回类型上有所不同:这是允许的(并且有效完全符合JVM规范的字节码格式,但Java语言规范禁止这样做。
A similar logic applies to the two getViewName
methods that differ only in return type: This is allowed (and works perfectly fine) in bytecode by the JVM spec, but the Java Language specification forbids it.
此类字节码的最常见原因是Java代码被编译为字节码,然后。
The most common reason for bytecode like this is Java code that was compiled to bytecode and then obfuscated.
另一个可能的原因是该代码最初不是通过编译Java代码生成的,而是另一种针对JVM的语言。在这种情况下,这种情况不太可能发生,因为没有人会在应该保留视图名称的情况下命名变量 do
。
Another possible reason is that the code was not originally produced by compiling Java code, but another language that targets the JVM. This is rather unlikely in this case, since no one would name a variable do
when it's supposed to hold the view name.
这篇关于此代码如何使用保留的关键字作为字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!