java高*之class文件解析

时间:2022-10-30 17:19:12

java高*之class文件解析

class文件的结构这里不再说了,网上有很多资料,也可以看《深入理解Java虚拟机 JVM高级特性与最佳实践》,里面对class文件结构的描述很详细。

这里我们主要使用java语言解析自身的class文件内容,因为代码不能全部贴在这里,所以上传到了下载频道。代码下载地址:
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  次版本:00  主版本:034
常量池大小:85 索引范围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 接口引用数量:0
字段长度:2
字段访问标识: ACC_PRIVATE, nameIndex=17 descIndex=18 attributesCount=0
字段访问标识: ACC_PRIVATE, ACC_STATIC, ACC_FINAL, nameIndex=19 descIndex=18 attributesCount=1
Attribute_constantsValue: constantsValueIndex=21
方法长度:4
方法访问标识: 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=2
属性表大小:3
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,