编程语言
首页 > 编程语言> > java – .class-files的名称和位置在哪里定义?

java – .class-files的名称和位置在哪里定义?

作者:互联网

最受欢迎的this question答案建议在特定文件夹中搜索具有特定名称的类文件,作为反映匿名内部类的解决方法.在哪个文档(如果有)中指定了Java类文件的名称和位置?

VM-spec包含类文件格式的详细规范,但似乎没有规定如何命名它们以及应该存储它们的位置.同样,language-spec似乎没有触及这个主题.

解决方法:

Class class (getSimpleName method)的源代码:

 1137           // According to JLS3 "Binary Compatibility" (13.1) the binary
 1138           // name of non-package classes (not top level) is the binary
 1139           // name of the immediately enclosing class followed by a '$' followed by:
 1140           // (for nested and inner classes): the simple name.
 1141           // (for local classes): 1 or more digits followed by the simple name.
 1142           // (for anonymous classes): 1 or more digits.

上述文件:JLS3 “Binary Compatibility” (13.1)表明它是这样的(更精确但不简洁):

Furthermore, the resulting class file must have certain properties. A
number of these properties are specifically chosen to support source
code transformations that preserve binary compatibility. The required
properties are:

The class or interface must be named by its binary name, which must
meet the following constraints:

The binary name of a top level type (§7.6) is its canonical name
(§6.7).

The binary name of a member type (§8.5, §9.5) consists of the binary
name of its immediately enclosing type, followed by $, followed by the
simple name of the member.

The binary name of a local class (§14.3) consists of the binary name
of its immediately enclosing type, followed by $, followed by a
non-empty sequence of digits, followed by the simple name of the local
class.

The binary name of an anonymous class (§15.9.5) consists of the binary
name of its immediately enclosing type, followed by $, followed by a
non-empty sequence of digits.

The binary name of a type variable declared by a generic class or
interface (§8.1.2, §9.1.2) is the binary name of its immediately
enclosing type, followed by $, followed by the simple name of the type
variable.

The binary name of a type variable declared by a generic method
(§8.4.4) is the binary name of the type declaring the method, followed
by $, followed by the descriptor of the method as defined in The Java™
Virtual Machine Specification, Java SE 7 Edition, followed by $,
followed by the simple name of the type variable.

The binary name of a type variable declared by a generic constructor
(§8.8.4) is the binary name of the type declaring the constructor,
followed by $, followed by the descriptor of the constructor as
defined in The Java™ Virtual Machine Specification, Java SE 7 Edition,
followed by $, followed by the simple name of the type variable.

因此,可以说通常已知的命名方案是完全规范化的,我们可以依赖它(因为必须依赖所有类加载器来查找必要的类文件).

标签:class-file,java,specifications
来源: https://codeday.me/bug/20190901/1786334.html