首页 > 技术文章 > Class 类文件结构

mervyn-hao 2021-08-13 11:34 原文

Class 类文件结构

类型 名称 数量 描述
u4 magic 1 魔数,值为:0xCAFEBABY
u2 minor_version 1 次版本号
u2 magor_version 1 主版本号
u2 constant_pool_count 1 常量池容量,从1开始
cp_info constant_pool constant_pool_count - 1 字面量和符号引用
u2 access_flags 1 访问标志
u2 this_class 1 类索引
u2 super_class 1 父类索引
u2 interfaces_count 1 接口索引数量
u2 interfaces interfaces_count 接口索引集合
u2 fields_count 1 字段表数量
field_info fields fields_count 字段表集合
u2 methods_count 1 方法表数量
method_info methods methods_count 方法表集合
u2 attributes_count 1 属性表数量
attribute_info attributes attributes_count 属性表集合
public class Father {
}

public class Test {
	private int m;
  private static final String DEFAULT = "Hello";

	public int inc() {
		return m + 1;
	}
}

javap -v -p Test

Classfile /Users/mervyn/Documents/Test.class
  Last modified Aug 10, 2021; size 356 bytes
  MD5 checksum 61c1cacb68c1d46c7964400ddb638849
  Compiled from "Test.java"
public class Test extends Father implements java.lang.Cloneable
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
  
/* 常量池 */
Constant pool:
   #1 = Methodref          #4.#20         // Father."<init>":()V
   #2 = Fieldref           #3.#21         // Test.m:I
   #3 = Class              #22            // Test
   #4 = Class              #23            // Father
   #5 = Class              #24            // java/lang/Cloneable
   #6 = Utf8               m
   #7 = Utf8               I
   #8 = Utf8               DEFAULT
   #9 = Utf8               Ljava/lang/String;
  #10 = Utf8               ConstantValue
  #11 = String             #25            // Hello
  #12 = Utf8               <init>
  #13 = Utf8               ()V
  #14 = Utf8               Code
  #15 = Utf8               LineNumberTable
  #16 = Utf8               inc
  #17 = Utf8               ()I
  #18 = Utf8               SourceFile
  #19 = Utf8               Test.java
  #20 = NameAndType        #12:#13        // "<init>":()V
  #21 = NameAndType        #6:#7          // m:I
  #22 = Utf8               Test
  #23 = Utf8               Father
  #24 = Utf8               java/lang/Cloneable
  #25 = Utf8               Hello
{
	/* 字段表集合 */
  private int m;
    descriptor: I
    flags: ACC_PRIVATE

  private static final java.lang.String DEFAULT;
    descriptor: Ljava/lang/String;
    flags: ACC_PRIVATE, ACC_STATIC, ACC_FINAL
    ConstantValue: String Hello

	/* 方法表集合 */
  public Test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method Father."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public int inc();
    descriptor: ()I
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: getfield      #2                  // Field m:I
         4: iconst_1
         5: iadd
         6: ireturn
      LineNumberTable:
        line 5: 0
}
/* 属性表集合 */
SourceFile: "Test.java"

推荐阅读