Java class文件信息

在谈论JVM-运行时数据区时,我们遇到了一个方法区,关于方法区的信息都来自于class,在JDK的工具中,有一个javap命令可以用来显示class文件的信息。

查看class文件内容的命令如下:

javap -v XXX.clsss
 
 
  • 1
  • 1

在这里以TestClass.Java示例

/**
 * 
 */
package com.hx;

import com.hx.vo.BaseParam;

/**
 * @author MichaelKoo
 * 
 *         2017-7-12
 */
public class TestClass extends BaseParam implements Runnable {
    private String name;
    private static final int AGE = 18;

    public void run() {
        name = "MichaelKoo," + AGE;

        try {
            CsdnUtil.getContent(getClass());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

编译成TestClass.class之后,使用javap命令打开信息如下:

Classfile /I:/adt_eclipse_work/HuanXinTest/bin/com/hx/TestClass.class
  Last modified 2017-7-12; size 785 bytes
  MD5 checksum 92698cfe5ce5c5322fa75fe4c2a54b2e
  Compiled from "TestClass.java"

public class com.hx.TestClass extends com.hx.vo.BaseParam implements java.lang.Runnable
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Class              #2             // com/hx/TestClass
   #2 = Utf8               com/hx/TestClass
   #3 = Class              #4             // com/hx/vo/BaseParam
   #4 = Utf8               com/hx/vo/BaseParam
   #5 = Class              #6             // java/lang/Runnable
   #6 = Utf8               java/lang/Runnable
   #7 = Utf8               name
   #8 = Utf8               Ljava/lang/String;
   #9 = Utf8               AGE
  #10 = Utf8               I
  #11 = Utf8               ConstantValue
  #12 = Integer            18
  #13 = Utf8               <init>
  #14 = Utf8               ()V
  #15 = Utf8               Code
  #16 = Methodref          #3.#17         // com/hx/vo/BaseParam."<init>":()V
  #17 = NameAndType        #13:#14        // "<init>":()V
  #18 = Utf8               LineNumberTable
  #19 = Utf8               LocalVariableTable
  #20 = Utf8               this
  #21 = Utf8               Lcom/hx/TestClass;
  #22 = Utf8               run
  #23 = String             #24            // MichaelKoo,18
  #24 = Utf8               MichaelKoo,18
  #25 = Fieldref           #1.#26         // com/hx/TestClass.name:Ljava/lang/String;
  #26 = NameAndType        #7:#8          // name:Ljava/lang/String;
  #27 = Methodref          #28.#30        // java/lang/Object.getClass:()Ljava/lang/Class;
  #28 = Class              #29            // java/lang/Object
  #29 = Utf8               java/lang/Object
  #30 = NameAndType        #31:#32        // getClass:()Ljava/lang/Class;
  #31 = Utf8               getClass
  #32 = Utf8               ()Ljava/lang/Class;
  #33 = Methodref          #34.#36        // com/hx/CsdnUtil.getContent:(Ljava/lang/Class;)Ljava/lang/String;
  #34 = Class              #35            // com/hx/CsdnUtil
  #35 = Utf8               com/hx/CsdnUtil
  #36 = NameAndType        #37:#38        // getContent:(Ljava/lang/Class;)Ljava/lang/String;
  #37 = Utf8               getContent
  #38 = Utf8               (Ljava/lang/Class;)Ljava/lang/String;
  #39 = Methodref          #40.#42        // java/lang/Exception.printStackTrace:()V
  #40 = Class              #41            // java/lang/Exception
  #41 = Utf8               java/lang/Exception
  #42 = NameAndType        #43:#14        // printStackTrace:()V
  #43 = Utf8               printStackTrace
  #44 = Utf8               e
  #45 = Utf8               Ljava/lang/Exception;
  #46 = Utf8               StackMapTable
  #47 = Utf8               SourceFile
  #48 = Utf8               TestClass.java
{
  public com.hx.TestClass();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #16                 // Method com/hx/vo/BaseParam."<init>":()V
         4: return
      LineNumberTable:
        line 13: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/hx/TestClass;

  public void run();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=1
         0: aload_0
         1: ldc           #23                 // String MichaelKoo,18
         3: putfield      #25                 // Field name:Ljava/lang/String;
         6: aload_0
         7: invokevirtual #27                 // Method java/lang/Object.getClass:()Ljava/lang/Class;
        10: invokestatic  #33                 // Method com/hx/CsdnUtil.getContent:(Ljava/lang/Class;)Ljava/lang/String;
        13: pop
        14: goto          22
        17: astore_1
        18: aload_1
        19: invokevirtual #39                 // Method java/lang/Exception.printStackTrace:()V
        22: return
      Exception table:
         from    to  target type
             6    14    17   Class java/lang/Exception
      LineNumberTable:
        line 18: 0
        line 21: 6
        line 22: 14
        line 23: 18
        line 25: 22
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      23     0  this   Lcom/hx/TestClass;
           18       4     1     e   Ljava/lang/Exception;
      StackMapTable: number_of_entries = 2
        frame_type = 81 /* same_locals_1_stack_item */
          stack = [ class java/lang/Exception ]
        frame_type = 4 /* same */
}
SourceFile: "TestClass.java"

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

其中有几个需要关注的节点: 
- 类的信息,在关键字’Constant pool:’之前就属于类的基本信息,从中可以看出,类的修饰符(public )、类的完整名称(com.hx.TestClass)、类的父类完整名称(com.hx.vo.BaseParam)、类的接口信息(java.lang.Runnable) 
- 常量池,在关键字’Constant pool:’之后,第一个’{‘之前的内容就属于常量池信息, 

- 在’{‘之后,’}’之前就是类的方法信息,从中可以看出:方法的修饰符、方法的返回值类型、方法名称、方法的异常表、方法的局部变量表等信息

转载地址:http://blog.csdn.net/android_app/article/details/75043072

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值