.class file method code hidden in there

java class can be compiled into a byte code, and then placed on virtual machines.

class file format

Class file is a set of 8-bit binary stream in units of bytes based on respective data items sequentially press closely arranged rearwardly from the front. We can be seen as a huge class file structure, all of the information in this class are all here, like json.

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

method_info methods[methods_count];This means that there are a method array, this type of information in all methods contained inside the array, each of which is the array method_infowhich is a structure, which contains all the information of a method.
This is the method_infodefinition of the structure of the pseudocode

method_info {
    u2             access_flags;
    u2             name_index;//方法名
    u2             descriptor_index;
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

attributes includes attributes of the various aspects of the method, which also code attributes. Although there appears to be an array of virtually every property should not the same size, so the attributes there should be a structure that contains many attributes.

Code_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 max_stack;
    u2 max_locals;
    u4 code_length;
    u1 code[code_length];
    u2 exception_table_length;
    {   u2 start_pc;
        u2 end_pc;
        u2 handler_pc;
        u2 catch_type;
    } exception_table[exception_table_length];
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}

The method body code is the binary code inside the array.
Here binary code is java byte code may be understood as a platform-independent assembly language. The virtual machine is responsible for the compilation of this translated into a real machine language.

class constant pool

Class is used to store constants used in the string, the class name, interface names, method names, and instance names like members.

Reference:
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.3
https://zh.wikipedia.org/wiki/Java%E5%AD % 97% E8% 8A% 82 % E7% A0% 81

Published 124 original articles · won praise 8 · views 50000 +

Guess you like

Origin blog.csdn.net/yrk0556/article/details/104080579