java virtual machine specification (se8) - class file format (a)

Chapter IV class file format

  This chapter describes the class file format java virtual machine. Each class file contains the definition of a single class or interface. Although not necessarily all classes and interfaces defined in the file (such as classes and interfaces can also be directly generated by the class loader), we will represent any valid colloquially the class or interface is called the class file format. class file is composed of 8-bit byte stream. All 16-bit, 32-bit and 64-bit numbers are respectively constituted by reading the two, four, and eight consecutive 8-bit bytes. Multi-byte data stored using big-endian mode, i.e. endian. In java se platform, this format is supported by interfaces java.io.DataInput and java.io.DataOutput and java.io.DataInputStream and other types e.g. java.io.DataOutputStream.

  The first chapter its own set of data types to represent the class file data: types u1, u2, and u4 represent an unsigned byte, 2 bytes and 4 bytes. In these types of Java SE platform java.io.DataInput read by the interface of readUnsignedByte, readUnsignerShort readInt and methods.

  C chapter pseudo language structure similar to the interface body to represent the class file format. Examples of other fields in order to avoid the concept of the class and confusion, the description of the structure of the class file format becomes item (item). Contiguous items are sequentially stored in the class file, and is not filled between the alignment.

  Table is made up of zero or more items of variable size, it is used for the class file representing a series of composite structure. Although we use the array syntax similar to C referenced entries, but the table is a stream of different size structure of the fact table index means that can not be directly converted to byte offset table.

  When we represented as a data structure array, the array is composed of zero or more adjacent items composed of fixed size, it can be accessed by indexing the array.

  This chapter contains ASCII character references should be interpreted as Unicode code point corresponding ASCII character.

4.1 ClassFile structure

  A class file is composed of a single ClassFile structures:

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];
}

  Description of ClassFile structures as follows:

  • magic

    magic entry defines a magic number identifying the class file format, its value is 0xCAFEBABE.

  • minor_version,major_version

    minor_version and values ​​represent the class file major_version of minor version number and major version number. Determine the version number of the class file's minor version number and major version number together. If the major version number of a class file is M, the minor version number is m, we are into this class file version number is Mm. Thus the class file format version number may be ordered lexicographically by, e.g., 1.5 <2.0 <2.1.

    If and only if v is in one continuous range Mi.0≤v≤Mj.m time (Mi.0 to Mj.m virtual machine supports the version number), Java virtual machine implementation may support v version of the class file format. Java virtual machine follows the Java SE platform release level is responsible for determining range.

    JDK version 1.0.2 of the Oracle Java virtual machine implementation class file format support includes 45.0 to 45.3 class. JDK1.1. * Version supports the class file format version in the range of 45.0 to 45.65535 (including). For k≥2, JDK version supports the class file format version 1.k the range of 45.0 to 44 + k.0.

  • constant_pool_count

    Is the number of +1 constant_pool_count constant_pool table entries. Constant_pool index is greater than 0 and less than a constant_pool_count is valid, in addition to long and double constants, see the specific 4.4.5.

  • constant_pool[]

    constant_pool is a table structure showing various string constants, class and interface names, field names, and other constants referred to in the ClassFile structure and substructures.

    Constant_pool table index from 1 to constant_pool_count_1.

  • access_flags

    Access_flags mask identifies the value representing the class or interface attributes and access permissions. Meaning in the following table is provided for each identified 4.1A

    Table 4.1A and access modifiers logo

       

