An attempt to modify the content of the bytecode file in the jar package

background

Recently, I want to implement the function of splitting documents according to section breaks doc / docx, and then I found this article Java splitting Word documents by sections , the dependencies used are:

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>5.2.0</version>
</dependency>

But during use, the following problems occurred:

insert image description here

It is obvious that the error message is that the number of paragraphs cannot exceed 500 lines, otherwise an error will be reported. We found the introduction of the company to which the dependency belongs from the Internet. From the figure below, we can see that the reason for the error is the number of paragraphs when loading the document More than 500 lines, and then an error is reported

insert image description here


But I don't want to be restricted, so what should I do? I tried many ways and found that the final solution is to change the limit on the number of bytecode files in the jar package to temporarily solve the problem

Solution

Determine where to modify

The first tool to share with everyone is: jd-gui(Java反编译工具).exe, the link is below, as follows:

Link: https://pan.baidu.com/s/1OMW_m20ylmamucckbh7pMQ?pwd=l5ao

Extraction code:l5ao

Then we can open the jar package through this tool, and then we click File, and click Save All Sourcesthe button to download the source code, as follows:

insert image description here
Afterwards, we searched for the restrictions in the error report in the code, and the search results are as follows:

insert image description here
There are four places in this kind of position. The above shows that the limit on the number of paragraphs and the number of tables is 500 and 25 respectively.

Now that we have identified the code that needs to be modified is:

com.spire.doc.collections.DocumentObjectCollection#add

com.spire.doc.collections.DocumentObjectCollection#insert

get bytecode file

Since the jar package is similar to zip, you can use a compression tool to decompress it, such as "360 compression". After the decompression is complete, we find the DocumentObjectCollection.classbytecode file

Modify the bytecode file

The JavaBite download information is as follows:

Link: https://pan.baidu.com/s/162a_Mi-nmIeH42ZkLqFAgw?pwd=a4v9

Extraction code:a4v9

Use the JavaBite tool to open the bytecode file, then find the add() and insert() methods, and then increase the paragraph limit of 500 and the table limit of 25. First, we first open the JavaBite tool, and then load the class file into the JavaBite tool , as follows:

insert image description here

Then find the add method, as follows:

insert image description here

First of all, we see that the bytecode instruction in front of the number 500 is sipush, and the value range of the bytecode instruction is -32768~32767, you can read this article to understand the difference between iconst, bipush, sipush, and ldc instructions in the bytecode instruction range of sipush , then we can adjust 500 to 32766. At this time, we double-click the left mouse button on the number 500, and then confirm and exit directly after the modification is completed, as follows:

insert image description here

If we want to modify the limit of up to 25 tables, we need to find the position of the number 25 below. Since the instruction type before 25 is, bipushthis value range is -128~127, then we can replace the bytecode instruction with sipush and change the value to 32766.

In addition, we also need to modify the limit of up to 500 paragraphs and 25 tables in the insert method

You may have doubts, why not use the ldc instruction, the reason is that ldcthe data of the instruction needs to be placed in the constant pool, which is more troublesome

Some students may also ask, since an exception message will be thrown, why not delete athrowthe instruction, because the execution method will report an error after deletion, and the error prompts that the local variable table is inconsistent. When I delete the athrowinstruction, the number of lines will decrease, but the local variable table The quantity is not small, which causes the quantity to be inconsistent, so we can only temporarily solve it by modifying the quantity

Combination jar package

Put DocumentObjectCollection.classthe bytecode file back to its original location, then select comand META-INFdirectory, and then type it into a zip package, and finally zipchange the suffix name of the package to jar, as follows:

insert image description here
Then jarthe package can be used normally

Guess you like

Origin blog.csdn.net/qq_42449963/article/details/130396955
Recommended