[Grocery store] Java JVM # Class class structure

Code compiles results into native machine code from bytecode, is a small step in the development of storage format, but it is a big step programming language. - "in-depth understanding of the Java Virtual Machine"

Computer known only 0 and 1. So we write programming languages ​​have escaped into binary machine code to make local machine understanding. However, with the development of virtual machines, including Java, including many languages, and have chosen an operating system, the machine instruction set independent neutral storage format to store data compiled.

Independence

We all know that Java classic slogan, "compile once, run anywhere." To achieve this goal, customized virtual machines on each platform, unified data needs to be read. This data does not depend on any platform, do not even care which language is compiled to come , as long as the unified format, the virtual machine will be able to use it properly. This unified format is - byte code (Class files).

Class files stored in the Java virtual machine instruction set and the symbol table and a number of other auxiliary and structural constraints. For safety reasons, Class files used in a number of mandatory grammatical and structural constraints.

Class structure of the class file

Let's look at the hard dish of this article, the structure of Class files. Although the book is a Gangster tells JDK1.4 version, but it contains instructions, the most important attribute is the most basic Class file. Follow-up different versions of it are enhanced.

Any Class file definition information corresponding to a single class or interface, but conversely, do not necessarily have to classes and interfaces defined in a file (such as classes and interfaces can also be generated directly by the class loader).

Class file is a set of 8-bit bytes of binary stream based unit, the data items are arranged compactly in the exact order in the Class file, without adding any intermediate separators, which makes the contents of the entire file stored almost Class data is necessary to run the program, there is no spaces.

Class there are two data types (although open with a hex editor, looks are hex characters): unsigned and tables . Unsigned number can be used to describe figures, reference index, the value of the number of values or string configuration according to UTF-8 encoding. Table unsigned number is a plurality of tables or as other data entry configured to conform to the data type, all tables are accustomed to "info_" end. Table for complex data structure describing a hierarchical relationship, the entire Class file is essentially a table.

Class structure

Which is similar to the immediately constant_pool_count, constant_pool such data may be considered as a whole (a table), the number in front of the latter record data.

Version of the magic number of Class file

See class file structure that table, the first is u4 magic. This is a magic number accounted for four bytes, and its sole function is to determine whether the file is a Class file. It is a flag that tells its own virtual machine is Class file, doing so is more secure, four bytes stored value is fixed, under hexadecimal "0xCAFEBABE", coffee baby.

The next two bytes are the Minor (minor version) and two-byte Major (primary version). When this Class are stored with the file which version of the compiler to compile such 50.3,50 is the major version 3 is the minor version. In the run can be backward-compatible, such as 51 versions of virtual machines you can run 50.3 version of the class file, but not vice versa.

Constant pool

Then constant_pool_count, constant_pool is part of the constant pool. The constant pool resources can be understood as a Class file repository, which is Class file structure associated with most data types and other projects, but also occupy one of the largest Class file data items.

First two bytes constant_pool_count is a constant quantity of the statistics behind constant_pool. Note that the latter is a number starting from 1, for example constant_pool_count stored number is 22, then constant_pool 21 stored on the data items. So designed to allow the "0th position," wrote special data storage. Class file is only this part of the count from the beginning, the other part is to start from zero.

The main constant pool storing two categories constants: literal and symbolic references . Literal understanding is injected into the well string, final modification of constant values and so on. The main symbolic references contained about three constants:

  1. The fully qualified name of the class and interface
  2. And a descriptor field name
  3. The method name and descriptor

You will know the real memory entry (address of a message) when the final distribution of the Class file memory does not store the various methods, fields, and only in the implementation of the specific code. In JDK1.4, the constant term constant pool may contain the following (content version will later be expanded):

The constant pool item type

The most troublesome of these types each have their own structure, but the common feature is the first byte is stored with the tag, that is, to tell their own virtual machine that a constant term. Part of this can be seen a lot of things, such as a two-byte maximum variable name, that is the size of 64KB English characters, of course, common sense is not such a metamorphosis variable name it.

Access flag

After the end of the constant pool, followed by two bytes represent access flag (access_flags), this flag is used to identify some of the class or interface level access information.

Used to calculate or access flag, such as a class is ACC_PIBLIC (0x0001), ACC_SUPER (0x0020) of the modified then calculated as 0x0001 | 0x0020 = 0x0021, the value of the flag value is stored is accessed. Java has special calculation package keyword.

Class index, and the index of the parent class interface index set

Class index (this_class) and the parent index (super_class) u2 is a type of data, and the interface is a collection of index u2 types of data. Class file has three to determine the inheritance. In addition to the Object class, not all primer Fusuo 0. If the size of the structure of the counter is 0, then no data behind that part.

Fields set of tables

