Introduction to Java class files

A class file can contain only one class or interface.

Java class files are 8-byte binary stream.

In the Java class file size and length of the variable length located before the entry of the actual data.

Basic types of class files

  u1 1byte unsigned type

  u2 2byte unsigned type

  u4 4byte unsigned type

  u8 8byte unsigned type

==========================================

The following is the Java source code file

public class ClassTest {
    final static int constantInt=12;
    final static String  constantString="我很好";
    static int variableInt=12;
    static String  variableString="我很好";
    private int variable;
    private String variable2;

    static void main public (String [] args) {
        System.out.println ( "- this is the main () method ---");

    }
    public void say() {
        System.out.println("这是普通方法");
    }
    public ClassTest(int variable, String variable2) {
        super();
        this.variable = variable;
        this.variable2 = variable2;
    }
    public ClassTest() {
       
    }

The following are uedit Editor to view bytecode files

The following are the contents of bytecode files javap -v ClssTest.class view 

Last modified 2019-7-24; size 924 bytes
  MD5 checksum be5b20653620d7d2b495a3f99e3fb417
  Compiled from "ClassTest.java"
public class ClassTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
  #1 = Fieldref          #34.#35        // java/lang/System.out:Ljava/io/PrintStream;
  #2 = String            #36            // --这是main()方法---
  #3 = Methodref          #37.#38        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #4 = String            #39            // 这是普通方法
  #5 = Methodref          #12.#40        // java/lang/Object."<init>":()V
  #6 = Fieldref          #11.#41        // ClassTest.variable:I
  #7 = Fieldref          #11.#42        // ClassTest.variable2:Ljava/lang/String;
  #8 = Fieldref          #11.#43        // ClassTest.variableInt:I
  #9 = String            #44            // 我很好
  #10 = Fieldref          #11.#45        // ClassTest.variableString:Ljava/lang/String;
  #11 = Class              #46            // ClassTest
  #12 = Class              #47            // java/lang/Object
  #13 = Utf8              constantInt
  #14 = Utf8              I
  #15 = Utf8              ConstantValue
  #16 = Integer            12
  #17 = Utf8              constantString
  #18 = Utf8              Ljava/lang/String;
  #19 = Utf8              variableInt
  #20 = Utf8              variableString
  #21 = Utf8              variable
  #22 = Utf8              variable2
  #23 = Utf8              main
  #24 = Utf8              ([Ljava/lang/String;)V
  #25 = Utf8              Code
  #26 = Utf8              LineNumberTable
  #27 = Utf8              say
  #28 = Utf8              ()V
  #29 = Utf8              <init>
  #30 = Utf8              (ILjava/lang/String;)V
  #31 = Utf8              <clinit>
  #32 = Utf8              SourceFile
  #33 = Utf8              ClassTest.java
  #34 = Class              #48            // java/lang/System
  #35 = NameAndType        #49:#50        // out:Ljava/io/PrintStream;
  # 36 = Utf8 - this is the main () method ---
  # # 51 is 37 [= // Java Class / IO / PrintStream
  # 38 is NameAndType # = 52 is: the println // # 53 is: (Ljava / lang / String;) V
  # 39 = Utf8 this is a common method of
  # 40 = NameAndType # 29: # 28 // "<the init>" :() V
  # 41 is NameAndType = # 21 is: # 14 // variable: the I
  # 42 is NameAndType = # 22 is: # // variable2 18: Ljava / lang / String;
  # 43 = NameAndType # 19: # 14 // variableInt: the I
  # 44 = Utf8 I'm fine
  # 45 = NameAndType # 20: # 18 // variableString: Ljava / lang / String ;
  # = 46 is Utf8 ClassTest
  # 47 = Utf8 Java / lang / Object
  # 48 = Utf8 Java / lang / the System
  #49 = Utf8              out
  #50 = Utf8              Ljava/io/PrintStream;
  #51 = Utf8              java/io/PrintStream
  #52 = Utf8              println
  #53 = Utf8              (Ljava/lang/String;)V
{
  static final int constantInt;
    descriptor: I
    flags: ACC_STATIC, ACC_FINAL
    ConstantValue: int 12

  static final java.lang.String constantString;
    descriptor: Ljava/lang/String;
    flags: ACC_STATIC, ACC_FINAL
    ConstantValue: String 我很好

  static int variableInt;
    descriptor: I
    flags: ACC_STATIC

  static java.lang.String variableString;
    descriptor: Ljava/lang/String;
    flags: ACC_STATIC

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
        0: getstatic    #1                  // Field java/lang/System.out:Ljava/io/PrintStream;
        3: ldc          #2                  // String --这是main()方法---
        5: invokevirtual #3                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        8: return
      LineNumberTable:
        line 11: 0
        line 13: 8

  public void say();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
        0: getstatic    #1                  // Field java/lang/System.out:Ljava/io/PrintStream;
        3: ldc          #4                  // String 这是普通方法
        5: invokevirtual #3                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        8: return
      LineNumberTable:
        line 15: 0
        line 16: 8

  public ClassTest(int, java.lang.String);
    descriptor: (ILjava/lang/String;)V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
        0: aload_0
        1: invokespecial #5                  // Method java/lang/Object."<init>":()V
        4: aload_0
        5: iload_1
        6: putfield      #6                  // Field variable:I
        9: aload_0
        10: aload_2
        11: putfield      #7                  // Field variable2:Ljava/lang/String;
        14: return
      LineNumberTable:
        line 18: 0
        line 19: 4
        line 20: 9
        line 21: 14

  public ClassTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
        0: aload_0
        1: invokespecial #5                  // Method java/lang/Object."<init>":()V
        4: return
      LineNumberTable:
        line 22: 0
        line 24: 4

  static {};
    descriptor: ()V
    flags: ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
        0: bipush        12
        2: putstatic    #8                  // Field variableInt:I
        5: ldc          #9                  // String 我很好
        7: putstatic    #10                // Field variableString:Ljava/lang/String;
        10: return
      LineNumberTable:
        line 5: 0
        line 6: 5
}
SourceFile: "ClassTest.java"

Simple version:

minor version: 0 // --- the major version number
  major version: 52 // --- minor version number of     
  flags: ACC_PUBLIC, ACC_SUPER // --- Class Access Identity
 // --- constant pool
Constant the pool:
  # 1 = 34. a // # 35 # FieldRef Java / lang / the System.out: Ljava / IO / PrintStream;
  # 2 = # 36 // String - this is the main () method ---
  .......
  # 52 Utf8 the println =
  # = 53 is Utf8 (Ljava / lang / String;) V
{
// --- field information
  static int Final constantInt;
    descriptor: the I
    the flags: ACC_STATIC, ACC_FINAL
    ConstantValue: int 12 is


  static int variableInt;
    descriptor: I
    flags: ACC_STATIC


//---方法信息
  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
        0: getstatic    #1                  // Field java/lang/System.out:Ljava/io/PrintStream;
        3: ldc          #2                  // String --这是main()方法---
        5: invokevirtual #3                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        8: return
      LineNumberTable:
        line 11: 0
        line 13: 8

 
  public ClassTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
        0: aload_0
        1: invokespecial #5                  // Method java/lang/Object."<init>":()V
        4: return
      LineNumberTable:
        line 22: 0
        line 24: 4
       
//-- super()方法
  static {};
    descriptor: ()V
    flags: ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
        0: bipush        12
        2: putstatic    #8                  // Field variableInt:I
        5: ldc          #9                  // String 我很好
        7: putstatic    #10                // Field variableString:Ljava/lang/String;
        10: return
      LineNumberTable:
        line 5: 0
        line 6: 5
}

class file content items

magic (magic numbers): class file nibbles before, always starts with the class file 0xCAFEBABE, as a class file identification.

minor_version / major_version major and minor version number, JVM based on the version number to determine how to load class file

constant_pool_count / constant_pool: constant pool

  Each constant pool entry identifier, starting from a (length of a byte). Identifying the location of the type specified time constant.

  Each sign has a corresponding table, the table name is the symbol name with "_info" That constant_utf8_info

access_flage: access flag to indicate class file defines a class or interface, and a class or interface modifiers.

this_class: constant_class_info index is pointing to the constant pool.

super_class: it is a constant pool index points.

interfaces / fields / methods / attribute: index also points to the constant pool.

XXX_count: that is, the number corresponding.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159587.htm