Identifying name value meaning
ACC_PUBLIC 0x0001 Declared public; class may be accessed outside the package.
ACC_FINAL 0x0010 Declared final; does not allow subclasses.
ACC_SUPER 0x0020 When you call invokspecialial instruction, special treatment to the superclass method.
ACC_INTERFACE 0x0200 Is an interface, not a class
ACC_ABSTRACT 0x0400 声明为 abstract; It can not be instantiated.
ACC_SYNTHETIC 0x1000 Synthetic declared; source code is not present.
ACC_ANNOTATION 0x2000 It is a type of annotation.
ACC_ENUM 0x4000 Is an enumerated type.

    Interface is distinguished by setting ACC_INTERFACE identity, if this identity is not set, then the class file is the definition of a class, not an interface

    If ACC_INTERFACE flag is set, then ACC_ABSTRACT identity must also be set, ACC_FINAL, ACC_SUPER and ACC_ENUM is not allowed to be set.

    If ACC_INTERFACE identity is not set, then the other identity except ACC_ANNOTATION can be set. However, such a class file can not be set ACC_FINAL and ACC_ABSTRACT at the same time.

    ACC_SUPER flag indicating if it appears in this class or interface, by invokespecial instruction (§invokespecial) indicates which of two alternate semantics. The compiler Java virtual machine instruction set should be set ACC_SUPER flag. In Java SE 8 and later, Java Virtual Machine that to set ACC_SUPER flag in each class file, regardless of which version of the flag of the actual value of the class file and class files.

    There is ACC_SUPER flag is for backward compatibility with older compiled by the Java programming language compiler code. In previous versions of the JDK 1.0.2, the compiler generates the access flag, which now represents the symbol ACC_SUPER not specified meaning, if the flag is set, Oracle's Java virtual machine implementation will ignore the flag.

    ACC_SYNTHETIC identifier indicates the class or interface is generated by the compiler and do not appear in the original code.

    Annotation type must be set ACC_ANNOTATION identity, if ACC_ANNOTATION flag is set, then ACC_INTERFACE identity must also be set.

    ACC_ENUM logo indicates that this class or its parent class is declared as an enumerated type.

    access_flags all unassigned in Table 4.1A of the bits are reserved for future use. When production class files, they should be set to 0, and the java virtual machine implementation needs to ignore them.

  • this_class

    This_class value must be a valid index constant_pool table. constant_pool index entries in this position must be a CONSTAN_Class_info structure, this class represents the class or interface definition file.

  • super_class

    For class, the value must be a valid index super_class 0 or the constant_pool. Super_class If the value is not 0, constant_pool entries in the index must be a position CONSTANT_Class_info structure represents the direct parent class in this class definition file. Its direct parent and all its parent class ClassFile structure can not be provided in the access_flags ACC_FINAL identification.

    Super_class If the value is 0, then this class must be the Object class, which is the only one with no direct support for parent classes or interfaces.

    For an interface, the value must be a valid index super_class constan_pool, and the index must be a corresponding entry CONSTAN_Class_info structure, representing the Object class.

  • interfaces_count

    Interfaces_count value represents the class or interface to interface directly the number of the parent

  • interfaces[]

    Each value interfaces array must be a valid index of constant_pool. constant_pool of interfaces [i] corresponding to the index value must be an entry CONSTANT_Class_info structure representing the class or interface to interface directly to the parent, and some give in order of from the left to the original code.

  • fields_count

    Fields_count value represents the number of fields in the table field_info structure. field_info structure represented by the class or interface declares all fields, including the class and instance variables.

  • fields[]

    Fields Each value in the table must be a field_indo structure, given a complete description of the class or interface in a field. Table field contains only fields declared in this class or interface. Inherited from the parent field does not include the parent class or interface.

  • methods_count

    Methods_count number of values ​​are given in Table class methods method_info structure

  • methods[]

    Methods Each value in the table must be a method_info structure, method_info given class or interface in a complete description of the method. If metchod_info structure access_flags of ACC_NATIVE and ACC_ABSTRACH identity are not set, then the realization of this method is java virtual machine instructions will be provided.

    method_info structure represents all the methods of this class or interface, instance methods, class methods, instance initialization method, and the initialization process of any of the classes and interfaces. methods inherited from the parent table does not include the parent class or interface method.

  • atttibutes_count

    Attribute_count value gives the number of attributes of this class of attrubute table.

  • attributes[]

    Each value in the table are attributes attribute_info structure.

    Table 4.7-C shows the occurrence of this specification defines the attributes in the attribute table structure of the class file. ,

    For definitions appear in the attribute table ClassFile structure of the rules given in §4.7 of attributes.

    Non-defined attributes predetermined rule attribute table relating ClassFile structure are given in §4.7.1.

4.2 represents the internal name

Name 4.2.1 binary classes and interfaces

  Class file structure appears in the class and interface names are always expressed in binary form is called a fully qualified name (binary names) of (JLS§13.1). These names are always expressed as CONSTANT_Utf8_info structure (Section 4.4.7), so you can get (if no further constrained) from the entire Unicode character space. And binary class name of the interface structures will be referenced by CONSTANT_NameAndType_info, constituting its descriptor (4.3), is achieved by reference to these names refer to them CONSTAN_Class_info structure.

  Different grammar and syntax JLS§13.1 binary name for historical reasons, appears in the class structure of the binary file name in the record. In this internal form, which constitute a delimited ASCII identifier name binary period (.) Is replaced by ASCII forward slash (/). Itself must be non-qualified name Identifier (Section 4.2.2).

  For example, Thread class ordinary binary name is java.lang.Thread. Internal form used in the class file format descriptors, a string representing the java / lang / Thread CONSTANT_Utf8_info structure to achieve a reference to the class name of Thread.