Field tables used to describe variables declared within a class or an interface. Field includes instance variables and class-level stage (target level) variables, but does not include local variables inside method. The following is the first property access flag field table structure and fields of the table.

Calculated and before the class or interface access access_flags denote like. The two properties are closely followed and name_index descriptor_index, they represent the name and simple method descriptor.

Field set list does not list inherited from a superclass or parent interface down field, but the field would be listed in the Java code is not present, such as in order to keep within the class field of the external access class automatically added . Also in Java, the same class can not appear the same field names simple names, such as int name, followed by the String name. However, the bytecode level, abbreviated name may be the same, different enough described later.

Table collection method

Field of the table mechanism and the structure substantially similar to the method table.

Fields in a table corresponding to the set, if the parent class method is not overridden in a subclass, the method table to the collection method of the parent class information does not occur. In the Java language, to override a method, in addition to having the same name as the original simple method, but also required to have a different and original method signatures. Is a collection of signatures of each parameter in the method field constant pool reference symbol, because the return value is not included in the characteristic signature, it is only return different values, it is not overloaded.

Property sheet collection

In the Class file, field table, table method carries its own set of attribute table. Property sheet data items relative to other parts of a little more relaxed, but there are a lot of content. The following look at the more important.

Code properties

Method body Java program class code has been compiled in Code stored in the attribute, but the method and the interface of the abstract class does not exist in the Code attribute.

max_locals represents the storage required for the local variable table, which is the minimum unit Slot. Slot which can be multiplexed, code is executed when the variable exceeds a local scope, local variable occupied Slot may be used by other local variables, saving significant space.

code_length and time code values ​​are stored after the Java source code compiler generates bytecode instructions. Since each code only one byte, so the number of instructions that can be represented only 256. Although the length of time code_length four bytes, but due to the provision of virtual machines can only use two bytes, so the maximum can only compile the 65535 instruction in general is good enough, but to compile complex JSP time Note that some of the information the compiler will output JSP pages and content merge in a method, it is possible the compilation fails.

It is worth mentioning that, Javac when compiling methods, parameters, even if you do not fill, agrs_size it may be 1, which is due to the implicit pass in the this, of course, static modification of the method parameter is 0 (without fill) .

Try-catch have been used when the notice finlly does not change the value of the local variables, thought it was already return a try, after the return of data finlly before going to execute, it is not. For example, the following code.

public int inc(){
    int x;
    try{
        x=1;
        return x;
    }catch(Exception e){
        x=2;
        return x;
    }finally{
        x=3;
    }
}
复制代码

This code is never output x = 3, is such that the order of execution (to not throw an example): First perform x = 1, the local variable is equal to 1. In this case then read return instruction, then x is value is assigned to a space, which is the value returned when return, we put a name to this space, called returnX, enter the code and then finally, pay attention to scope this time, still in this inc () method. Then assign x is equal to 3, and finally execute the return instruction, the return value returnX just a piece of space to the caller. Inc is leaving () scopes, when x Slot piece can be reused.

other

  1. Exceptions storage method throws exception later.
  2. LineNumberTable is not required, but the default fill, if you do not fill, throw exception stack when he could not locate where the line.
  3. LocalVariableTable not required, it is used to describe the relationship between variables in the stack frame of the local variable table variables defined in the Java source code.
  4. This Class SourceFile records generated source file name
  5. ConstantValue notify the virtual machine is automatically assigned a static variable, it was assigned before the initialization.
  6. InnerClasser records an association between the class and the host inner classes.
  7. Signature by the time of this writing AOP common, this property record information is generic, because Java generics will be erased at compile time, so the need to record it, so Java at runtime can get generics original information .
  8. This property holds BootstrapMethods invokedynamic method instruction qualifiers reference guide, and has a lot Invoke packet.

Bytecode instruction

Bytecode instructions not more than 256, followed by an instruction in general will follow the parameters, it is very natural, just as we need to add parameters to write method (no arguments is kind of parameters). However, due to the number of Java virtual machine for the operating stack instead of registers (compiled language) architecture, so in most cases contain only one operation code.

Due to the limited number of byte code, so a lot of instruction will be forced unity. When such processing boolean, byte, short, and an array of char, int also converted to the corresponding type of bytecode instructions processed.

Bytecode manipulation can cause overflow when, for example, two large positive integer addition, results may be referred to as a negative number. When an overflow operation, will use the symbol of infinity to have said that if an action is not the result of a clear definition of digital words will be used to represent NaN values. All NaN values ​​used as an operand arithmetic operation, the result will be returned NaN.

Java virtual machine can support an internal synchronization method and a synchronization method for a period of a sequence of instructions level, the synchronization structure are both used when the tube (Monitor) to the support. Synchronized can be seen at this time is to get the lock Monitor.

Guess you like

Origin juejin.im/post/5d7c8c8ee51d4561d41d2e9a