Class loading and bytecode technology

1. Class file structure

A simple helloworld.java program:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("hello world!"); }}Copy the code

Javac-parameters -d HelloWorld.

How do I read the binary bytecode file once I have it? There are two ways:

  • JDK decompilerjavap -verbose XXX.class

  • Method 2: run notepad++.classFile that needs to load the plug-in HexEditor, see article:Notepad++ to view binary. Class files – HexEditor plug-in (64/32 bit) installation tutorial, installation in the article can be completed, and can be opened.classFiles to view binary bytecode:

According to the JVM specification, the class file structure is as follows

\

1.1 Magic number (U4 Magic)

Magic number (U4 Magic) : 0 to 3 bytes corresponding to the bytecode file, indicating the specific type of the file. Different files have their own magic number information, such as Java binary.

0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 091

So much for the origin of cafeBabe!

\

Version 1.2 (U2 minor_Version, U2 major_Version)

Version: 4 to 7 bytes in the bytecode file, indicating the class version:

0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09

The hex 34H(00 34) represents the decimal 52, which is JDK8, and so on: 51 is JDK 7,53 is JDK 9.

\

1.3 constant pool

Constant pool table:

Constant Type Constant Type Value Serial number of a constant (in decimal)
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

Case Study:

00 23 (35) indicates that the constant pool contains items 1 to 34. Note that the value of item 0 is not counted and does not contain 0000000 CA fe BA be 000000 34 00 23 0A 00 06 00 15 09

  • Item #1, 0a (in hexadecimal), is 10 in decimal. Lookup the constant pool table shows that it is CONSTANT_Methodref (method reference), 00 06 (6) and 00 15 (21) indicate that it references items #6 and #21 in the constant pool to get the class and method name of this method.

    0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09

  • 00 16 (22) and 00 17 (23) indicate that it refers to items #22 and # 23 in the constant pool to obtain the [class] and [member variable name] of this member variable.

    0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09

    0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07

  • Item #3, 08, indicates a string constant name, and 00, 18 (24) indicates that it refers to item #24 in the constant pool

    0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07

  • Item #4 0a refers to a Method, 00 19 (25) and 00 1A (26) refer to items #25 and #26 in the constant pool to obtain the class and Method name of the Method.

    0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07

  • Item #5 07 indicates a Class message, and 00 1b (27) indicates that it refers to item #27 in the constant pool

    0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07

  • Item #6 07 indicates a Class message, and 00 1c (28) indicates that it refers to item #28 in the constant pool

    0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07

    0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29

  • Item #7 01 represents a UTF8 string, 00 06 represents the length, 3c 69, 6e 69, 74, 3e is []

    0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29

  • Item #8 01 is a UTF8 string, 00 03 is the length, 28, 29, 56 is [()V]

    0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29

    0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e

  • Item #9 01 is a UTF8 string, 00, 04 is the length, 43, 6f, 64, 65 is Code.

    0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e

  • LineNumberTable 01 is a utF8 string, 00 0f (15) is the length, 4c 69 6E 65 4E 75 6D 62 65 72 54 61 62 6c 65

    0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e

    0000100 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63

  • 4C 6f 63 61 6C 56 61 72 69 61 62 6C 65 54 61 62 6C 65 LocalVariableTable

    0000100 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63

    0000120 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01

  • Item #12 01 is a UTF8 string, 00, 04 is the length, 74, 68, 69, 73 is this

    0000120 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01

    0000140 00 04 74 68 69 73` 01 00 1d 4c 63 6e 2f 69 74 63

  • # 13 01 said a bunch utf8, 00 1 d (29) said length, is [Lcn itcast/JVM/t5 / HelloWorld;]

    0000140 00 04 74 68 69 73 01 00 1d 4c 63 6e 2f 69 74 63

    0000160 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c 6f

  • Item #14 01 is a UTF8 string, 00, 04 is the length, 74, 68, 69, 73 is main.

    0000200 57 6f 72 6c 64 3b 01 00 04 6d 61 69 6e 01 00 16

  • Item #15 01 represents a utf8 String, 00 16 (22) represents the length, is [([Ljava/lang/String;)V] is actually an array of strings, no return value

    0000200 57 6f 72 6c 64 3b 01 00 04 6d 61 69 6e01 00 16

    0000220 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72

    0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13

  • Item #16 01 represents a UTF8 string, 00 04 represents the length, and is args.

    0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13

  • Item #17 01 represents a UTF8 String, 00 13 (19) represents the length, is [Ljava/lang/String;]

    0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13 0000260 5b 4c 6a 61 76 61 2f 6c 61 6e

    67 2f 53 74 72 69 0000300 6e 67 3b 01 00 10 4d 65 74 68 6f 64 50 61 72 61

  • Item #18 01 indicates a UTF8 string, 00, 10 (16) indicates the length, and is MethodParameters.

    0000300 6e 67 3b 01 00 10 4d 65 74 68 6f 64 50 61 72 61

    0000320 6d 65 74 65 72 73 01 00 0a 53 6f 75 72 63 65 46

  • Item #19 01 indicates a UTF8 string, 00 0a (10) indicates the length, and is SourceFile.

    0000320 6d 65 74 65 72 73 01 00 0a 53 6f 75 72 63 65 46

    0000340 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64

  • Item #20 01 indicates a UTF8 string, 00 0f (15) indicates the length, and is helloworld.java.

    0000340 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64 0000360 2e 6a 61 76 61 0c 00 07 00 08 07

    00 1d 0c 00 1e

  • 0000360 2E 6A 61 76 61 0c 00 07 07 00 1D 0C 00 1e 0000360 2E 6A 61 76 61 0c 00 07 00 08 07 00 1D 0C 00 1e

  • 0000360 2E 6A 61 76 61 0C 00 07 00 08 07 00 1D 0C 00 1E 0000360 2E 6A 61 76 61 0C 00 07 00 08 07 00 1D 0C 00 1e

  • 0000360 2E 6a 61 76 61 0c 00 07 00 08 07 00 1d0c 00 1e 0000360 2E 6A 61 76 61 0c 00 07 00 08 07 00 1d0c 00 1e

    000040000 1f 01 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64

  • Item #24 01 indicates a UTF8 string, 00 0f (15) indicates the length, and is hello world.

    0000400 00 1f01 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64

  • Item #25 07 represents a Class message, 00 20 (32) references item #32 in the constant pool

    0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74

  • Item #26 0c represents a [name + type], 00 21 (33) 00 22 (34) references the constant pool entries #33 #34

    0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74

  • Item # 27 01 said a utf8 string, 00 1 b (27) said length, is the cn/itcast/JVM/t5 / HelloWorld 】

    0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74

    0000440 63 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c

    0000460 6f 57 6f 72 6c 64 01 00 10 6a 61 76 61 2f 6c 61

  • Item #28 01 represents a UTF8 string, 00 10 (16) represents the length, is Java /lang/Object

    0000460 6f 57 6f 72 6c 64 01 00 10 6a 61 76 61 2f 6c 61

    0000500 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61

  • Item #29 01 represents a UTF8 string, 00 10 (16) represents the length, is Java /lang/System

    0000500 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61

    0000520 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00 03 6f

  • Item #30 01 represents a UTF8 string, 00 03 represents the length, and is [out]

    0000520 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00 03 6f

    0000540 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72

  • Item #31 01 represents a UTF8 string, 00 15 (21) represents the length, and is Ljava/ IO /PrintStream;

    0000540 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72

    0000560 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a 61 76

  • Item #32 01 represents a UTF8 string, 00 13 (19) represents the length, and is Java/IO /PrintStream.

    0000560 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a 61 76

    0000600 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d

  • Println 0000620 01 00 07 70 72 69 6E 74 6c 6E 01 00 15 28 4C 6a

  • Item #34 01 represents a utf8 String, 00 15 (21) represents the length, and is [(Ljava/lang/String;)V].

    0000620 01 00 07 70 72 69 6e 74 6c 6e 01 00 15 28 4c 6a

    0000640 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b

    0000660 29 56 00 21 00 05 0006 0000 0000 0000 0000 02 00 01

\

1.4 Access identity and inheritance information

(0x0001 + 0x0020) ACC_PUBLIC + ACC_SUPER:

0000660 29 56 00 21 00 05 0006 0000 0000 0000 02 00 01 05

Find the fully qualified name of the class from constant pool #5:

0000660 29 56 00 21 00 05 0006 0000 0000 0000 02 00 01 06

Find the fully qualified name of the parent class according to #6 in the constant pool:

0000660 29 56 00 21 00 0500 06 0000 0000 0000 0000 02 00 01

Represents the number of interfaces. This class is 0:

0 0000660 29 56 00 21 00 05 00 0600 0000 0000 00 02 00 01

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Declared public ; May be email exchange with email exchange from outside its package.
ACC_FINAL 0x0010 Declared final ; no subclasses allowed.(Final)
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. (enumeration)

\

1.5 the Field of information

Represents the number of member variables, 0 for this class

0 0000660 29 56 00 21 00 05 0006 0000 0000 0000 00 02 00 01

FieldType Type Interpretation
B byte signed byte
C char Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
D double double-precision floating-point value
F float single-precision floating-point value
I int integer
J long long integer
L ClassName ; reference an instance of class ClassName
S short signed short
Z boolean true or false
[ reference one array dimension

\

1.6 Method of information

Represents the number of methods. This class is 2

0000660 29 56 00 21 00 05 0006 0000 0000 0000 0000 02 00 01

A method consists of access modifiers, names, parameter descriptions, number of method attributes, and method attributes:

  • Red represents the access modifier (public in this class)

  • Blue means that constant pool #07 entries are referenced as method names

  • Green indicates that constant pool #08 items are referenced as method parameter descriptions

  • Yellow represents the number of method attributes, which in this case is 1

  • Red represents method properties

    • 00 09 refers to constant pool entry #09, found to be the [Code] property

    • 00 00 00 2f indicates that the length of this property is 47

    • 00 01 indicates the maximum depth of operand stack

    • 00 01 indicates the maximum number of slots in the local variable table

    • 2A B7 00 01 B1 are bytecode instructions

    • 00 00 00 02 represents the number of method detail attributes, in this case 2

    • 00 0a refers to constant pool item #10, found to be LineNumberTable property

      • 00 00 00 06 represents the total length of this attribute, which in this case is 6
      • 00 01 indicates the LineNumberTable length
      • 00 00 indicates the bytecode line number. 00 04 indicates the Java source line number
    • 00 0b refers to constant pool item #11 and is found to be a LocalVariableTable property

      • 00 00 00 0c represents the total length of this attribute, which in this case is 12
      • 00 01 indicates the length of LocalVariableTable
      • 00 00 represents the offset relative to the bytecode at the start of the local variable’s life cycle
      • 00 05 indicates the length of the range covered by local variables
      • 00 0c represents the local variable name, which in this example references the constant pool# 12The term is [this]
      • 00 0d represents the type of the local variable, which in this example references the constant pool# 13Item, it is [Lcn itcast/JVM/t5 / HelloWorld;]
      • 00 00 indicates the slot number occupied by the local variable. In this example, the slot number is 0

0000660 29 56 00 21 00 05 0006 0000 0000 0000 0000 02 00 01

0000700 00 07 00 08 00 01 00 09 00 00 00 2f 00 01 00 01

0000720 00 00 00 05 2a b7 00 01 b1 00 00 00 02 00 0a 00

0000740 00 00 06 00 01 00 00 00 04 00 0b 00 00 00 0c 00

0000760 01 00 00 00 05 00 0c 00 0d 00 00 00 09 00 0e 00

  • Red represents the access modifier (public static in this class)

  • Blue means that constant pool #14 is referenced as the method name

  • Green indicates that constant pool #15 is referenced as a method parameter description

  • Yellow represents the number of method attributes, which in this case is 2

  • Red represents method properties (attribute 1)

    • 00 09 refers to constant pool entry #09, found to be the [Code] property

    • 00 00 00 37 indicates that the length of this property is 55

    • 00 02 indicates the maximum depth of operand stack

    • 00 01 indicates the maximum number of slots in the local variable table

    • 00 00 00 05 indicates the bytecode length, which in this case is 9

    • B2 00 02 12 03 B6 00 04 b1 is a bytecode instruction

    • 00 00 00 02 represents the number of method detail attributes, in this case 2

    • 00 0a refers to constant pool item #10, found to be LineNumberTable property

      • 00 00 00 0a represents the total length of this attribute, which in this case is 10
      • 00 02 indicates the LineNumberTable length
      • 00 00 indicates the bytecode line number. 00 06 indicates the Java source line number
      • 00 08 indicates the bytecode line number. 00 07 indicates the Java source line number
    • 00 0b refers to constant pool item #11 and is found to be a LocalVariableTable property

      • 00 00 00 0c represents the total length of this attribute, which in this case is 12
      • 00 01 indicates the length of LocalVariableTable
      • 00 10 represents the local variable name, which in this example references the constant pool# 16The term is args.
      • 00 11 indicates the type of the local variable, which in this example references the constant pool# 17Item, is [Ljava/lang/String;]
      • 00 00 indicates the slot number occupied by the local variable. In this example, the slot number is 0

0000760 01 00 00 00 05 00 0c 00 0d 00 00 00 09 00 0e 00

0001000 0f 00 02 00 09 00 00 00 37 00 02 00 01 00 00 00

0001020 09 b2 00 02 12 03 b6 00 04 b1 00 00 00 02 00 0a

0001040 00 00 00 0a 00 02 00 00 00 06 00 08 00 07 00 0b

0001060 00 00 00 0c 00 01 00 00 00 09 00 10 00 11 00 00

Red represents method properties (Property 2)

  • 00 12 means that constant pool #18 is referenced and found to be the [MethodParameters] property

    • 00 00 00 05 represents the total length of this attribute, which in this case is 5
    • 01 Parameter Quantity
    • 00 10 refers to the constant pool# 16The term is args.
    • 00 00 Access modifier

0001100 00 12 00 00 00 05 01 00 10 00 00 0001 00 13 00

0001120 00 00 02 00 14

\

1.7 Additional Attributes

  • 00 01 indicates the number of additional attributes
  • 00 13 refers to constant pool item #19, i.e. SourceFile
  • 00 00 00 02 indicates the length of this property
  • 00 14 refers to the constant pool item #20, namely [helloworld.java].

0001100 00 12 00 00 00 05 01 00 10 00 00 0001 00 13 00

0001120 00 00 02 00 14

References docs.oracle.com/javase/spec…