Java Class File 解析

解析Java Class File

1.环境

操作系统: mac
Java: 1.8
参考文档: https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf

2.准备工作

将源文件编译成class文件,使用特殊工具查看16进制的内容,本次使用工具为sublime
自行官网下载: http://www.sublimetext.com/

2.1源文件

package com.dora.jvm;

/**
 * 用于查看16进制流文件
 */
public class HelloClass {
    public static void main(String[] args) {

    }
}

2.2 编译

编译成对应的HelloClass.class文件,用sublime打开

cafe babe 0000 0034 0014 0a00 0300 1107
0012 0700 1301 0006 3c69 6e69 743e 0100
0328 2956 0100 0443 6f64 6501 000f 4c69
6e65 4e75 6d62 6572 5461 626c 6501 0012
4c6f 6361 6c56 6172 6961 626c 6554 6162
6c65 0100 0474 6869 7301 0019 4c63 6f6d
2f64 6f72 612f 6a76 6d2f 4865 6c6c 6f43
6c61 7373 3b01 0004 6d61 696e 0100 1628
5b4c 6a61 7661 2f6c 616e 672f 5374 7269
6e67 3b29 5601 0004 6172 6773 0100 135b
4c6a 6176 612f 6c61 6e67 2f53 7472 696e
673b 0100 0a53 6f75 7263 6546 696c 6501
000f 4865 6c6c 6f43 6c61 7373 2e6a 6176
610c 0004 0005 0100 1763 6f6d 2f64 6f72
612f 6a76 6d2f 4865 6c6c 6f43 6c61 7373
0100 106a 6176 612f 6c61 6e67 2f4f 626a
6563 7400 2100 0200 0300 0000 0000 0200
0100 0400 0500 0100 0600 0000 2f00 0100
0100 0000 052a b700 01b1 0000 0002 0007
0000 0006 0001 0000 0006 0008 0000 000c
0001 0000 0005 0009 000a 0000 0009 000b
000c 0001 0006 0000 002b 0000 0001 0000
0001 b100 0000 0200 0700 0000 0600 0100
0000 0900 0800 0000 0c00 0100 0000 0100
0d00 0e00 0000 0100 0f00 0000 0200 10

下面就将对这个class文件进行解析,过程是枯燥无聊的

3.Class Format

引用 -> java虚拟机规范 4.1 The ClassFile Structure

A class file consists of a single ClassFile structure:
一个class文件的组成结构

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

3.1 magic

it has the value 0xCAFEBABE,默认前4个字节就是java的魔术 cafe babe

3.2 minor_version, major_version

对应的就是java的版本,一个占2个字节,一共4个字节,java8对应的就是52,16进制就是34(没有去官网文档证实,数据来源于网络)

3.3 constant_pool_count

常量池的大小 -> 一共两个字节,我们编译的值是0x0014,也就代表常量池大小为20

3.4 constant_pool[constant_pool_count-1];

这里就是常量池,真正的大小为常量池大小减1

All constant_pool table entries have the following general format:
所有常量池对象有以下通用格式

cp_info {
 u1 tag;
 u1 info[];
}

3.4.1 Constant pool tags table

Constant Type Value
CONSTANT_Class 7
CONSTANT_Fieldref 9
CONSTANT_Methodref 10
CONSTANT_InterfaceMethodref 11
CONSTANT_String 8
CONSTANT_Integer 3
CONSTANT_Float 4
CONSTANT_Long 5
CONSTANT_Double 6
CONSTANT_NameAndType 12
CONSTANT_Utf8 1
CONSTANT_MethodHandle 15
CONSTANT_MethodType 16
CONSTANT_InvokeDynamic 18

3.4.2 The CONSTANT_Class_info Structure

The CONSTANT_Class_info structure is used to represent a class or an interface:
CONSTANT_Class_info 用于表示一个类或者一个接口

CONSTANT_Class_info {
 u1 tag;
 u2 name_index;
}

name_index:
The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry at that
index must be a CONSTANT_Utf8_info structure (§4.4.7) representing a valid binary class or interface name encoded in
internal form

3.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures

CONSTANT_Fieldref_info {
 u1 tag;
 u2 class_index;
 u2 name_and_type_index;
}
CONSTANT_Methodref_info {
 u1 tag;
 u2 class_index;
 u2 name_and_type_index;
}
CONSTANT_InterfaceMethodref_info {
 u1 tag;
 u2 class_index;
 u2 name_and_type_index;
}

class_index The value of the class_index item must be a valid index into the constant_pool table. The constant_pool
entry at that index must be a CONSTANT_Class_info structure (§4.4.1) representing a class or interface type that has the
field or method as a member. The class_index item of a CONSTANT_Methodref_info structure must be a class type, not an
interface type. The class_index item of a CONSTANT_InterfaceMethodref_info structure must be an interface type. The
class_index item of a CONSTANT_Fieldref_info structure may be either a class type or an interface type.

