版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Class字节码中有两种数据类型:1. 字节数据直接量:这是基本的数据类型。共细分为u1、u2、u4、u8四种,分别代表连续的1个字节、2个字节、4个字节、8个字节组成的整体数据。2. 表:表是由多个基本数据或其他表,按照既定顺序组成的大的数据集合。表是有结构的,它的结构体现在,组成表的成分所在的位置和顺序都是已经严格定义好的。Class字节码总体结构如下:具体详解请参考我在这里要说明几个细节问题:1. 为什么说常量表的数量是constant_pool_count-1,且索引从1开始而不是0。其实根本原因在于,索引为0也是一个常量(保留常量),只不过它不存在常量表,这个常量就对应null值。因
2、此加上这个系统保留常量,常量个数共为constant_pool_count个,但是常量表数量要减1。2. 在常量池中,如果存在long型或double型字面量,它们会占用两个连续索引。比如:假设一个类中只有一个int型字面量1和一个double型字面量1(当然这种假设是不可能的,因为总会有类名字面量等),则常量池个数为3,而不是2。这正是因为double字面量占用了两个连续的索引。接下来,贴出一个小demo来展示如何读取字节码:ClassParser负责把握Class字节码整体结构的解析。packagecom.lixin;importjava.io.IOException;importjava
3、.io.InputStream;publicclassClassParserprivateInputStreamin;publicClassParser(InputStreamin)this.in=in;publicvoidparse()throwsIOException/魔数magicNumber();/主次版本号version();/常量池constantPool();/类或接口修饰符accessFlag();/继承关系(当前类、父类、父接口)inheritence();/字段集合fieldList();/方法集合methodList();/属性集合attributeList();priv
4、atevoidattributeList()throwsIOExceptionline();intattrLength=StreamUtils.read2(in);System.out.println(共有+attrLength+个属性);for(inti=0;iattrLength;i+)line();attribute();privatevoidattribute()throwsIOExceptionintnameIndex=StreamUtils.read2(in);intlength=StreamUtils.read4(in);byteinfo=StreamUtils.read(in,
5、length);System.out.println(nameIndex:+nameIndex);System.out.println(length:+length);System.out.println(info:+info);privatevoidmethodList()throwsIOExceptionintlength=StreamUtils.read2(in);System.out.println(共有+length+个方法);for(inti=0;ilength;i+)method();privatevoidmethod()throwsIOExceptionSystem.out.p
6、rintln(-);intaccessFlag=StreamUtils.read2(in);intnameIndex=StreamUtils.read2(in);intdescriptorIndex=StreamUtils.read2(in);System.out.println(accessFlag:+accessFlag);System.out.println(nameIndex:+nameIndex);System.out.println(descriptorIndex:+descriptorIndex);attributeList();privatevoidfieldList()thr
7、owsIOExceptionline();intlength=StreamUtils.read2(in);System.out.println(共有+length+个字段);for(inti=0;ilength;i+)System.out.println(-);intaccessFlag=StreamUtils.read2(in);intnameIndex=StreamUtils.read2(in);intdescriptorIndex=StreamUtils.read2(in);System.out.println(accessFlag:+accessFlag);System.out.pri
8、ntln(nameIndex:+nameIndex);System.out.println(descriptorIndex:+descriptorIndex);attributeList();privatevoidinheritence()throwsIOExceptionline();intthisClassRef=StreamUtils.read2(in);intsuperClassRef=StreamUtils.read2(in);System.out.println(thisClassRef:+thisClassRef);System.out.println(superClassRef
9、:+superClassRef);intinterfaceLen=StreamUtils.read2(in);System.out.println(接口数量:+interfaceLen);for(inti=0;iinterfaceLen;i+)intinterfaceRef=StreamUtils.read2(in);System.out.println(interfaceRef:+interfaceRef);privatevoidaccessFlag()throwsIOExceptionline();intaccessFlag=StreamUtils.read2(in);System.out
10、.println(accessFlag:0x+Integer.toHexString(accessFlag)+(+accessFlag+);privatevoidconstantPool()throwsIOExceptionnewConstantPoolParser(in).constPool();privatevoidversion()throwsIOExceptionline();intminorVersion=StreamUtils.read2(in);intmajorVersion=StreamUtils.read2(in);System.out.println(版本:+majorVe
11、rsion+.+minorVersion);privatevoidmagicNumber()throwsIOExceptionline();intmagic=StreamUtils.read4(in);System.out.println(魔数:+Integer.toHexString(magic).toUpperCase();privatevoidline()System.out.println(-);ConstPoolParser负责常量池的解析(因为常量池表较多,且数据量也较大,因此单独拉出来解析)packagecom.lixin;importjava.io.IOException;im
12、portjava.io.InputStream;publicclassConstPoolParserpublicstaticfinalintUtf8_info=1;publicstaticfinalintInteger_info=3;publicstaticfinalintFloat_info=4;publicstaticfinalintLong_info=5;publicstaticfinalintDouble_info=6;publicstaticfinalintClass_info=7;publicstaticfinalintString_info=8;publicstaticfinal
13、intFieldref_info=9;publicstaticfinalintMethodref_info=10;publicstaticfinalintInterfaceMethodref_info=11;publicstaticfinalintNameAndType_info=12;publicstaticfinalintMethodHandle_info=15;publicstaticfinalintMethodType_info=16;publicstaticfinalintInvokeDynamic_info=18;privateInputStreamin;publicConstPo
14、olParser(InputStreamin)this.in=in;publicvoidconstPool()throwsIOExceptionline();intlength=StreamUtils.read2(in);System.out.println(共有+length+个常量);booleandoubleBytes=false;for(inti=1;ilength;i+)if(doubleBytes)doubleBytes=false;continue;line();System.out.println(常量索引:+i);intflag=StreamUtils.read1(in);/
15、System.out.println(标志:+flag);switch(flag)caseUtf8_info:utf8Info();continue;caseInteger_info:integerInfo();continue;caseFloat_info:floatInfo();continue;caseLong_info:doubleBytes=true;longInfo();continue;caseDouble_info:doubleBytes=true;doubleInfo();continue;caseClass_info:classInfo();continue;caseStr
16、ing_info:stringInfo();continue;caseFieldref_info:fieldrefInfo();continue;caseMethodref_info:methodrefInfo();continue;caseInterfaceMethodref_info:interfaceMethodrefInfo();continue;caseNameAndType_info:nameAndTypeInfo();continue;caseMethodHandle_info:methodHandleInfo();continue;caseMethodType_info:met
17、hodTypeInfo();continue;caseInvokeDynamic_info:invokeDynamicInfo();continue;default:System.err.println(flag);thrownewRuntimeException(unknown);privatevoidline()System.out.println(-);privatevoidutf8Info()throwsIOExceptionintlength=StreamUtils.read2(in);bytebuf=StreamUtils.read(in,length);Strings=newSt
18、ring(buf,0,buf.length);System.out.println(utf8Info表:);System.out.println(值:+s);privatevoidintegerInfo()throwsIOExceptionSystem.out.println(integerInfo表:);intvalue=StreamUtils.read4(in);System.out.println(值:+value);privatevoidfloatInfo()throwsIOExceptionSystem.out.println(floatInfo表:);intvalue=Stream
19、Utils.read4(in);floatf=FBitsToFloat(value);System.out.println(值:+f);privatevoidlongInfo()throwsIOExceptionSystem.out.println(longInfo表:);longvalue=StreamUtils.read8(in);System.out.println(值:+value);privatevoiddoubleInfo()throwsIOExceptionSystem.out.println(doubleInfo表:);longvalue=StreamUtils
20、.read8(in);doubled=Double.longBitsToDouble(value);System.out.println(值:+d);privatevoidclassInfo()throwsIOExceptionSystem.out.println(classInfo表:);intindex=StreamUtils.read2(in);System.out.println(index:+index);privatevoidstringInfo()throwsIOExceptionSystem.out.println(stringInfo表:);intindex=StreamUt
21、ils.read2(in);System.out.println(index:+index);privatevoidfieldrefInfo()throwsIOExceptionintclassIndex=StreamUtils.read2(in);intnameAndTypeIndex=StreamUtils.read2(in);System.out.println(fieldrefInfo表:);System.out.println(classIndex:+classIndex);System.out.println(nameAndTypeIndex:+nameAndTypeIndex);
22、privatevoidmethodrefInfo()throwsIOExceptionintclassIndex=StreamUtils.read2(in);intnameAndTypeIndex=StreamUtils.read2(in);System.out.println(methodrefInfo表:);System.out.println(classIndex:+classIndex);System.out.println(nameAndTypeIndex:+nameAndTypeIndex);privatevoidinterfaceMethodrefInfo()throwsIOEx
23、ceptionintclassIndex=StreamUtils.read2(in);intnameAndTypeIndex=StreamUtils.read2(in);System.out.println(interfaceMethodrefInfo表:);System.out.println(classIndex:+classIndex);System.out.println(nameAndTypeIndex:+nameAndTypeIndex);privatevoidnameAndTypeInfo()throwsIOExceptionintnameIndex=StreamUtils.re
24、ad2(in);inttypeIndex=StreamUtils.read2(in);System.out.println(nameAndTypeInfo表:);System.out.println(nameIndex:+nameIndex);System.out.println(typeIndex:+typeIndex);privatevoidmethodHandleInfo()throwsIOExceptionintreferenceKind=StreamUtils.read1(in);intreferenceIndex=StreamUtils.read2(in);System.out.println(methodHandleInfo表:);System.out.println(referenceKind:+referenceKind);System.out.println(referenceIndex:+referenceIndex);privatevoidmethodTypeInfo()throwsIOExceptionSystem.out.println(methodTypeInfo表:);intdescriptorIndex=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025标准版石油管道运输合同
- 2025版肺炎症状和护理技能分享
- 2025高中语文第2单元8.1荷花淀8.2玄黑结婚节选8.3党费试题含解析部编版选择性必修中
- 男性泌尿科介绍
- 2025年心肺复苏指南试题及答案
- PVC-U排水管道施工方案
- 2025医疗器械管理试题及答案
- 2025年食品营养健康管理师专业知识考试试题及答案解析
- 健之素的配比方法
- 妇产科正副高级职称考试试题库与答案
- 《风电场项目经济评价规范》(NB-T 31085-2016)
- 《数值分析》研究生配套教学课件
- 企业合同风险与防范讲义课件
- 最新人教版六年级英语上册课件(完美版)Unit 2 Part A 第2课时
- 公路工程监理公司质量保证体系
- 超星尔雅学习通《研究生科研能力训练与培养》章节测试含答案
- 第2章_铁路线路
- 高效液相色谱
- NX-ug理论试题
- 矩量法 Method of Moment
- 电力调控中心监控班组标准化建设工作手册)
评论
0/150
提交评论