栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从字节码解析类名

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从字节码解析类名

如果只需要类名,则可能自己解析类文件的开头,而不是仅仅为此目的而添加用于处理类代码的第三方库。您只需要常量池中的类和字符串,跳过访问标志,然后将/替换为即可。在班级名称中。如果有字节数组,则可以使用以下方法调用此方法

newByteArrayInputStream(byteArray)

public static String getClassName(InputStream is) throws Exception {    DataInputStream dis = new DataInputStream(is);    dis.readLong(); // skip header and class version    int cpcnt = (dis.readShort()&0xffff)-1;    int[] classes = new int[cpcnt];    String[] strings = new String[cpcnt];    for(int i=0; i<cpcnt; i++) {        int t = dis.read();        if(t==7) classes[i] = dis.readShort()&0xffff;        else if(t==1) strings[i] = dis.readUTF();        else if(t==5 || t==6) { dis.readLong(); i++; }        else if(t==8) dis.readShort();        else dis.readInt();    }    dis.readShort(); // skip access flags    return strings[classes[(dis.readShort()&0xffff)-1]-1].replace('/', '.');}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428605.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号