4.2.2 Non-qualified name

  Methods, fields, local variables and parameter names using unqualified name (unqualified names) storage. Unqualified name must contain at least a Unicode code point, and may not contain ASCII characters;. [/ (I.e. dot, semicolon, the left bracket, forward slash).

  The method further limited name, method name in addition to the special addition <the init> and <clinit> (§2.9), they can not contain ASCII characters <or> (i.e., left or right angle bracket angle bracket).

  Note that the field name or the name of the interface method can be <init> or <clinit>, but there is no way you can call instruction references <clinit>, only invokespecial instruction (§invokespecial) can refer to <init>.

4.3 descriptor

  Descriptor is a field or method type string. Descriptor using a modified UTF-8 strings (Section 4.4.7) represented in the class file format, it is possible to use the entire space character Unicode character (if no further restrictions words).

4.3.1 Syntax Notation

  Descriptors used to represent a specific syntax. Is a set of syntax describes how the sequence of characters of various products syntactically correct descriptor. Syntax terminator fixed-width font, italic nonterminals. Nonterminals is defined by the rear non-terminated name followed by a colon indicates defined. Right of the colon of one or more non-exchangeable terminator are continuously arranged.

  It appears on the right {x} means 0 or more times.

  The phrase on the right side (one of) each represented by the following line terminator or on a plurality of lines are optional defined.

4.3.2 field descriptors

  A field descriptor type indicates a class, instance, or local variables.

FieldDescriptor:
  FieldType
FieldType:
  BaseType
  ObjectType
  ArrayType
BaseType:
  (one of)
  B C D F I J S Z
ObjectType:
  L ClassName ;
ArrayType:
  [ ComponentType
ComponentType:
  FieldType

  BaseType character, ObjecType L and; and ArrayType of [all ASCII characters.

  ClassName represents internal form of a binary class or interface name.

  Type Meaning field descriptor table 4.3-A

  An array of field descriptors of the type represented by only 255 or less when having a dimension types.

  Table 4.3-A Meaning field descriptor

Field Type Item Types of meaning
B byte signed byte
C char Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
D double double-precision floating-point value
F float single-precision floating-point value
I int integer
J long long integer
L ClassName ; reference an instance of class ClassName
S short signed short
Z boolean true or false
[ reference one array dimension

  Word instance variable of type int descriptor is I.

  Examples of field descriptors variable of type Object is Ljava / lang / Object ;. Note that the internal use of binary name of the class Object.

  Double word descriptor type multi-dimensional array [] [] [] instance variable is [[[D.

4.3.3 Method descriptor

  The method descriptor contains zero or more parameters descriptors, a parameter indicating the type of method used, and returns a descriptor indicating the type of the value returned by the method (if any).

MethodDescriptor:
  ( {ParameterDescriptor} ) ReturnDescriptor
ParameterDescriptor:
  FieldType
ReturnDescriptor:
  FieldType
  VoidDescriptor
VoidDescriptor:
  V

  Symbol V denotes method does not return a value (which is the result of void).

This method descriptor: 

Object m (int I, Double D, Thread T) {...} 
is: 

(IDLjava / lang / Thread;) Ljava / lang / Object; 
Note that the use of binary Thread name and Objec internal form.

  If a method descriptor is valid, then the parameter list of the total length of its corresponding method of less than 255, the strength for a method and interface methods need to be considered extra this. Parameter list length calculation rule as follows: each class parameter long and double length of 2, the remainder is 1, the total length is equal to the argument list of all the method parameters and lengths.

  Regardless of the methods described or instance method is a class method, the method descriptor is the same. Although the method is passed the this example, the pointing object is called a reference method, in addition to its expected parameters, this fact is not reflected in the process descriptor. Examples of this method is referenced by calling a Java virtual machine instructions implicitly passed (§2.6.1, §4.11).

Guess you like

Origin www.cnblogs.com/lilinwei340/p/11168925.html