package bytecode;
/**
* Created by IntelliJ IDEA.
*
* @author Eric Cui
* @date 2017/12/7 16:43
*/
public class ClazzTest extends Thread implements Runnable {
public int instanceField = 1;
public static int classField = 2;
public static final int constField = 3;
{
System.out.println("static block");
}
public static void main(String[] args) {
ClazzTest clazz = new ClazzTest();
System.out.println("instantField: " + clazz.instanceField);
System.out.println("classField : " + ClazzTest.classField);
clazz.start();
}
@Override
public void run() {
System.out.println("Thread start run. " + constField);
}
}
执行命令 javap -c -verbose ClazzTest
警告: 二进制文件ClazzTest包含bytecode.ClazzTest
Classfile /Users/minee/workspace/IdeaProjects/multi_module_sample/__java-sample/target/classes/bytecode/ClazzTest.class
Last modified Dec 21, 2017; size 1211 bytes
MD5 checksum ede05c27167189c157425032ac81b6c2
Compiled from "ClazzTest.java"
public class bytecode.ClazzTest extends java.lang.Thread implements java.lang.Runnable
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #18.#42 // java/lang/Thread."<init>":()V
#2 = Fieldref #6.#43 // bytecode/ClazzTest.instanceField:I
#3 = Fieldref #44.#45 // java/lang/System.out:Ljava/io/PrintStream;
#4 = String #46 // static block
#5 = Methodref #47.#48 // java/io/PrintStream.println:(Ljava/lang/String;)V
#6 = Class #49 // bytecode/ClazzTest
#7 = Methodref #6.#42 // bytecode/ClazzTest."<init>":()V
#8 = Class #50 // java/lang/StringBuilder
#9 = Methodref #8.#42 // java/lang/StringBuilder."<init>":()V
#10 = String #51 // instantField:
#11 = Methodref #8.#52 // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#12 = Methodref #8.#53 // java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
#13 = Methodref #8.#54 // java/lang/StringBuilder.toString:()Ljava/lang/String;
#14 = String #55 // classField :
#15 = Fieldref #6.#56 // bytecode/ClazzTest.classField:I
#16 = Methodref #6.#57 // bytecode/ClazzTest.start:()V
#17 = String #58 // Thread start run. 3
#18 = Class #59 // java/lang/Thread
#19 = Class #60 // java/lang/Runnable
#20 = Utf8 instanceField
#21 = Utf8 I
#22 = Utf8 classField
#23 = Utf8 constField
#24 = Utf8 ConstantValue
#25 = Integer 3
#26 = Utf8 <init>
#27 = Utf8 ()V
#28 = Utf8 Code
#29 = Utf8 LineNumberTable
#30 = Utf8 LocalVariableTable
#31 = Utf8 this
#32 = Utf8 Lbytecode/ClazzTest;
#33 = Utf8 main
#34 = Utf8 ([Ljava/lang/String;)V
#35 = Utf8 args
#36 = Utf8 [Ljava/lang/String;
#37 = Utf8 clazz
#38 = Utf8 run
#39 = Utf8 <clinit>
#40 = Utf8 SourceFile
#41 = Utf8 ClazzTest.java
#42 = NameAndType #26:#27 // "<init>":()V
#43 = NameAndType #20:#21 // instanceField:I
#44 = Class #61 // java/lang/System
#45 = NameAndType #62:#63 // out:Ljava/io/PrintStream;
#46 = Utf8 static block
#47 = Class #64 // java/io/PrintStream
#48 = NameAndType #65:#66 // println:(Ljava/lang/String;)V
#49 = Utf8 bytecode/ClazzTest
#50 = Utf8 java/lang/StringBuilder
#51 = Utf8 instantField:
#52 = NameAndType #67:#68 // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#53 = NameAndType #67:#69 // append:(I)Ljava/lang/StringBuilder;
#54 = NameAndType #70:#71 // toString:()Ljava/lang/String;
#55 = Utf8 classField :
#56 = NameAndType #22:#21 // classField:I
#57 = NameAndType #72:#27 // start:()V
#58 = Utf8 Thread start run. 3
#59 = Utf8 java/lang/Thread
#60 = Utf8 java/lang/Runnable
#61 = Utf8 java/lang/System
#62 = Utf8 out
#63 = Utf8 Ljava/io/PrintStream;
#64 = Utf8 java/io/PrintStream
#65 = Utf8 println
#66 = Utf8 (Ljava/lang/String;)V
#67 = Utf8 append
#68 = Utf8 (Ljava/lang/String;)Ljava/lang/StringBuilder;
#69 = Utf8 (I)Ljava/lang/StringBuilder;
#70 = Utf8 toString
#71 = Utf8 ()Ljava/lang/String;
#72 = Utf8 start
{
public int instanceField;
descriptor: I
flags: ACC_PUBLIC
public static int classField;
descriptor: I
flags: ACC_PUBLIC, ACC_STATIC
public static final int constField;
descriptor: I
flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
ConstantValue: int 3
public bytecode.ClazzTest();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Thread."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field instanceField:I
9: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
12: ldc #4 // String static block
14: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
17: return
LineNumberTable:
line 10: 0
line 12: 4
line 17: 9
line 18: 17
LocalVariableTable:
Start Length Slot Name Signature
0 18 0 this Lbytecode/ClazzTest;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=2, args_size=1
0: new #6 // class bytecode/ClazzTest
3: dup
4: invokespecial #7 // Method "<init>":()V
7: astore_1
8: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
11: new #8 // class java/lang/StringBuilder
14: dup
15: invokespecial #9 // Method java/lang/StringBuilder."<init>":()V
18: ldc #10 // String instantField:
20: invokevirtual #11 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
23: aload_1
24: getfield #2 // Field instanceField:I
27: invokevirtual #12 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
30: invokevirtual #13 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
33: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
36: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
39: new #8 // class java/lang/StringBuilder
42: dup
43: invokespecial #9 // Method java/lang/StringBuilder."<init>":()V
46: ldc #14 // String classField :
48: invokevirtual #11 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
51: getstatic #15 // Field classField:I
54: invokevirtual #12 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
57: invokevirtual #13 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
60: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
63: aload_1
64: invokevirtual #16 // Method start:()V
67: return
LineNumberTable:
line 21: 0
line 22: 8
line 23: 36
line 24: 63
line 25: 67
LocalVariableTable:
Start Length Slot Name Signature
0 68 0 args [Ljava/lang/String;
8 60 1 clazz Lbytecode/ClazzTest;
public void run();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #17 // String Thread start run. 3
5: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
LineNumberTable:
line 29: 0
line 30: 8
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 this Lbytecode/ClazzTest;
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: iconst_2
1: putstatic #15 // Field classField:I
4: return
LineNumberTable:
line 13: 0
}
SourceFile: "ClazzTest.java"
class文件16进制查看:
00000000: cafe babe 0000 0034 0049 0a00 1200 2a09
00000010: 0006 002b 0900 2c00 2d08 002e 0a00 2f00
00000020: 3007 0031 0a00 0600 2a07 0032 0a00 0800
00000030: 2a08 0033 0a00 0800 340a 0008 0035 0a00
00000040: 0800 3608 0037 0900 0600 380a 0006 0039
00000050: 0800 3a07 003b 0700 3c01 000d 696e 7374
00000060: 616e 6365 4669 656c 6401 0001 4901 000a
00000070: 636c 6173 7346 6965 6c64 0100 0a63 6f6e
00000080: 7374 4669 656c 6401 000d 436f 6e73 7461
00000090: 6e74 5661 6c75 6503 0000 0003 0100 063c
000000a0: 696e 6974 3e01 0003 2829 5601 0004 436f
000000b0: 6465 0100 0f4c 696e 654e 756d 6265 7254
000000c0: 6162 6c65 0100 124c 6f63 616c 5661 7269
000000d0: 6162 6c65 5461 626c 6501 0004 7468 6973
000000e0: 0100 144c 6279 7465 636f 6465 2f43 6c61
000000f0: 7a7a 5465 7374 3b01 0004 6d61 696e 0100
00000100: 1628 5b4c 6a61 7661 2f6c 616e 672f 5374
00000110: 7269 6e67 3b29 5601 0004 6172 6773 0100
00000120: 135b 4c6a 6176 612f 6c61 6e67 2f53 7472
00000130: 696e 673b 0100 0563 6c61 7a7a 0100 0372
00000140: 756e 0100 083c 636c 696e 6974 3e01 000a
00000150: 536f 7572 6365 4669 6c65 0100 0e43 6c61
00000160: 7a7a 5465 7374 2e6a 6176 610c 001a 001b
00000170: 0c00 1400 1507 003d 0c00 3e00 3f01 000c
00000180: 7374 6174 6963 2062 6c6f 636b 0700 400c
00000190: 0041 0042 0100 1262 7974 6563 6f64 652f
000001a0: 436c 617a 7a54 6573 7401 0017 6a61 7661
000001b0: 2f6c 616e 672f 5374 7269 6e67 4275 696c
000001c0: 6465 7201 000e 696e 7374 616e 7446 6965
000001d0: 6c64 3a20 0c00 4300 440c 0043 0045 0c00
000001e0: 4600 4701 000e 636c 6173 7346 6965 6c64
000001f0: 2020 3a20 0c00 1600 150c 0048 001b 0100
00000200: 1354 6872 6561 6420 7374 6172 7420 7275
00000210: 6e2e 2033 0100 106a 6176 612f 6c61 6e67
00000220: 2f54 6872 6561 6401 0012 6a61 7661 2f6c
00000230: 616e 672f 5275 6e6e 6162 6c65 0100 106a
00000240: 6176 612f 6c61 6e67 2f53 7973 7465 6d01
00000250: 0003 6f75 7401 0015 4c6a 6176 612f 696f
00000260: 2f50 7269 6e74 5374 7265 616d 3b01 0013
00000270: 6a61 7661 2f69 6f2f 5072 696e 7453 7472
00000280: 6561 6d01 0007 7072 696e 746c 6e01 0015
00000290: 284c 6a61 7661 2f6c 616e 672f 5374 7269
000002a0: 6e67 3b29 5601 0006 6170 7065 6e64 0100
000002b0: 2d28 4c6a 6176 612f 6c61 6e67 2f53 7472
000002c0: 696e 673b 294c 6a61 7661 2f6c 616e 672f
000002d0: 5374 7269 6e67 4275 696c 6465 723b 0100
000002e0: 1c28 4929 4c6a 6176 612f 6c61 6e67 2f53
000002f0: 7472 696e 6742 7569 6c64 6572 3b01 0008
00000300: 746f 5374 7269 6e67 0100 1428 294c 6a61
00000310: 7661 2f6c 616e 672f 5374 7269 6e67 3b01
00000320: 0005 7374 6172 7400 2100 0600 1200 0100
00000330: 1300 0300 0100 1400 1500 0000 0900 1600
00000340: 1500 0000 1900 1700 1500 0100 1800 0000
00000350: 0200 1900 0400 0100 1a00 1b00 0100 1c00
00000360: 0000 4800 0200 0100 0000 122a b700 012a
00000370: 04b5 0002 b200 0312 04b6 0005 b100 0000
00000380: 0200 1d00 0000 1200 0400 0000 0a00 0400
00000390: 0c00 0900 1100 1100 1200 1e00 0000 0c00
000003a0: 0100 0000 1200 1f00 2000 0000 0900 2100
000003b0: 2200 0100 1c00 0000 8800 0300 0200 0000
000003c0: 44bb 0006 59b7 0007 4cb2 0003 bb00 0859
000003d0: b700 0912 0ab6 000b 2bb4 0002 b600 0cb6
000003e0: 000d b600 05b2 0003 bb00 0859 b700 0912
000003f0: 0eb6 000b b200 0fb6 000c b600 0db6 0005
00000400: 2bb6 0010 b100 0000 0200 1d00 0000 1600
00000410: 0500 0000 1500 0800 1600 2400 1700 3f00
00000420: 1800 4300 1900 1e00 0000 1600 0200 0000
00000430: 4400 2300 2400 0000 0800 3c00 2500 2000
00000440: 0100 0100 2600 1b00 0100 1c00 0000 3700
00000450: 0200 0100 0000 09b2 0003 1211 b600 05b1
00000460: 0000 0002 001d 0000 000a 0002 0000 001d
00000470: 0008 001e 001e 0000 000c 0001 0000 0009
00000480: 001f 0020 0000 0008 0027 001b 0001 001c
00000490: 0000 001d 0001 0000 0000 0005 05b3 000f
000004a0: b100 0000 0100 1d00 0000 0600 0100 0000
000004b0: 0d00 0100 2800 0000 0200 290a
A class file consists of a single ClassFile structure:
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];
}
Constant pool tags
Hex Value    Constant Type
 0x07     CONSTANT_Class
 0x09     CONSTANT_Fieldref
 0x0A     CONSTANT_Methodref
 0x0B     CONSTANT_InterfaceMethodref
 0x08     CONSTANT_String
 0x03     CONSTANT_Integer
 0x04     CONSTANT_Float
 0x05     CONSTANT_Long
 0x06     CONSTANT_Double
 0x0C     CONSTANT_NameAndType
 0x01     CONSTANT_Utf8
 0x0F     CONSTANT_MethodHandle
 0x10     CONSTANT_MethodType
 0x12     CONSTANT_InvokeDynamic