资讯专栏INFORMATION COLUMN

Class文件解析器(1)-介绍

liuyix / 3030人阅读

摘要:文件解析器解析器的名字随便起的关于对编译后的文件进行解析,目前已经完成了一大半,可能会写文章介绍一下划重点,可能。

Class文件解析器 Ming

解析器的名字[Ming](随便起的)

关于

对Java编译后的Class文件进行解析,目前已经完成了一大半,可能会写文章介绍一下(划重点,可能)。

项目地址

github: Ming

已知可能存在的问题

class文件使用的是MUTF-8的编码格式,与UTF-8有微妙的不同,在某些情况下可能错在无法解析的情况。

有一些类型虽然声明了,但是还没有具体的添加内容,(才不是因为懒呢),如果碰到没有实装的类型,会报错,我会尽量今早将所有的类型进行实装。

遇到没有实装类型的错误类型有两种

没有找到指定的属性类型:"can"t find specific attribute"

没有找到指定的常量类型:"can"t find specific type tag: [该常量类型所对应的具体的数值]"

使用方法 环境

支持JDK8及其以上版本

测试用例(因为还没有完全完成这个解析器,所以如果想要运行请尽量使用该示例)

Simple.java

public class Simple implements Parent, Child{
    private int data;
    private static final String flag= "HelloWorld";
    private static final long num = 1L;
    private static final double dou = 0.1;
    public int add(int a, int b) {
      return a + b;
    }
}

Parent.java

public interface Parent {

}

Child.java

public interface Child {

}
运行

参考文件

java com.ming.print.PrintClassInfo.java 具体信息的输出

java com.ming.test.Test.java 调用PrintClassInfo#printAll() 打印所有信息

运行结果示例
[magic]: cafebabe
[minor_version]: 0
[major_version]: 52
[constant_pool_count]: 34
[constant_pool]: 
[1]: ConstantMethodrefInfo
[tag]: 10
[class_index]: 3-java/lang/Object
[name_and_type_index]: 28--()V

[2]: ConstantClassInfo
[tag]: 7
[name_index]: 29-Simple

[3]: ConstantClassInfo
[tag]: 7
[name_index]: 30-java/lang/Object

[4]: ConstantClassInfo
[tag]: 7
[name_index]: 31-Parent

[5]: ConstantClassInfo
[tag]: 7
[name_index]: 32-Child

[6]: ConstantUtf8Info
[tag]: 1
[length]: 4
[bytes]: data

[7]: ConstantUtf8Info
[tag]: 1
[length]: 1
[bytes]: I

[8]: ConstantUtf8Info
[tag]: 1
[length]: 4
[bytes]: flag

[9]: ConstantUtf8Info
[tag]: 1
[length]: 18
[bytes]: Ljava/lang/String;

[10]: ConstantUtf8Info
[tag]: 1
[length]: 13
[bytes]: ConstantValue

[11]: ConstantStringInfo
[tag]: 8
[string_index]: 33-HelloWorld

[12]: ConstantUtf8Info
[tag]: 1
[length]: 3
[bytes]: num

[13]: ConstantUtf8Info
[tag]: 1
[length]: 1
[bytes]: J

[14]: ConstantLongInfo
[tag]: 5
[value]: 01

[16]: ConstantUtf8Info
[tag]: 1
[length]: 3
[bytes]: dou

[17]: ConstantUtf8Info
[tag]: 1
[length]: 1
[bytes]: D

[18]: ConstantDoubleInfo
[tag]: 6
[value]: 3fb999999999999a

[20]: ConstantUtf8Info
[tag]: 1
[length]: 6
[bytes]: 

[21]: ConstantUtf8Info
[tag]: 1
[length]: 3
[bytes]: ()V

[22]: ConstantUtf8Info
[tag]: 1
[length]: 4
[bytes]: Code

[23]: ConstantUtf8Info
[tag]: 1
[length]: 15
[bytes]: LineNumberTable

[24]: ConstantUtf8Info
[tag]: 1
[length]: 3
[bytes]: add

[25]: ConstantUtf8Info
[tag]: 1
[length]: 5
[bytes]: (II)I

[26]: ConstantUtf8Info
[tag]: 1
[length]: 10
[bytes]: SourceFile

[27]: ConstantUtf8Info
[tag]: 1
[length]: 11
[bytes]: Simple.java

[28]: ConstantNameAndTypeInfo
[tag]: 12
[name_index]: 20-
[descriptor_index]: 21-()V

[29]: ConstantUtf8Info
[tag]: 1
[length]: 6
[bytes]: Simple

[30]: ConstantUtf8Info
[tag]: 1
[length]: 16
[bytes]: java/lang/Object

[31]: ConstantUtf8Info
[tag]: 1
[length]: 6
[bytes]: Parent

[32]: ConstantUtf8Info
[tag]: 1
[length]: 5
[bytes]: Child

