java高分局のclassファイル解析

32233 ワード

java高分局のclassファイル解析
classファイルの構造はここでは言いません.インターネットには多くの資料があります.「Java仮想マシンJVMの高級特性と最適な実践を深く理解する」も見られます.
ここではjava言語を使って、自分のクラスのファイル内容を解析します.コードはここに全部貼り付けられないので、ダウンロードチャンネルにアップロードしました.コードのダウンロード先:http://download.csdn.net/detail/maosijunzi/8644009
ソース
public class Test<T>{
    private int num;
    private final static int numstatic = 100;
    @Deprecated
    public static void method1(int param1,String param2)throws Exception{
        int i = 10;
        try{    
            i++;
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            i++;
        }
        System.out.println(i);
    }
    public void method2(T t){
        System.out.println(t);
    }
    public static void main(String ... args)throws Exception{
        method1(1,"aaa");

        Test<String> t = new Test<String>();
        t.method2("hello ");
    }
    private static class User{
        private String name;
        public String getName(){
            return this.name;
        }
    }
}
解析結果
  :CA FE BA BE     :0003485     1-85
1  tag=10 classInfoIndex=13 nameAndTypeIndex=62
2  tag=7 index=63
3  tag=10 classInfoIndex=2 nameAndTypeIndex=64
4  tag=9 classInfoIndex=65 nameAndTypeIndex=66
5  tag=10 classInfoIndex=67 nameAndTypeIndex=68
6  tag=10 classInfoIndex=67 nameAndTypeIndex=69
7  tag=8 index=70
8  tag=10 classInfoIndex=9 nameAndTypeIndex=71
9  tag=7 index=72
10  tag=10 classInfoIndex=9 nameAndTypeIndex=62
11  tag=8 index=73
12  tag=10 classInfoIndex=9 nameAndTypeIndex=74
13  tag=7 index=75
14  tag=7 index=76
15  tag=1  length=4 content=User
16  tag=1  length=12 content=InnerClasses
17  tag=1  length=3 content=num
18  tag=1  length=1 content=I
19  tag=1  length=9 content=numstatic
20  tag=1  length=13 content=ConstantValue
21  tag=3 num=100
22  tag=1  length=6 content=<init>
23  tag=1  length=3 content=()V
24  tag=1  length=4 content=Code
25  tag=1  length=15 content=LineNumberTable
26  tag=1  length=18 content=LocalVariableTable
27  tag=1  length=4 content=this
28  tag=1  length=6 content=LTest;
29  tag=1  length=22 content=LocalVariableTypeTable
30  tag=1  length=11 content=LTest<TT;>;
31  tag=1  length=7 content=method1
32  tag=1  length=22 content=(ILjava/lang/String;)V
33  tag=1  length=1 content=e
34  tag=1  length=21 content=Ljava/lang/Exception;
35  tag=1  length=6 content=param1
36  tag=1  length=6 content=param2
37  tag=1  length=18 content=Ljava/lang/String;
38  tag=1  length=1 content=i
39  tag=1  length=13 content=StackMapTable
40  tag=7 index=77
41  tag=7 index=63
42  tag=7 index=78
43  tag=1  length=10 content=Exceptions
44  tag=1  length=10 content=Deprecated
45  tag=1  length=25 content=RuntimeVisibleAnnotations
46  tag=1  length=22 content=Ljava/lang/Deprecated;
47  tag=1  length=7 content=method2
48  tag=1  length=21 content=(Ljava/lang/Object;)V
49  tag=1  length=1 content=t
50  tag=1  length=18 content=Ljava/lang/Object;
51  tag=1  length=3 content=TT;
52  tag=1  length=9 content=Signature
53  tag=1  length=6 content=(TT;)V
54  tag=1  length=4 content=main
55  tag=1  length=22 content=([Ljava/lang/String;)V
56  tag=1  length=4 content=args
57  tag=1  length=19 content=[Ljava/lang/String;
58  tag=1  length=26 content=LTest<Ljava/lang/String;>;
59  tag=1  length=40 content=<T:Ljava/lang/Object;>Ljava/lang/Object;
60  tag=1  length=10 content=SourceFile
61  tag=1  length=9 content=Test.java
62  tag=12 nameIndex=22 descIndex=23
63  tag=1  length=19 content=java/lang/Exception
64  tag=12 nameIndex=79 descIndex=23
65  tag=7 index=80
66  tag=12 nameIndex=81 descIndex=82
67  tag=7 index=83
68  tag=12 nameIndex=84 descIndex=85
69  tag=12 nameIndex=84 descIndex=48
70  tag=1  length=3 content=aaa
71  tag=12 nameIndex=31 descIndex=32
72  tag=1  length=4 content=Test
73  tag=1  length=6 content=hello 
74  tag=12 nameIndex=47 descIndex=48
75  tag=1  length=16 content=java/lang/Object
76  tag=1  length=9 content=Test$User
77  tag=1  length=16 content=java/lang/String
78  tag=1  length=19 content=java/lang/Throwable
79  tag=1  length=15 content=printStackTrace
80  tag=1  length=16 content=java/lang/System
81  tag=1  length=3 content=out
82  tag=1  length=21 content=Ljava/io/PrintStream;
83  tag=1  length=19 content=java/io/PrintStream
84  tag=1  length=7 content=println
85  tag=1  length=4 content=(I)V
    :33 ACC_PUBLIC,  ACC_SUPER, 
  :Test   :java/lang/Object       :02
      : ACC_PRIVATE,   nameIndex=17 descIndex=18 attributesCount=0
      : ACC_PRIVATE,  ACC_STATIC,  ACC_FINAL,   nameIndex=19 descIndex=18 attributesCount=1
Attribute_constantsValue:   constantsValueIndex=214
      : ACC_PUBLIC  nameIndex=22 descIndex=23 attributesCount=1
maxStack=1  maxLocal=1
codeLength=5
code[0]=aload_0
code[1]=invokespecial
code[2]=nop
code[3]=aconst_null
code[4]=return0
Attribute_lineNumberTable:        =0      =1
Attribute_lineNumberTable:        =4      =31
Attribute_localvariabletable:   localvariabletableLength=1 startPc=0 length=5 nameIndex=27 descIndex=28 index=0
Attribute_localvariabletypetable:   localvariabletableLength=1 startPc=0 length=5 nameIndex=27 signature=30 index=0
      : ACC_PUBLIC ACC_STATIC  nameIndex=31 descIndex=32 attributesCount=4
maxStack=2  maxLocal=5
codeLength=39
code[0]=bipush
code[1]=lconst_1
code[2]=istore_2
code[3]=iinc
code[4]=iconst_m1
code[5]=aconst_null
code[6]=iinc
code[7]=iconst_m1
code[8]=aconst_null
code[9]=go_to
code[10]=nop
code[11]=lload
code[12]=astore_3
code[13]=aload_3
code[14]=invokevirtual
code[15]=nop
code[16]=iconst_0
code[17]=iinc
code[18]=iconst_m1
code[19]=aconst_null
code[20]=go_to
code[21]=nop
code[22]=fconst_0
code[23]=astore
code[24]=iconst_1
code[25]=iinc
code[26]=iconst_m1
code[27]=aconst_null
code[28]=aload
code[29]=iconst_1
code[30]=athrow
code[31]=getstatic
code[32]=nop
code[33]=iconst_1
code[34]=iload_2
code[35]=invokevirtual
code[36]=nop
code[37]=iconst_2
code[38]=return0
  : startPc=3 endPc=6  handlePc=12 catchType=2
  : startPc=3 endPc=6  handlePc=23 catchType=0
  : startPc=12 endPc=17  handlePc=23 catchType=0
  : startPc=23 endPc=25  handlePc=23 catchType=0
Attribute_lineNumberTable:        =0      =7
Attribute_lineNumberTable:        =3      =9
Attribute_lineNumberTable:        =6      =13
Attribute_lineNumberTable:        =9      =14
Attribute_lineNumberTable:        =12      =10
Attribute_lineNumberTable:        =13      =11
Attribute_lineNumberTable:        =17      =13
Attribute_lineNumberTable:        =20      =14
Attribute_lineNumberTable:        =23      =13
Attribute_lineNumberTable:        =31      =15
Attribute_lineNumberTable:        =38      =16
Attribute_localvariabletable:   localvariabletableLength=4 startPc=13 length=4 nameIndex=33 descIndex=34 index=3
Attribute_localvariabletable:   localvariabletableLength=4 startPc=0 length=39 nameIndex=35 descIndex=18 index=0
Attribute_localvariabletable:   localvariabletableLength=4 startPc=0 length=39 nameIndex=36 descIndex=37 index=1
Attribute_localvariabletable:   localvariabletableLength=4 startPc=3 length=36 nameIndex=38 descIndex=18 index=2
numOfEntries=3
 offset_delta12
integer_variable_info
object_variable_info index=40
integer_variable_info
object_variable_info index=41
object_variable_info index=42
Attribute_exceptions: numOfExceptions=1  exceptionIndexTable=2
        
      : ACC_PUBLIC  nameIndex=47 descIndex=48 attributesCount=2
maxStack=2  maxLocal=2
codeLength=8
code[0]=getstatic
code[1]=nop
code[2]=iconst_1
code[3]=aload_1
code[4]=invokevirtual
code[5]=nop
code[6]=iconst_3
code[7]=return0
Attribute_lineNumberTable:        =0      =20
Attribute_lineNumberTable:        =7      =21
Attribute_localvariabletable:   localvariabletableLength=2 startPc=0 length=8 nameIndex=27 descIndex=28 index=0
Attribute_localvariabletable:   localvariabletableLength=2 startPc=0 length=8 nameIndex=49 descIndex=50 index=1
Attribute_localvariabletypetable:   localvariabletableLength=2 startPc=0 length=8 nameIndex=27 signature=30 index=0
Attribute_localvariabletypetable:   localvariabletableLength=2 startPc=0 length=8 nameIndex=49 signature=51 index=1
Attribute_signature:signatureIndex=53  = (TT;)V
      : ACC_PUBLIC ACC_STATIC ACC_TRANSIENT  nameIndex=54 descIndex=55 attributesCount=2
maxStack=2  maxLocal=2
codeLength=21
code[0]=iconst_1
code[1]=ldc
code[2]=iconst_4
code[3]=invokestatic
code[4]=nop
code[5]=iconst_5
code[6]=new0
code[7]=nop
code[8]=lconst_0
code[9]=dup
code[10]=invokespecial
code[11]=nop
code[12]=lconst_1
code[13]=astore_1
code[14]=aload_1
code[15]=ldc
code[16]=fconst_0
code[17]=invokevirtual
code[18]=nop
code[19]=fconst_1
code[20]=return0
Attribute_lineNumberTable:        =0      =24
Attribute_lineNumberTable:        =6      =26
Attribute_lineNumberTable:        =14      =27
Attribute_lineNumberTable:        =20      =28
Attribute_localvariabletable:   localvariabletableLength=2 startPc=0 length=21 nameIndex=56 descIndex=57 index=0
Attribute_localvariabletable:   localvariabletableLength=2 startPc=14 length=7 nameIndex=49 descIndex=28 index=1
Attribute_localvariabletypetable:   localvariabletableLength=1 startPc=14 length=7 nameIndex=49 signature=58 index=1
Attribute_exceptions: numOfExceptions=1  exceptionIndexTable=23
Attribute_signature:signatureIndex=59  = <T:Ljava/lang/Object;>Ljava/lang/Object;
sourceFileIndex=61
Attribute_innerClasses: numOfClasses=1  innerClassInfoIndex=14  outerClassInfoIndex=9  innerNameIndex=15 innerClassAccessFlags=10:ACC_PRIVATE,:ACC_STATIC,