Class文件的结构及解析

7 minute read

ClassFile 伪代码结构

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

示例文件

Main.java

package io.github.worldwonderer;

public class Main {

    private static String a = "hello, world";

    public static void main(String[] args) {
	    // write your code here
        System.out.println();
    }
}

Main.class

00000000:  cafe babe 0000 0034 0021 0a00 0700 1309 0014 0015 0a00 1600  :.......4.!..............
00000018:  1708 0018 0900 0600 1907 001a 0700 1b01 0001 6101 0012 4c6a  :..................a...Lj
00000030:  6176 612f 6c61 6e67 2f53 7472 696e 673b 0100 063c 696e 6974  :ava/lang/String;...<init
00000048:  3e01 0003 2829 5601 0004 436f 6465 0100 0f4c 696e 654e 756d  :>...()V...Code...LineNum
00000060:  6265 7254 6162 6c65 0100 046d 6169 6e01 0016 285b 4c6a 6176  :berTable...main...([Ljav
00000078:  612f 6c61 6e67 2f53 7472 696e 673b 2956 0100 083c 636c 696e  :a/lang/String;)V...<clin
00000090:  6974 3e01 000a 536f 7572 6365 4669 6c65 0100 094d 6169 6e2e  :it>...SourceFile...Main.
000000a8:  6a61 7661 0c00 0a00 0b07 001c 0c00 1d00 1e07 001f 0c00 2000  :java.................. .
000000c0:  0b01 000c 6865 6c6c 6f2c 2077 6f72 6c64 0c00 0800 0901 001c  :....hello, world........
000000d8:  696f 2f67 6974 6875 622f 776f 726c 6477 6f6e 6465 7265 722f  :io/github/worldwonderer/
000000f0:  4d61 696e 0100 106a 6176 612f 6c61 6e67 2f4f 626a 6563 7401  :Main...java/lang/Object.
00000108:  0010 6a61 7661 2f6c 616e 672f 5379 7374 656d 0100 036f 7574  :..java/lang/System...out
00000120:  0100 154c 6a61 7661 2f69 6f2f 5072 696e 7453 7472 6561 6d3b  :...Ljava/io/PrintStream;
00000138:  0100 136a 6176 612f 696f 2f50 7269 6e74 5374 7265 616d 0100  :...java/io/PrintStream..
00000150:  0770 7269 6e74 6c6e 0021 0006 0007 0000 0001 000a 0008 0009  :.println.!..............
00000168:  0000 0003 0001 000a 000b 0001 000c 0000 001d 0001 0001 0000  :........................
00000180:  0005 2ab7 0001 b100 0000 0100 0d00 0000 0600 0100 0000 0300  :..*.....................
00000198:  0900 0e00 0f00 0100 0c00 0000 2300 0100 0100 0000 07b2 0002  :............#...........
000001b0:  b600 03b1 0000 0001 000d 0000 000a 0002 0000 0009 0006 000a  :........................
000001c8:  0008 0010 000b 0001 000c 0000 001e 0001 0000 0000 0006 1204  :........................
000001e0:  b300 05b1 0000 0001 000d 0000 0006 0001 0000 0005 0001 0011  :........................
000001f8:  0000 0002 0012                                               :......

Python解析脚本

read_class.py

References

The class File Format

Updated: