java 读取 class_java class文件解析(一)

读取class文件,解析数据流。

package readClass;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import readClass.util.UtilMap;

/**

* 模拟虚拟机解析class文件

*

* @author Administrator

*

*/

public class MyPraseClassFile

{

static char[] xIndex ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

public static void main(String[] args)

{

Map resultMap = new HashMap(); //用来记录constants中的值

Map tempMap = new HashMap();

try

{

DataInputStream stream = new DataInputStream(MyPraseClassFile.class.getResourceAsStream("exampleClass/ExampleClass.class"));

byte[] b = new byte[4];

int index = 0;

stream.read(b);

ClassBean bean = null;

System.out.println("magic----->"+convert0x(b,index,4));

System.out.println("minor_version----->"+stream.readShort());

System.out.println("major_version----->"+stream.readShort());

int constantsCountt =stream.readShort();

System.out.println("constants_pool_count----->"+constantsCountt);

List list = null; //

for(int count =1;count

{

//读取常量类型

byte tempByte = stream.readByte();

list = resultMap.get(String.valueOf(tempByte));

if(null == list)

{

list = new ArrayList();

resultMap.put(String.valueOf(tempByte),list);

}

bean = new ClassBean();

tempMap.put(String.valueOf(count), bean); //用于之后的查找

list.add(bean);

bean.setIndex(count);

bean.setTag(tempByte);

switch (tempByte)

{

case 1: short length = stream.readShort(); b = new byte[length]; stream.read(b); bean.setValue(new String(b)); break;

case 3: bean.setValue(String.valueOf(stream.readInt())); break;

case 4: bean.setValue(String.valueOf(stream.readFloat())); break;

case 5: bean.setValue(S

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值