简介
一个简单的class
public class JavaClassUsage {
private int age=18;
public void inc(int number){
this.age=this.age+ number;
}}
public class com.flydean.JavaClassUsage
minor version:
major version: 58
flags: ACC_PUBLIC, ACC_SUPERConstant pool:
#1 = Methodref #2.#3 // java/lang/Object."<init>":()V
#2 = Class #4 // java/lang/Object
#3 = NameAndType #5:#6 // "<init>":()V
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Fieldref #8.#9 // com/flydean/JavaClassUsage.age:I
#8 = Class #10 // com/flydean/JavaClassUsage
#9 = NameAndType #11:#12 // age:I
#10 = Utf8 com/flydean/JavaClassUsage
#11 = Utf8 age
#12 = Utf8 I
#13 = Utf8 Code
#14 = Utf8 LineNumberTable
#15 = Utf8 LocalVariableTable
#16 = Utf8 this
#17 = Utf8 Lcom/flydean/JavaClassUsage;
#18 = Utf8 inc
#19 = Utf8 (I)V
#20 = Utf8 number
#21 = Utf8 SourceFile
#22 = Utf8 JavaClassUsage.java{
public com.flydean.JavaClassUsage();
descriptor: ()V
flags: ACC_PUBLIC Code:
stack=2, locals=1, args_size=1
: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0 5: bipush 18
7: putfield #7 // Field age:I
10: return
LineNumberTable:
line 7:
line 9: 4
LocalVariableTable:
Start Length Slot Name Signature
11 this Lcom/flydean/JavaClassUsage;
public void inc(int);
descriptor: (I)V
flags: ACC_PUBLIC Code:
stack=3, locals=2, args_size=2
: aload_0 1: aload_0 2: getfield #7 // Field age:I
5: iload_1 6: iadd 7: putfield #7 // Field age:I
10: return
LineNumberTable:
line 12:
line 13: 10
LocalVariableTable:
Start Length Slot Name Signature
11 this Lcom/flydean/JavaClassUsage;
11 1 number I}SourceFile: "JavaClassUsage.java"
ClassFile的二进制文件
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];}
01
magic
02
version
03
常量池
这里有两点要注意,点,常量池数组的index是从1开始到constant_pool_count-1结束。
第二点,常量池数组的第0位是作为一个保留位,表示“不引用任何常量池项目”,为某些特殊的情况下使用。
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;}
CONSTANT_Class_info {
u1 tag;
u2 name_index;}
Constant pool:
1 = Methodref #2.#3 // java/lang/Object."<init>":()V
2 = Class #4 // java/lang/Object
3 = NameAndType #5:#6 // "<init>":()V
4 = Utf8 java/lang/Object
5 = Utf8 <init>
6 = Utf8 ()V
7 = Fieldref #8.#9 // com/flydean/JavaClassUsage.age:I
8 = Class #10 // com/flydean/JavaClassUsage
9 = NameAndType #11:#12 // age:I
10 = Utf8 com/flydean/JavaClassUsage
11 = Utf8 age
12 = Utf8 I
13 = Utf8 Code
14 = Utf8 LineNumberTable
15 = Utf8 LocalVariableTable
16 = Utf8 this
17 = Utf8 Lcom/flydean/JavaClassUsage;
18 = Utf8 inc
19 = Utf8 (I)V
20 = Utf8 number
21 = Utf8 SourceFile
22 = Utf8 JavaClassUsage.java
04
描述符
05
access_flags
06
this_class和super_class
2 = Class #4 // java/lang/Object
8 = Class #10 // com/flydean/JavaClassUsage
07
interfaces_count和interfaces[]
08
fields_count和fields[]
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];}
09
methods_count和methods[]
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];}
10
attributes_count和attributes[]
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];}
总结