Java class parser implementation method example
Recently, I was writing a private project called classanalyzer. The purpose of classanalyzer is to enable us to have an in-depth understanding of the design and structure of Java class files. The main framework and basic functions have been completed, and some detailed functions will be added in the future. In fact, JDK has provided a command-line tool javap to decompile class files, but this article will illustrate my idea of implementing the parser.
Class file
As the carrier of class or interface information, each class file completely defines a class. In order to make Java programs "write once and run everywhere", the Java virtual machine specification strictly stipulates the class file. The basic data unit constituting the class file is bytes. There is no separator between these bytes, which makes almost all the contents stored in the whole class file are necessary data for program operation. The data that cannot be represented by a single byte is represented by multiple consecutive bytes.
The source code of classanalyzer has been placed on GitHub. In readme of classanalyzer, I take the class file of a class as an example to analyze each byte of the class file. I hope it will be helpful to your understanding.
Deep understanding of Java virtual machine