How to restore line numbers after decompilation…

by perfstories

I use JD-GUI for decompilation to java now ( There is also JD-Eclipse, plugin for Eclipse.

It’s very nice tool. In general, it solves all my problems in this area. Yeah, of course, several times I got some methods decompiled not very good. And, of course, that were the most important methods for me at that moment. 🙂

However, there was more significant inconvinience for me (in both JD-GUI and JD-Eclipse, as they use the same core). Line numbers of decompiled code are not the same as line numbers of source code.

If you have few sources, and each of source files is not too big, there is no problem. Just add empty lines and go ahead. However, as source files number increases along with their sizes, this manual allignment becomes less desireable.

Fortunately, there was a solution for JD-GUI that could help us with automatic allignment ( Allignment will be done only for methods, but, perhaps, it’s enough in a lot of cases. The only thing I need to learn is how to use this automatic allignment without Eclispe, more exactly, without Eclipse IDE.

It’s really simple. Let’s code Java a little bit:

package util; 
import jd.ide.eclipse.realignment.editors.*;

public class Alligner {
    public static void main(String[] args)  throws Exception {
        FileReader reader = null;
        try {
            File file = new File(args[0]);
            char[] buffer = new char[(int)file.length()];
            reader = new FileReader(file);
            String result = new String(buffer);
            DecompilerOutputUtil decompilerOutputUtil 
                = new DecompilerOutputUtil(result);
            result = new String(decompilerOutputUtil.realign());
        } finally {

And a very simple script to allign a whole directory. Note, you need to download a plugin per se (jar-файл) and install Eclipse (there are jar-dependencies):

FILES=`find $DIR -name "*java"`
CP=`/mnt/JumpStart/perf/scripts/ /users/egm/distr/eclipse`

for NAME in $FILES
   echo "Handling... $NAME"
   java -cp /users/egm/allign/
   util.Alligner $NAME  > $TMP_FILE

That’s it! Let’s thank developers of JD-GUI and allignment plugin and go for debug!