一个class文件分析示例

  /**
 * Created by walulu on 2017/1/21.
 */
public class ForJavaP {

    private int i = 10;
    static String s = "a";
    final byte b = 10;

    public static void main(String ... args) throws InterruptedException {
        System.out.println(s);
        Thread.sleep(100);
    }

}
nfhy@ubuntu:/tmp/java$ od -t x1 ForJavaP.class
0000000 ca fe ba be 00 00 00 34 00 37 0a 00 0c 00 20 09
0000020 00 0b 00 21 09 00 0b 00 22 09 00 23 00 24 09 00
0000040 0b 00 25 0a 00 26 00 27 05 00 00 00 00 00 00 00
0000060 64 0a 00 28 00 29 08 00 2a 07 00 2b 07 00 2c 01
0000100 00 01 69 01 00 01 49 01 00 01 73 01 00 12 4c 6a
0000120 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b
0000140 01 00 01 62 01 00 01 42 01 00 0d 43 6f 6e 73 74
0000160 61 6e 74 56 61 6c 75 65 03 00 00 00 0a 01 00 06
0000200 3c 69 6e 69 74 3e 01 00 03 28 29 56 01 00 04 43
0000220 6f 64 65 01 00 0f 4c 69 6e 65 4e 75 6d 62 65 72
0000240 54 61 62 6c 65 01 00 04 6d 61 69 6e 01 00 16 28
0000260 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69
0000300 6e 67 3b 29 56 01 00 0a 45 78 63 65 70 74 69 6f
0000320 6e 73 07 00 2d 01 00 08 3c 63 6c 69 6e 69 74 3e
0000340 01 00 0a 53 6f 75 72 63 65 46 69 6c 65 01 00 0d
0000360 46 6f 72 4a 61 76 61 50 2e 6a 61 76 61 0c 00 15
0000400 00 16 0c 00 0d 00 0e 0c 00 11 00 12 07 00 2e 0c
0000420 00 2f 00 30 0c 00 0f 00 10 07 00 31 0c 00 32 00
0000440 33 07 00 34 0c 00 35 00 36 01 00 01 61 01 00 08
0000460 46 6f 72 4a 61 76 61 50 01 00 10 6a 61 76 61 2f
0000500 6c 61 6e 67 2f 4f 62 6a 65 63 74 01 00 1e 6a 61
0000520 76 61 2f 6c 61 6e 67 2f 49 6e 74 65 72 72 75 70
0000540 74 65 64 45 78 63 65 70 74 69 6f 6e 01 00 10 6a
0000560 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01
0000600 00 03 6f 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f
0000620 2f 50 72 69 6e 74 53 74 72 65 61 6d 3b 01 00 13
0000640 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72
0000660 65 61 6d 01 00 07 70 72 69 6e 74 6c 6e 01 00 15
0000700 28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69
0000720 6e 67 3b 29 56 01 00 10 6a 61 76 61 2f 6c 61 6e
0000740 67 2f 54 68 72 65 61 64 01 00 05 73 6c 65 65 70
0000760 01 00 04 28 4a 29 56 00 20 00 0b 00 0c 00 00 00
0001000 03 00 02 00 0d 00 0e 00 00 00 08 00 0f 00 10 00
0001020 00 00 10 00 11 00 12 00 01 00 13 00 00 00 02 00
0001040 14 00 03 00 00 00 15 00 16 00 01 00 17 00 00 00
0001060 31 00 02 00 01 00 00 00 11 2a b7 00 01 2a 10 0a
0001100 b5 00 02 2a 10 0a b5 00 03 b1 00 00 00 01 00 18
0001120 00 00 00 0e 00 03 00 00 00 01 00 04 00 03 00 0a
0001140 00 05 00 89 00 19 00 1a 00 02 00 17 00 00 00 30
0001160 00 02 00 01 00 00 00 10 b2 00 04 b2 00 05 b6 00
0001200 06 14 00 07 b8 00 09 b1 00 00 00 01 00 18 00 00
0001220 00 0e 00 03 00 00 00 08 00 09 00 09 00 0f 00 0a
0001240 00 1b 00 00 00 04 00 01 00 1c 00 08 00 1d 00 16
0001260 00 01 00 17 00 00 00 1e 00 01 00 00 00 00 00 06
0001300 12 0a b3 00 05 b1 00 00 00 01 00 18 00 00 00 06
0001320 00 01 00 00 00 04 00 01 00 1e 00 00 00 02 00 1f
0001340
  • magic num : ca fe ba be
  • minor version: 00 00 =00
  • major version: 00 34 = 52
    • version =52.00
      
  • constant pool count 00 37 =55
    • 1 0a 00 0c 00 20 mr(methodRef) c12.c32 java.lang.Object.()V
    • 2 09 00 0b 00 21 fr c11.c33 ForJavaP.i:I
    • 3 09 00 0b 00 22 fr c11.c34 ForJavaP.b:B
    • 4 09 00 23 00 24 fr c35.c36 java.lang.System.out:java.io.printStream
    • 5 09 00 0b 00 25 fr c11.c37 ForJavaP.s:S
    • 6 0a 00 26 00 27 mr c38.c39 java.io.PrintStream:println(Ljava.lang.String:)V
    • 7 05 00 00 00 00 00 00 00 64 longValue 100
    • 9 0a 00 28 00 29 mr c40.c41 java.lang.Thread:sleep(J)V
    • 10 08 00 2a string c42 a
    • 11 07 00 2b class c43 ForJavaP
    • 12 07 00 2c class c44 java.lang.Object
    • 13 01 00 01 69 utf8 i
    • 14 01 00 01 49 utf8 I
    • 15 01 00 01 73 utf8 s
    • 16 01 00 12 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b Ljava.lang.String:
    • 17 01 00 01 62 utf8 b
    • 18 01 00 01 42 utf8 B
    • 19 01 00 0d 43 6f 6e 73 74 61 6e 74 56 61 6c 75 65 utf8 ConstantValue
    • 20 03 00 00 00 0a int 10
    • 21 01 00 06 3c 69 6e 69 74 3e utf8
    • 22 01 00 03 28 29 56 utf8 ()V
    • 23 01 00 04 43 6f 64 65 utf8 Code
    • 24 01 00 0f 4c 69 6e 65 4e 75 6d 62 65 72 54 61 62 6c 65 utf8 LineNumberTable
    • 25 01 00 04 6d 61 69 6e utf8 main
    • 26 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 utf8 ([Ljava.lang.String)V
    • 27 01 00 0a 45 78 63 65 70 74 69 6f 6e 73 utf8 Exceptions
    • 28 07 00 2d class c45 java.lang.InterruptException
    • 29 01 00 08 3c 63 6c 69 6e 69 74 3e utf8
    • 30 01 00 0a 53 6f 75 72 63 65 46 69 6c 65 utf8 sourceFile
    • 31 01 00 0d 46 6f 72 4a 61 76 61 50 2e 6a 61 76 61 utf8 ForJavaP.java
    • 32 0c 00 15 00 16 NaT c21.c22 ()V
    • 33 0c 00 0d 00 0e NaT c13.c14 i:I
    • 34 0c 00 11 00 12 NaT c17.c18 b:B
    • 35 07 00 2e class c46 java.lang.System
    • 36 0c 00 2f 00 30 NaT c47.c48 out:Ljava.io.printStream
    • 37 0c 00 0f 00 10 NaT c15.c16 s:Ljava.lang.String
    • 38 07 00 31 class c49 java.io.printStream
    • 39 0c 00 32 00 33 NaT c.50.c51 pringln:(Ljava.lang.String:)V
    • 40 07 00 34 class c52 java.lang.Thread
    • 41 0c 00 35 00 36 NaT c53.c54 sleep(J)V
    • 42 01 00 01 61 utf8 a
    • 43 01 00 08 46 6f 72 4a 61 76 61 50 ForJavaP
    • 44 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 java/lang/Object
    • 45 01 00 1e 6a 61 76 61 2f 6c 61 6e 67 2f 49 6e 74 65 72 72 75 70 74 65 64 45 78 63 65 70 74 69 6f 6e java/lang/InterruptException
    • 46 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d java/lang/System
    • 47 01 00 03 6f 75 74 out
    • 48 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:
    • 49 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
    • 50 01 00 07 70 72 69 6e 74 6c 6e println
    • 51 01 00 15 28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 29 56 (Ljava.lang.String:)V
    • 52 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 54 68 72 65 61 64 java/lang/Thread
    • 53 01 00 05 73 6c 65 65 70 sleep
    • 54 01 00 04 28 4a 29 56 (J)V
  • access flag 00 20 acc_super
  • this class 00 0b c11 ForJavaP
  • super class 00 0c java.lang.Object
  • interface count=0 00 00
  • field count=3 00 03
    • field1
      
      • access flag acc_private 00 02
      • name index c13 i 00 0d
      • descriptor index c14 I 00 0e
      • attribute count=0 00 00
    • field2
      
      • access flag acc_static 00 08
      • name index s 00 0f
      • descriptor index c16 Ljava.lang.String 00 10
      • attribute count=0 00 00
    • field3
      
      • access flag acc_private||acc_static 00 10
      • name index c17 b 00 11
      • descriptor index c18 B 00 12
      • attribute count =1 00 01
      • ConstantValue c13 00 13
      • length=2 00 00 00 02
      • c20 =10 00 14
  • method count=3 00 03
    • method1
      
      • access flag 00 00
      • name index 00 15
      • descriptor index ()V 00 16
      • attribute count 1 00 01
      • Code 00 17
      • length 49 00 00 00 31
      • max stack 2 00 02
      • max locals 1 00 01
      • code_length 17 00 00 00 11
      • code 2a b7 00 01 2a 10 0a b5 00 02 2a 10 0a b5 00 03 b1
      •     aload_0
        
      •     invokespecial
        
      •     nop
        
      •     aconst_null
        
      •     aload_0
        
      •     bipush
        
      •     lconst_1
        
      •     putfield
        
      •     nop
        
      •     iconst_m1
        
      •     aload_0
        
      •     bipush
        
      •     lconst_1
        
      •     putfield
        
      •     nop
        
      •     iconst_0
        
      •     return
        
      • exception table length=0 00 00
      • attribute count=1 00 01
      • LineNumberTable c24 00 18
      •   length=14 00 00 00 0e 
        
      •   lineNumberTableLength=3 00 03 
        
      •   lineNumberInfo1
        
      •   1:0 00 00 00 01 
        
      •   lineNumberInfo2
        
      •   3:4 00 04 00 03 
        
      •   lineNumberInfo3 
        
      •   5:10 00 0a 00 05
        
  • method2
    • access flag acc_plubic|acc_static|acc_varargs 00 89
    • name index c25 main 00 19
    • descriptor index ([Ljava.lang.String)V c26 00 1a
    • attribute count 2 00 02
    • attribute1
    •   Code 00 17 
      
    •   length 48 00 00 00 30
      
    •   max stack 2 00 02 
      
    •   max locals 1 00 01 
      
    •   code length 16 00 00 00 10 
      
    •   code b2 00 04 b2 00 05 b6 00 06 14 00 07 b8 00 09 b1
      
    •       getstatic
      
    •       nop
      
    •       iconst_1
      
    •       getstatic
      
    •       nop
      
    •       iconst_2
      
    •       invokevirtual
      
    •       nop
      
    •       iconst_3
      
    •       ldc2_w
      
    •       nop
      
    •       iconst_4
      
    •       invokestatic
      
    •       nop
      
    •       lconst_0
      
    •       return 
      
    •   exception_table_length 0 00 00 
      
    •   attribute count 1 00 01 
      
    •   LineNumberTable 00 18 
      
    •   length = 14 00 00 00 0e 
      
    •   lineNumberTableLength=3 00 03 
      
    •   lineNumberInfo1
      
    •   8:0 00 00 00 08 
      
    •   lineNumberInfo2
      
    •   9:9 00 09 00 09 
      
    •   lineNumberInfo3
      
    •   10:15 00 0f 00 0a
      
    • attribute2
    •   Exceptions 00 1b 
      
    •   length 4 00 00 00 04 
      
    •   number_of_exceptions=1 00 01 
      
    •   exception_index_table java.lang.InterruptException c28 00 1c 
      
  • method3
    • access flag acc_static 00 08
    • name index c29 00 1d
    • descriptor index ()V c22 00 16
    • attribute count 1 00 01
    • attribute1
    • Code 00 17
    • length 30 00 00 00 1e
    • max stack 1 00 01
    • max locals 0 00
    • code length 6 00 00 00 00 06
    • code 12 0a b3 00 05 b1
    •   ldc
      
    •   lconst_1
      
    •   putstatic
      
    •   nop
      
    •   iconst_2
      
    •   return 
      
    • exception_table_length 0 00 00
    • attribute count 1 00 01
    • LineNumberTable 00 18
    • length 6 00 00 00 06
    • lineNumberTableLength 1 00 01
    • lineNumberInfo1 0:4 00 00 00 04
  • attribute
    • count=1 00 01
    • attribute
    • sourceFile 00 1e
    • length 2 00 00 00 02
    • ForJavaP.java c31 00 1f

你可能感兴趣的:(一个class文件分析示例)