zoukankan      html  css  js  c++  java
  • java class 文件解析

    参考下面两个文章对一个class文件进行解析:

    http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.6

    http://blog.163.com/hfut_quyouhu/blog/static/7847183520127214559314/

    java的源代码如下:

     1 package test.java.abs;
     2 
     3 public class Abs {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10 
    11         int a = -2;
    12         System.out.println(abs(a));
    13     }
    14     
    15     public static int abs(int i){
    16         return ~i+1;
    17     }
    18 
    19 }

    编译之后对class 文件的分析结果如下:

    CA FE BA BE ----> magic
    00 00 -----> minor version
    00 32 -----> major version
    00 27 -----> constant pool count(39)

    -------------------constant pool 1---------------
    07 ----->CONSTANT_Class
    00 02
    -------------------constant pool 2---------------
    01
    00 11
    74 65 73 74 2F 6A 61 76 61 2F 61 62 73 2F 41 62
    73
    test/java/abs/Abs
    -------------------constant pool 3---------------
    07
    00 04
    -------------------constant pool 4---------------
    01
    00 10
    6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74
    java/lang/Object
    -------------------constant pool 5---------------
    01
    00 06
    3C 69 6E 69 74 3E
    <init>
    -------------------constant pool 6---------------
    01
    00 03
    28 29 56
    ()V
    -------------------constant pool 7---------------
    01
    00 04
    43 6F 64 65
    Code
    -------------------constant pool 8---------------
    0A
    00 03
    00 09
    -------------------constant pool 9---------------
    0C
    00 05
    00 06
    -------------------constant pool 10---------------
    01
    00 0F
    4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65
    LineNumberTable
    -------------------constant pool 11---------------
    01
    00 12
    4C 6F 63 61 6C 56 61 72 69 61 62 6C 65 54 61 62 6C 65
    LocalVariableTable
    -------------------constant pool 12---------------
    01
    00 04
    74 68 69 73
    this
    -------------------constant pool 13---------------
    01
    00 13
    4C 74 65 73 74 2F 6A 61 76 61 2F 61 62 73 2F 41 62 73 3B
    Ltest/java/abs/Abs;
    -------------------constant pool 14---------------
    01
    00 04
    6D 61 69 6E
    main
    -------------------constant pool 15---------------
    01
    00 16
    28 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56
    ([Ljava/lang/String;)V
    -------------------constant pool 16---------------
    09
    00 11
    00 13
    -------------------constant pool 17---------------
    07
    00 12
    -------------------constant pool 18---------------
    01
    00 10
    6A 61 76 61 2F 6C 61 6E 67 2F 53 79 73 74 65 6D
    java/lang/System
    -------------------constant pool 19---------------
    0C
    00 14
    00 15
    -------------------constant pool 20---------------
    01
    00 03
    6F 75 74
    out
    -------------------constant pool 21---------------
    01
    00 15
    4C 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 3B
    Ljava/io/PrintStream;
    -------------------constant pool 22---------------
    0A
    00 01
    00 17
    -------------------constant pool 23---------------
    0C
    00 18
    00 19
    -------------------constant pool 24---------------
    01
    00 03
    61 62 73
    abs
    -------------------constant pool 25---------------
    01
    00 04
    28 49 29 49
    (I)I
    -------------------constant pool 26---------------
    0A
    00 1B
    00 1D
    -------------------constant pool 27---------------
    07
    00 1C
    -------------------constant pool 28---------------
    01
    00 13
    6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D
    java/io/PrintStream
    -------------------constant pool 29---------------
    0C
    00 1E
    00 1F
    -------------------constant pool 30---------------
    01
    00 07
    70 72 69 6E 74 6C 6E
    println
    -------------------constant pool 31---------------
    01
    00 04
    28 49 29 56
    (I)V
    -------------------constant pool 32---------------
    01
    00 04
    61 72 67 73
    args
    -------------------constant pool 33---------------
    01
    00 13
    5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B
    [Ljava/lang/String;
    -------------------constant pool 34---------------
    01
    00 01
    61
    a
    -------------------constant pool 35---------------
    01
    00 01
    49
    I
    -------------------constant pool 36---------------
    01
    00 01
    69
    i
    -------------------constant pool 37---------------
    01
    00 0A
    53 6F 75 72 63 65 46 69 6C 65
    SourceFile
    -------------------constant pool 38---------------
    01
    00 08
    41 62 73 2E 6A 61 76 61
    Abs.java

    00 21 ------> ACC_PUBLIC,ACC_SUPER
    00 01 ------> constant pool index 1
    00 03 ------> super class
    00 00 ------> interface count
    00 00 ------> field count
    00 03 ------> method count


    -----------------method 1----------------------
    00 01 ------> ACC_PUBLIC
    00 05 ------> name_index <init>
    00 06 ------> descriptor_index ()V
    00 01 ------> attributes_count
            ----------------attribute 1 of method 1 --------------
            00 07 ------> attribute_name_index Code
            00 00 00 2F ------> attribute_length 47
            00 01 ------> max_stack
            00 01 ------> max_locals
            00 00 00 05 ------> code_length
            2A B7 00 08 B1
                    -------------------------instructions----------------------
                    2A aload_0 this
                    B7 invokespecial 00 08 Object.init
                    B1 return
            00 00 ------> exception_table_length
            00 02 ------> attributes_count
                    ---------------------sub attribute 1-------------
                    00 0A ------> attribute_name_index LineNumberTable
                    00 00 00 06 ------> attribute_length
                    00 01 ------> line_number_table_length
                    00 00 ------> start_pc
                    00 03 ------> line_number
                    ---------------------sub attribute 2-------------
                    00 0B ------> attribute_name_index LocalVariableTable
                    00 00 00 0C ------> attribute_length
                    00 01 ------> local_variable_table_length
                    00 00 ------> start_pc
                    00 05 ------> length
                    00 0C ------> name_index this
                    00 0D ------> descriptor_index Ltest/java/abs/Abs;
                    00 00 ------> index

    -----------------method 2----------------------
    00 09 -------> ACC_PUBLIC,ACC_STATIC
    00 0E -------> main
    00 0F -------> ([Ljava/lang/String;)V
    00 01 -------> attributes_count
            --------------------attribute 1 of method 2 -----------------------
            00 07 ------> attribute_name_index Code
            00 00 00 4A ------> attribute_length 74
            00 02 ------> max_stack
            00 02 ------> max_locals
            00 00 00 0E ------> code_length
            10 FE 3C B2 00 10 1B B8 00 16 B6 00 1A B1
                     -------------------------instructions----------------------
                    10 bipush FE(-2)
                    3C istore_1
                    B2 getstatic 00 10 System.out
                    1B iload_1
                    B8 invokestatic 00 16 abs()
                    B6 invokevirtual 00 1A println()
                    B1 return
             00 00 ------> exception_table_length
             00 02 ------> attributes_count
                     ----------------------sub attribute 1 -----------------------
                     00 0A ----------> attribute_name_index LineNumberTable
                     00 00 00 0E ----------> attribute_length
                     00 03 ----------> line_number_table_length
                     00 00 ----------> start_pc
                     00 0B ----------> line_number
                     00 03 ----------> start_pc
                     00 0C ----------> line_number
                     00 0D ----------> start_pc
                     00 0D ----------> line_number
                     ----------------------sub attribute 2 -----------------------
                     00 0B ----------> attribute_name_index LocalVariableTable
                     00 00 00 16 ----------> attribute_length
                     00 02 ----------> local_variable_table_length
                     00 00 ----------> start_pc
                     00 0E ----------> length
                     00 20 ----------> name_index args
                     00 21 ----------> descriptor_index [Ljava/lang/String;
                     00 00 ----------> index 0
                     00 03 ----------> start_pc
                     00 0B ----------> length
                     00 22 ----------> name_index a
                     00 23 ----------> descriptor_index I
                     00 01 ----------> index 1

    -----------------method 3----------------------
    00 09 -------> ACC_PUBLIC,ACC_STATIC
    00 18 -------> abs
    00 19 -------> (I)I
    00 01 -------> attribute_count

    -------------------------attribute 1 of method 3 --------------------
    00 07 ------> attribute_name_index Code
    00 00 00 30 ------> attribute_lenght 48
    00 02 ------> max_stack
    00 01 ------> max_locals
    00 00 00 06 ------> code_length
    1A 02 82 04 60 AC

    -------------------------instructions----------------------
    1A iload_0
    02 iconst_m1
    82 ixor
    04 iconst_1
    60 iadd
    AC ireturn

    00 00 ------> exception_table_length 

    00 02 ------> attributes_count

    ------------------------sub attribute 1-----------------------
    00 0A ----------> attribute_name_index LineNumberTable
    00 00 00 06 ----------> attribute_length
    00 01 ----------> line_number_table_length
    00 00 ----------> start_pc
    00 10 ----------> line_number
    ------------------------sub attribute 2-----------------------
    00 0B ----------> attribute_name_index LocalVariableTable
    00 00 00 0C ----------> attribute_length
    00 01 ----------> local_varialbe_table_length
    00 00 ----------> start_pc
    00 06 ----------> length
    00 24 ----------> name_index i
    00 23 ----------> descriptor_index I
    00 00 ----------> index


    ------------------------ class file attribute ----------------------------------
    00 01 ---------> attribute count
    00 25 ---------> attribute_name_index SourceFile
    00 00 00 02 ---------> attribute_length
    00 26 ---------> sourcefile_index Abs.java

  • 相关阅读:
    enyo学习之enyo.Object类
    java.lang.Object
    Nginx转发匹配规则
    工具类Redis工具类
    Redis从0到精通Redis.conf详解
    工具类类型转换器
    Docker入门到精通
    Redis从0到精通Jedis
    工具类字符集工具类
    Spock高质量单元测试实操篇
  • 原文地址:https://www.cnblogs.com/cruze/p/3767519.html
Copyright © 2011-2022 走看看