[33]: ConstantUtf8Info
[tag]: 1
[length]: 10
[bytes]: HelloWorld

[access_flags]: 33
[this_class]: 2
[super_class]: 3
[interfaces_count]: 2
[interfaces]: 
[0]: 4-Parent
[1]: 5-Child

[fields_count]: 4
[fields]: 

[field 1 ]: 
[access_flags]: 2
[name_index]: 6
[descriptor_index]: 7
[attributes_count]: 0

[field 2 ]: 
[access_flags]: 26
[name_index]: 8
[descriptor_index]: 9
[attributes_count]: 1

[1]: AttributeConstantValueInfo
[attribute_name_index]: 10
[attribute_length]: 2
[constantvalue_index]: 11

[field 3 ]: 
[access_flags]: 26
[name_index]: 12
[descriptor_index]: 13
[attributes_count]: 1

[1]: AttributeConstantValueInfo
[attribute_name_index]: 10
[attribute_length]: 2
[constantvalue_index]: 14

[field 4 ]: 
[access_flags]: 26
[name_index]: 16
[descriptor_index]: 17
[attributes_count]: 1

[1]: AttributeConstantValueInfo
[attribute_name_index]: 10
[attribute_length]: 2
[constantvalue_index]: 18


[methods_count]: 2
[methods]: 

[method 1 ]: 
[access_flags]: 1
[name_index]: 20
[descriptor_index]: 21
[attributes_count]: 1

[1]: AttributeCodeInfo
[attribute_name_index]: 22
[attribute_length]: 29
[max_stack]: 1
[max_locals]: 1
[code_length]: 5
42-aload_0
183-invokespecial
0-nop
1-aconst_null
177-return_
[exception_table_length]: 0
[attributes_count]: 1

[method 2 ]: 
[access_flags]: 1
[name_index]: 24
[descriptor_index]: 25
[attributes_count]: 1

[1]: AttributeCodeInfo
[attribute_name_index]: 22
[attribute_length]: 28
[max_stack]: 2
[max_locals]: 3
[code_length]: 4
27-iload_1
28-iload_2
96-iadd
172-ireturn
[exception_table_length]: 0
[attributes_count]: 1


[attributes_count]: 1
[attributes]: 

[0]: AttributeSourceFileInfo
[attribute_name_index]: 26
[attribute_length]: 2
[sourcefile_index]: 27

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69535.html

相关文章

  • 深入理解虚拟机之虚拟机类加载机制

    摘要:最终形成可以被虚拟机最直接使用的类型的过程就是虚拟机的类加载机制。即重写一个类加载器的方法验证验证是连接阶段的第一步,这一阶段的目的是为了确保文件的字节流中包含的信息符合当前虚拟机的要求,并且不会危害虚拟机自身的安全。 《深入理解Java虚拟机:JVM高级特性与最佳实践(第二版》读书笔记与常见相关面试题总结 本节常见面试题(推荐带着问题阅读,问题答案在文中都有提到): 简单说说类加载过...

    MadPecker 评论0 收藏0
  • 网络爬虫介绍

    摘要:什么是爬虫网络爬虫也叫网络蜘蛛,是一种自动化浏览网络的程序,或者说是一种网络机器人。 什么是爬虫 网络爬虫也叫网络蜘蛛,是一种自动化浏览网络的程序,或者说是一种网络机器人。它们被广泛用于互联网搜索引擎或其他类似网站,以获取或更新这些网站的内容和检索方式。它们可以自动采集所有其能够访问到的页面内容,以供搜索引擎做进一步处理(分检整理下载的页面),而使得用户能更快的检索到他们需要的信息。简...

    sf190404 评论0 收藏0
  • Java设计模式之(二)——工厂模式

    摘要:需要说明的是在设计模式一书中将工厂模式分为两类工厂方法模式与抽象工厂模式,将简单工厂模式看为工厂方法模式的一种特例,两者归为一类。工厂模式的作用工厂模式的作用封装变化创建逻辑有可能变化,封装成工厂类之后,创建逻辑的变更对调用者透明。1、什么是工厂模式Define an interface for creating an object,but let subclasses decide whi...

    Doyle 评论0 收藏0
  • 如何实现一个Java Class字节解析(Golang版)

    摘要:在属性中,和分别用于存储字节码长度和字节码指令,每条指令即一个字节类型。在虚拟机执行时,通过读取中的一个个字节码,并将字节码翻译成相应的指令。另外,虽然是一个类型的值,但是实际上一个方法不允许超过条字节码指令。 最近在写一个私人项目,名字叫做SmallVM,SmallVM的目的在于通过实现一个轻量级的Java虚拟机,加深对Java虚拟机的认知和理解。在Java虚拟机加载类的过程中,需要...

    diabloneo 评论0 收藏0

发表评论

0条评论

liuyix

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<