How to restore line numbers after decompilation…

by perfstories

I use JD-GUI for decompilation to java now (http://java.decompiler.free.fr/). 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 (http://java.decompiler.free.fr/?q=node/464). 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.*;
import java.io.*;

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);
            reader.read(buffer);
            String result = new String(buffer);
            DecompilerOutputUtil decompilerOutputUtil 
                = new DecompilerOutputUtil(result);
            result = new String(decompilerOutputUtil.realign());
            System.out.println(result);
        } finally {
            reader.close();
        }
    }
} 

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):

#!/bin/bash
DIR=$1
FILES=`find $DIR -name "*java"`
TMP_FILE_SUFFIX=".alligned"
CP=`/mnt/JumpStart/perf/scripts/makeCP.sh /users/egm/distr/eclipse`

for NAME in $FILES
do
   echo "Handling... $NAME"
   TMP_FILE=$NAME"_"$TMP_FILE_SUFFIX
   java -cp /users/egm/allign/
      :/users/egm/distr/realignment.jd.ide.eclipse_1.0.2.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.jdt.core_3.7.3.v20120119-1537.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.equinox.common_3.6.0.v20110523.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.core.resources_3.7.101.v20120125-1505.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.core.jobs_3.5.101.v20120113-1953.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.core.runtime_3.7.0.v20110110.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.osgi_3.7.2.v20120110-1415.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar
      :/users/egm/distr/eclipse/plugins/org.eclipse.equinox.preferences_3.4.2.v20120111-2020.jar
   util.Alligner $NAME  > $TMP_FILE
   mv $TMP_FILE $NAME
done

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

Advertisements