请参考《深入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
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 ClassFile
structure are as follows:
-
magic
-
The
magic
item supplies the magic number identifying theclass
file format; it has the value0xCAFEBABE
.
minor_version, major_version
-
The values of the
minor_version
andmajor_version
items are the minor and major version numbers of thisclass
file. Together, a major and a minor version number determine the version of theclass
file format. If aclass
file has major version number M and minor version number m, we denote the version of itsclass
file format as M.m. Thus,class
file format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1.A Java Virtual Machine implementation can support a
class
file 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
class
file format versions 45.0 through 45.3 inclusive. JDK releases 1.1.* supportclass
file format versions in the range 45.0 through 45.65535 inclusive. For k ≥ 2, JDK release 1.k supportsclass
file format versions in the range 45.0 through 44+k.0 inclusive.
constant_pool_count
-
The value of the
constant_pool_count
item is equal to the number of entries in theconstant_pool
table plus one. Aconstant_pool
index is considered valid if it is greater than zero and less thanconstant_pool_count
, with the exception for constants of typelong
anddouble
noted in §4.4.5.
constant_pool[]
-
The
constant_pool
is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within theClassFile
structure and its substructures. The format of eachconstant_pool
table entry is indicated by its first "tag" byte.The
constant_pool
table is indexed from 1 toconstant_pool_count
-1.
access_flags
-
The value of the
access_flags
item 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 enum
type.
A class may be marked with the
ACC_SYNTHETIC
flag to indicate that it was generated by a compiler and does not appear in source code.The
ACC_ENUM
flag indicates that this class or its superclass is declared as an enumerated type.An interface is distinguished by its
ACC_INTERFACE
flag being set. If itsACC_INTERFACE
flag is not set, thisclass
file defines a class, not an interface.If the
ACC_INTERFACE
flag of thisclass
file is set, itsACC_ABSTRACT
flag must also be set (JLS §9.1.1.1). Such aclass
file must not have itsACC_FINAL
,ACC_SUPER
orACC_ENUM
flags set.An annotation type must have its
ACC_ANNOTATION
flag set. If theACC_ANNOTATION
flag is set, theACC_INTERFACE
flag must be set as well. If theACC_INTERFACE
flag of thisclass
file is not set, it may have any of the other flags in Table 4.1 set, except theACC_ANNOTATION
flag. However, such aclass
file cannot have both itsACC_FINAL
andACC_ABSTRACT
flags set (JLS §8.1.1.2).The
ACC_SUPER
flag 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 theACC_SUPER
flag.The
ACC_SUPER
flag 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 generatedClassFile
access_flags
in which the flag now representingACC_SUPER
had no assigned meaning, and Oracle's Java Virtual Machine implementation ignored the flag if it was set.All bits of the
access_flags
item not assigned in Table 4.1 are reserved for future use. They should be set to zero in generatedclass
files and should be ignored by Java Virtual Machine implementations.
this_class
-
The value of the
this_class
item must be a valid index into theconstant_pool
table. Theconstant_pool
entry at that index must be aCONSTANT_Class_info
structure (§4.4.1) representing the class or interface defined by thisclass
file.
super_class
-
For a class, the value of the
super_class
item either must be zero or must be a valid index into theconstant_pool
table. If the value of thesuper_class
item is nonzero, theconstant_pool
entry at that index must be aCONSTANT_Class_info
structure (§4.4.1) representing the direct superclass of the class defined by thisclass
file. Neither the direct superclass nor any of its superclasses may have theACC_FINAL
flag set in theaccess_flags
item of itsClassFile
structure.If the value of the
super_class
item is zero, then thisclass
file must represent the classObject
, the only class or interface without a direct superclass.For an interface, the value of the
super_class
item must always be a valid index into theconstant_pool
table. Theconstant_pool
entry at that index must be aCONSTANT_Class_info
structure representing the classObject
.
interfaces_count
-
The value of the
interfaces_count
item gives the number of direct superinterfaces of this class or interface type.
interfaces[]
-
Each value in the
interfaces
array must be a valid index into theconstant_pool
table. Theconstant_pool
entry at each value ofinterfaces[i]
, where 0 ≤ i <interfaces_count
, must be aCONSTANT_Class_info
structure (§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_count
item gives the number offield_info
structures in thefields
table. Thefield_info
structures (§4.5) represent all fields, both class variables and instance variables, declared by this class or interface type.
fields[]
-
Each value in the
fields
table must be afield_info
(§4.5) structure giving a complete description of a field in this class or interface. Thefields
table 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_count
item gives the number ofmethod_info
structures in themethods
table.
methods[]
-
Each value in the
methods
table must be amethod_info
(§4.6) structure giving a complete description of a method in this class or interface. If neither of theACC_NATIVE
andACC_ABSTRACT
flags are set in theaccess_flags
item of amethod_info
structure, the Java Virtual Machine instructions implementing the method are also supplied.The
method_info
structures 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). Themethods
table does not include items representing methods that are inherited from superclasses or superinterfaces.
attributes_count
-
The value of the
attributes_count
item gives the number of attributes (§4.7) in theattributes
table of this class.
attributes[]
-
Each value of the
attributes
table must be anattribute_info
(§4.7) structure.The attributes defined by this specification as appearing in the
attributes
table of aClassFile
structure are theInnerClasses
(§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), andBootstrapMethods
(§4.7.21) attributes.If a Java Virtual Machine implementation recognizes
class
files whose version number is 49.0 or above, it must recognize and correctly readSignature
(§4.7.9),RuntimeVisibleAnnotations
(§4.7.16), andRuntimeInvisibleAnnotations
(§4.7.17) attributes found in theattributes
table of aClassFile
structure of aclass
file whose version number is 49.0 or above.If a Java Virtual Machine implementation recognizes
class
files whose version number is 51.0 or above, it must recognize and correctly readBootstrapMethods
(§4.7.21) attributes found in theattributes
table of aClassFile
structure of aclass
file whose version number is 51.0 or above.A Java Virtual Machine implementation is required to silently ignore any or all attributes in the
attributes
table of aClassFile
structure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclass
file, but only to provide additional descriptive information (§4.7.1).