name_and_type_index The value of the name_and_type_index item must be a valid index into the constant_pool table. The
constant_pool entry at that index must be a CONSTANT_NameAndType_info structure (§4.4.6). This constant_pool entry
indicates the name and descriptor of the field or method. In a CONSTANT_Fieldref_info, the indicated descriptor must be
a field descriptor (§4.3.2). Otherwise, the indicated descriptor must be a method descriptor (§4.3.3). If the name of
the method of a CONSTANT_Methodref_info structure begins with a '<' ('\u003c'), then the name must be the special
name , representing an instance initialization method (§2.9). The return type of such a method must be void

3.4.3 The CONSTANT_String_info Structure

The CONSTANT_String_info structure is used to represent constant objects of the type String:

CONSTANT_String_info {
 u1 tag;
 u2 string_index;
}

string_index The value of the string_index item must be a valid index into the constant_pool table. The constant_pool
entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing the sequence of Unicode code points to
which the String object is to be initialized.

3.4.5 The CONSTANT_Integer_info and CONSTANT_Float_info Structures

CONSTANT_Integer_info {
 u1 tag;
 u4 bytes;
}
CONSTANT_Float_info {
 u1 tag;
 u4 bytes;
}

3.4.6 The CONSTANT_Long_info and CONSTANT_Double_info Structures

CONSTANT_Long_info {
 u1 tag;
 u4 high_bytes;
 u4 low_bytes;
}
CONSTANT_Double_info {
 u1 tag;
 u4 high_bytes;
 u4 low_bytes;
}

3.4.7 The CONSTANT_NameAndType_info Structure

CONSTANT_NameAndType_info {
 u1 tag;
 u2 name_index;
 u2 descriptor_index;
}

name_index The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry
at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing either the special method name (§2.9)
or a valid unqualified name denoting a field or method
(§4.2.2).

descriptor_index The value of the descriptor_index item must be a valid index into the constant_pool table. The
constant_pool entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing a valid field descriptor
or method descriptor (§4.3.2, §4.3.3).

3.4.8 The CONSTANT_Utf8_info Structure

CONSTANT_Utf8_info {
 u1 tag;
 u2 length;
 u1 bytes[length];
}

length
The value of the length item gives the number of bytes in the bytes array (not
the length of the resulting string).
bytes[]
The bytes array contains the bytes of the string.
No byte may have the value (byte)0.
No byte may lie in the range (byte)0xf0 to (byte)0xff

3.4.9 The CONSTANT_MethodHandle_info Structure

CONSTANT_MethodHandle_info {
 u1 tag;
 u1 reference_kind;
 u2 reference_index;
}

3.4.10 The CONSTANT_MethodType_info Structure

CONSTANT_MethodType_info {
 u1 tag;
 u2 descriptor_index;
}

3.4.11 The CONSTANT_InvokeDynamic_info Structure

CONSTANT_InvokeDynamic_info {
 u1 tag;
 u2 bootstrap_method_attr_index;
 u2 name_and_type_index;
}

3.5 u2 access_flags

两个字节的 访问修饰符

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.

3.6 this_class

this_class The value of the this_class item must be a valid index into the constant_pool table. The constant_pool entry
at that index must be a CONSTANT_Class_info structure (§4.4.1) representing the class or interface defined by this class
file

3.7 super_class

For a class, the value of the super_class item either must be zero or must be a valid index into the constant_pool
table. If the value of the super_class item is nonzero, the constant_pool entry at that index must be a
CONSTANT_Class_info structure representing the direct superclass of the class defined by this class file. Neither the
direct superclass nor any of its superclasses may have the ACC_FINAL flag set in the access_flags item of its ClassFile
structure. If the value of the super_class item is zero, then this class file must represent the class Object, 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 the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure
representing the class Object.

3.8 interfaces_count

The value of the interfaces_count item gives the number of direct superinterfaces of this class or interface type.

3.9 interfaces[]

Each value in the interfaces array must be a valid index into the constant_pool table. The constant_pool entry at each
value of interfaces[i], where 0 ≤ i < interfaces_count, must be a CONSTANT_Class_info structure 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

3.10 fields_count

The value of the fields_count item gives the number of field_info structures in the fields table. The field_info
structures represent all fields, both class variables and instance variables, declared by this class or interface type.

3.11 fields[]

The structure has the following format:

field_info {
 u2 access_flags;
 u2 name_index;
 u2 descriptor_index;
 u2 attributes_count;
 attribute_info attributes[attributes_count];
}

Field access and property flags

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_PRIVATE 0x0002 Declared private; usable only within the defining class.
ACC_PROTECTED 0x0004 Declared protected; may be accessed within subclasses.
ACC_STATIC 0x0008 Declared static.
ACC_FINAL 0x0010 Declared final; never directly assigned to after object construction (JLS §17.5).
ACC_VOLATILE 0x0040 Declared volatile; cannot be cached.
ACC_TRANSIENT 0x0080 Declared transient; not written or read by a persistent object manager.
ACC_SYNTHETIC 0x1000 Declared synthetic; not present in the source code.
ACC_ENUM 0x4000 Declared as an element of an enum.

3.12 methods_count[]

