JVM的class文件结构
2014-03-25 21:27
387 查看
请参考《深入java虚拟机第二版》和《java虚拟机规范》。
java虚拟机规范链接:http://docs.oracle.com/javase/specs/jvms/se7/html/
关于class文件结构的介绍:http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.1
The items in the
magic
The
minor_version, major_version
The values of the
Together, a major and a minor version number determine the version of the
format as M.m. Thus,
A Java Virtual Machine implementation can support a
release level of the Java SE platform to which a Java Virtual Machine implementation conforms is responsible for determining the range.
Oracle's Java Virtual Machine implementation in JDK release 1.0.2 supports
format versions in the range 45.0 through 45.65535 inclusive. For k ≥ 2, JDK release 1.k supports
constant_pool_count
The value of the
is considered valid if it is greater than zero and less than
constant_pool[]
The
representing various string constants, class and interface names, field names, and other constants that are referred to within the
entry is indicated by its first "tag" byte.
The
access_flags
The value of the
of each flag, when set, is as shown in Table 4.1.
Table 4.1. Class access and property modifiers
A class may be marked with the
The
An interface is distinguished by its
defines a class, not an interface.
If the
(JLS §9.1.1.1). Such a
An annotation type must have its
must be set as well. If the
except the
The
(§invokespecial) if it appears in this class. Compilers
to the instruction set of the Java Virtual Machine should set the
The
which the flag now representing
All bits of the
reserved for future use. They should be set to zero in generated
this_class
The value of the
at that index must be a
representing the class or interface defined by this
super_class
For a class, the value of the
If the value of the
representing the direct superclass of the class defined by this
of its
If the value of the
the only class or interface without a direct superclass.
For an interface, the value of the
at that index must be a
interfaces_count
The value of the
interfaces[]
Each value in the
at each value of
(§4.4.1) representing an interface that is a direct superinterface of this
class or interface type, in the left-to-right order given in the source for the type.
fields_count
The value of the
The
instance variables, declared by this class or interface type.
fields[]
Each value in the
structure giving a complete description of a field in this class or interface. The
from superclasses or superinterfaces.
methods_count
The value of the
methods[]
Each value in the
structure giving a complete description of a method in this class or interface. If neither of the
the Java Virtual Machine instructions implementing the method are also supplied.
The
methods (§2.9), and any class or interface initialization method (§2.9).
The
attributes_count
The value of the
in the
attributes[]
Each value of the
structure.
The attributes defined by this specification as appearing in the
and
If a Java Virtual Machine implementation recognizes
and
attributes found in the
If a Java Virtual Machine implementation recognizes
attributes found in the
A Java Virtual Machine implementation is required to silently ignore any or all attributes in the
that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of the
java虚拟机规范链接:http://docs.oracle.com/javase/specs/jvms/se7/html/
关于class文件结构的介绍:http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.1
ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; }
The items in the
ClassFilestructure are as follows:
magic
The
magicitem supplies the magic number identifying the
classfile format; it has the value
0xCAFEBABE.
minor_version, major_version
The values of the
minor_versionand
major_versionitems are the minor and major version numbers of this
classfile.
Together, a major and a minor version number determine the version of the
classfile format. If a
classfile has major version number M and minor version number m, we denote the version of its
classfile
format as M.m. Thus,
classfile format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1.
A Java Virtual Machine implementation can support a
classfile format of version v if and only if v lies in some contiguous range Mi.0 ≤ v ≤ Mj.m. The
release level of the Java SE platform to which a Java Virtual Machine implementation conforms is responsible for determining the range.
Oracle's Java Virtual Machine implementation in JDK release 1.0.2 supports
classfile format versions 45.0 through 45.3 inclusive. JDK releases 1.1.* support
classfile
format versions in the range 45.0 through 45.65535 inclusive. For k ≥ 2, JDK release 1.k supports
classfile format versions in the range 45.0 through 44+k.0 inclusive.
constant_pool_count
The value of the
constant_pool_countitem is equal to the number of entries in the
constant_pooltable plus one. A
constant_poolindex
is considered valid if it is greater than zero and less than
constant_pool_count, with the exception for constants of type
longand
doublenoted in §4.4.5.
constant_pool[]
The
constant_poolis a table of structures (§4.4)
representing various string constants, class and interface names, field names, and other constants that are referred to within the
ClassFilestructure and its substructures. The format of each
constant_pooltable
entry is indicated by its first "tag" byte.
The
constant_pooltable is indexed from 1 to
constant_pool_count-1.
access_flags
The value of the
access_flagsitem is a mask of flags used to denote access permissions to and properties of this class or interface. The interpretation
of each flag, when set, is as shown in Table 4.1.
Table 4.1. Class access and property modifiers
Flag Name | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public; may be accessed from outside its package. |
ACC_FINAL | 0x0010 | Declared final; no subclasses allowed. |
ACC_SUPER | 0x0020 | Treat superclass methods specially when invoked by the invokespecial instruction. |
ACC_INTERFACE | 0x0200 | Is an interface, not a class. |
ACC_ABSTRACT | 0x0400 | Declared abstract; must not be instantiated. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic; not present in the source code. |
ACC_ANNOTATION | 0x2000 | Declared as an annotation type. |
ACC_ENUM | 0x4000 | Declared as an enumtype. |
ACC_SYNTHETICflag to indicate that it was generated by a compiler and does not appear in source code.
The
ACC_ENUMflag indicates that this class or its superclass is declared as an enumerated type.
An interface is distinguished by its
ACC_INTERFACEflag being set. If its
ACC_INTERFACEflag is not set, this
classfile
defines a class, not an interface.
If the
ACC_INTERFACEflag of this
classfile is set, its
ACC_ABSTRACTflag must also be set
(JLS §9.1.1.1). Such a
classfile must not have its
ACC_FINAL,
ACC_SUPERor
ACC_ENUMflags set.
An annotation type must have its
ACC_ANNOTATIONflag set. If the
ACC_ANNOTATIONflag is set, the
ACC_INTERFACEflag
must be set as well. If the
ACC_INTERFACEflag of this
classfile is not set, it may have any of the other flags in Table 4.1 set,
except the
ACC_ANNOTATIONflag. However, such a
classfile cannot have both its
ACC_FINALand
ACC_ABSTRACTflags set (JLS §8.1.1.2).
The
ACC_SUPERflag indicates which of two alternative semantics is to be expressed by the invokespecial instruction
(§invokespecial) if it appears in this class. Compilers
to the instruction set of the Java Virtual Machine should set the
ACC_SUPERflag.
The
ACC_SUPERflag exists for backward compatibility with code compiled by older compilers for the Java programming language. In Oracle’s JDK prior to release 1.0.2, the compiler generated
ClassFile
access_flagsin
which the flag now representing
ACC_SUPERhad no assigned meaning, and Oracle's Java Virtual Machine implementation ignored the flag if it was set.
All bits of the
access_flagsitem not assigned in Table 4.1 are
reserved for future use. They should be set to zero in generated
classfiles and should be ignored by Java Virtual Machine implementations.
this_class
The value of the
this_classitem must be a valid index into the
constant_pooltable. The
constant_poolentry
at that index must be a
CONSTANT_Class_infostructure (§4.4.1)
representing the class or interface defined by this
classfile.
super_class
For a class, the value of the
super_classitem either must be zero or must be a valid index into the
constant_pooltable.
If the value of the
super_classitem is nonzero, the
constant_poolentry at that index must be a
CONSTANT_Class_infostructure (§4.4.1)
representing the direct superclass of the class defined by this
classfile. Neither the direct superclass nor any of its superclasses may have the
ACC_FINALflag set in the
access_flagsitem
of its
ClassFilestructure.
If the value of the
super_classitem is zero, then this
classfile must represent the class
Object,
the only class or interface without a direct superclass.
For an interface, the value of the
super_classitem must always be a valid index into the
constant_pooltable. The
constant_poolentry
at that index must be a
CONSTANT_Class_infostructure representing the class
Object.
interfaces_count
The value of the
interfaces_countitem gives the number of direct superinterfaces of this class or interface type.
interfaces[]
Each value in the
interfacesarray must be a valid index into the
constant_pooltable. The
constant_poolentry
at each value of
interfaces[i], where 0 ≤ i <
interfaces_count, must be a
CONSTANT_Class_infostructure
(§4.4.1) representing an interface that is a direct superinterface of this
class or interface type, in the left-to-right order given in the source for the type.
fields_count
The value of the
fields_countitem gives the number of
field_infostructures in the
fieldstable.
The
field_infostructures (§4.5) represent all fields, both class variables and
instance variables, declared by this class or interface type.
fields[]
Each value in the
fieldstable must be a
field_info(§4.5)
structure giving a complete description of a field in this class or interface. The
fieldstable includes only those fields that are declared by this class or interface. It does not include items representing fields that are inherited
from superclasses or superinterfaces.
methods_count
The value of the
methods_countitem gives the number of
method_infostructures in the
methodstable.
methods[]
Each value in the
methodstable must be a
method_info(§4.6)
structure giving a complete description of a method in this class or interface. If neither of the
ACC_NATIVEand
ACC_ABSTRACTflags are set in the
access_flagsitem of a
method_infostructure,
the Java Virtual Machine instructions implementing the method are also supplied.
The
method_infostructures represent all methods declared by this class or interface type, including instance methods, class methods, instance initialization
methods (§2.9), and any class or interface initialization method (§2.9).
The
methodstable does not include items representing methods that are inherited from superclasses or superinterfaces.
attributes_count
The value of the
attributes_countitem gives the number of attributes (§4.7)
in the
attributestable of this class.
attributes[]
Each value of the
attributestable must be an
attribute_info(§4.7)
structure.
The attributes defined by this specification as appearing in the
attributestable of a
ClassFilestructure are the
InnerClasses(§4.7.6),
EnclosingMethod(§4.7.7),
Synthetic(§4.7.8),
Signature(§4.7.9),
SourceFile(§4.7.10),
SourceDebugExtension(§4.7.11),
Deprecated(§4.7.15),
RuntimeVisibleAnnotations(§4.7.16),
RuntimeInvisibleAnnotations(§4.7.17),
and
BootstrapMethods(§4.7.21) attributes.
If a Java Virtual Machine implementation recognizes
classfiles whose version number is 49.0 or above, it must recognize and correctly read
Signature(§4.7.9),
RuntimeVisibleAnnotations(§4.7.16),
and
RuntimeInvisibleAnnotations(§4.7.17)
attributes found in the
attributestable of a
ClassFilestructure of a
classfile whose version number is 49.0 or above.
If a Java Virtual Machine implementation recognizes
classfiles whose version number is 51.0 or above, it must recognize and correctly read
BootstrapMethods(§4.7.21)
attributes found in the
attributestable of a
ClassFilestructure of a
classfile whose version number is 51.0 or above.
A Java Virtual Machine implementation is required to silently ignore any or all attributes in the
attributestable of a
ClassFilestructure
that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of the
classfile, but only to provide additional descriptive information (§4.7.1).
相关文章推荐
- Jvm(35.2),class文件结构----常量池(下)
- JVM的Class文件结构
- JVM探索之路之Class文件结构解析(二):常量池
- JVM-Class文件结构
- Jvm(36),class文件结构----访问标志
- [二]Java虚拟机 jvm内存结构 运行时数据内存 class文件与jvm内存结构的映射 jvm数据类型 虚拟机栈 方法区 堆 含义
- Jvm(37),class文件结构-----类索引、父类索引、接口索引集合
- JVM(PART XIX)Class文件(II)Class文件结构
- JVM探索之路之Class文件结构解析(二):常量池
- java面试- 深入理解JVM(七)——Class文件结构
- 理解JVM的class文件结构
- Jvm(38),class文件结构----字段表集合
- Jvm(39),class文件结构----- 方法表集合
- 深入JVM的Class文件结构
- 深入理解JVM(七)——Class文件结构
- 深入理解JVM : class文件结构之常量池(1)
- JVM学习序列之一:Java Class文件结构分析
- 深入理解JVM : class文件结构之类信息描述、字段表、方法表(2)
- Jvm(40),class文件结构----- 属性表集合(这里内容比较多,等jvm写完之后看这里还有什么补充的吗)
- JVM Class 文件结构详解二