The value of the methods_count item gives the number of method_info structures in the methods table.

3.13 methods[]

Method access and property flags

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_PRIVATE 0x0002 Declared private; accessible only within the defining class.
ACC_PROTECTED 0x0004 Declared protected; may be accessed within subclasses.
ACC_STATIC 0x0008 Declared static.
ACC_FINAL 0x0010 Declared final; must not be overridden (§5.4.5).
ACC_SYNCHRONIZED 0x0020 Declared synchronized; invocation is wrapped by a monitor use.
ACC_BRIDGE 0x0040 A bridge method, generated by the compiler.
ACC_VARARGS 0x0080 Declared with variable number of arguments.
ACC_NATIVE 0x0100 Declared native; implemented in a language other than Java.
ACC_ABSTRACT 0x0400 Declared abstract; no implementation is provided.
ACC_STRICT 0x0800 Declared strictfp; floating-point mode is FP strict.
ACC_SYNTHETIC 0x1000 Declared synthetic; not present in the source code.

3.14 attributes_count

The value of the attributes_count item gives the number of attributes in the
attributes table of this class.

3.15 attributes[]

这里东西太多了 去文档自己查吧 都是对照文档进行解析的

4 解析文件

cafe babe                   魔术
0000 0034                   minor.version and major.veriosn
0014                        常量池数量 20
0a 0003 0011                1.CONSTANT_Methodref class_index -> #3 name_and_type_index -> #17
07 0012                     2.CONSTANT_Class name_index -> #18
07 0013                     3.CONSTANT_Class name_index -> #19
01 0006 3c69 6e69 743e      4.CONSTANT_Utf8 length = 6  bytes[length]
01 0003 2829 56             5.CONSTANT_Utf8 length = 3  bytes[length]
01 0004 436f 6465           6.CONSTANT_Utf8 length = 4  bytes[length]
01 000f 4c69 6e65 4e75 6d62 
6572 5461 626c 65           7.CONSTANT_Utf8 length = 15  bytes[length]
01 0012 4c6f 6361 6c56 6172 
6961 626c 6554 6162 6c65    8.CONSTANT_Utf8 length = 18  bytes[length]
01 0004 74 6869 73          9.CONSTANT_Utf8 length = 4  bytes[length]
01 0019 4c63 6f6d 2f64 6f72 
612f 6a76 6d2f 4865 6c6c 6f43
6c61 7373 3b                10.CONSTANT_Utf8 length = 25  bytes[length]
01 0004 6d61 696e           11.CONSTANT_Utf8 length = 4  bytes[length]
01 0016 28 5b4c 6a61 7661 
2f6c 616e 672f 5374 7269
6e67 3b29 56                12.CONSTANT_Utf8 length = 22  bytes[length]
01 0004 6172 6773           13.CONSTANT_Utf8 length = 4  bytes[length]
01 0013 5b 4c6a 6176 612f 
6c61 6e67 2f53 7472 696e 
673b                        14.CONSTANT_Utf8 length = 19  bytes[length]
01 000a 53 6f75 7263 6546 
696c 65                     15.CONSTANT_Utf8 length = 10  bytes[length]
01 000f 4865 6c6c 6f43 6c61 
7373 2e6a 6176 61           16.CONSTANT_Utf8 length = 15  bytes[length] 
0c 0004 0005                17.CONSTANT_NameAndType name_index -> #4 descriptor_index-> #5
01 0017 63 6f6d 2f64 6f72
612f 6a76 6d2f 4865 6c6c 
6f43 6c61 7373              18.CONSTANT_Utf8 length = 23  bytes[length]
01 0010 6a 6176 612f 6c61 
6e67 2f4f 626a 6563 74      19.CONSTANT_Utf8 length = 16  bytes[length]
00 21                       access_flags ACC_PUBLIC  ACC_SUPER
00 02                       this_class  #2
00 03                       super_class #3
00 00                       interfaces_count 0
00 00                       fields_count 0
00 02                       methods_count 2
0001                        method access_flags ACC_PUBLIC
0004                        name_index       #4
0005                        descriptor_index #5
0001                        attributes_count  1
0006                        attribute_name_index #6 CODE
0000 002f                   attribute_length 47
0001 0001 0000 0005 2a      info[attribute_length]
b700 01b1 0000 0002 0007
0000 0006 0001 0000 0006 
0008 0000 000c 0001 0000 
0005 0009 000a 0000 
0009                        method access_flags ACC_PUBLIC ACC_STATIC
000b                        name_index #11
000c                        descriptor_index #12
0001                        attributes_count  1
0006                        attribute_name_index #6 CODE
0000 002b                   attribute_length 43
0000 0001 0000 0001 b100    info[attribute_length]
0000 0200 0700 0000 0600 
0100 0000 0900 0800 0000 
0c00 0100 0000 0100 0d00 
0e00 00
0001                        attributes_count 1
000f                        attribute_name_index #15
0000 0002                   attribute_length 2
00 10                       info[attribute_length]
posted @ 2022-09-21 11:38  immortal_mode  阅读(60)  评论(0编辑  收藏  举报