Java をデコンパイル

環境は、Ubuntu 16.04

10年ぐらい前に作った classfile のソースが行方不明なのでデコンパイル。


#jad

「jadでclassをデコンパイル」
http://qiita.com/Takmiy/items/0c968aab8460d93166b0

を見てやってみる

```

Jad 1.5.8e for Linux on Intel platform (214917 bytes).
Jad 1.5.8e for Linux (statically linked) (389972 bytes) - take this version if the one above crashes or displays the "seek error" message.
```

上をダウンロードして実行したら

```

./jad: error while loading shared libraries: libstdc++-libc6.2-2.so.3: cannot open shared object file: No such file or directory
```

となったので下をダウンロードして実行。../PDFConverter.class をディスコンパイルしてみる。

```

$ ./jad ../PDFConverter.class 
Parsing ../PDFConverter.class...The class file version is 49.0 (only 45.3, 46.0 and 47.0 are supported)
 Generating PDFConverter.jad
Overlapped try statements detected. Not all exception handlers will be resolved in the method main
Couldn't fully decompile method main
Couldn't resolve all exception handlers in method main
```

あれー、(only 45.3, 46.0 and 47.0 are supported)って出てるね。うまくいかないなー　と思っていたら、PDFConverter.jad  というファイルができていました。

```

$ head -30 PDFConverter.jad 
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PDFConverter.java

import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;
import java.io.File;

public class PDFConverter
{

    public PDFConverter()
    {
    }

    public static void main(String args[])
    {
        String s = convertToURL(args[0]);
        String s1 = getFilterName(args[0]);
        String s2 = s.replaceAll("\\..{3}\\Z", ".pdf");
        XComponentContext xcomponentcontext = Bootstrap.bootstrap();
        XMultiComponentFactory xmulticomponentfactory = xcomponentcontext.getServiceManager(); 
```


# jd-gui

http://jd.benow.ca/
こちらからダウンロード

スクリーンショットがWindowsなのでイヤな予感がしたが、マルチプラットフォームで安心。
jd-gui_1.4.0-0_all.deb　をダウンロード。

```

$ sudo dpkg -i jd-gui_1.4.0-0_all.deb 
[sudo] nanbuwks のパスワード: 
以前に未選択のパッケージ jd-gui を選択しています。
(データベースを読み込んでいます ... 現在 793826 個のファイルとディレクトリがインストールされています。)
jd-gui_1.4.0-0_all.deb を展開する準備をしています ...
jd-gui (1.4.0-0) を展開しています...
jd-gui (1.4.0-0) を設定しています ...

```

UnityのDashからJD-GUIを呼び出して実行


![image](https://qiita-image-store.s3.amazonaws.com/0/139524/5b3c859f-564d-b068-f5ba-13cdc20fcbe2.png)


うまくいった。

# 比較

```

$ diff PDFConverter.jad ../PDFConverter-jdgui.java 
1,5d0
< // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
< // Jad home page: http://www.geocities.com/kpdus/jad.html
< // Decompiler options: packimports(3) 
< // Source File Name:   PDFConverter.java
< 
19,20c14,16
< 
<     public PDFConverter()
---
>   public static void main(String[] paramArrayOfString)
>   {
>     try
21a18,61
>       String str1 = convertToURL(paramArrayOfString[0]);
>       String str2 = getFilterName(paramArrayOfString[0]);
>       String str3 = str1.replaceAll("\\..{3}\\Z", ".pdf");
>       
>       XComponentContext localXComponentContext = Bootstrap.bootstrap();
>       XMultiComponentFactory localXMultiComponentFactory = localXComponentContext.getServiceManager();
>       
>       Object localObject1 = localXMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", localXComponentContext);
>       
>       XComponentLoader localXComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, localObject1);
>       
>       PropertyValue[] arrayOfPropertyValue = new PropertyValue[2];
>       
>       arrayOfPropertyValue[0] = new PropertyValue();
>       arrayOfPropertyValue[0].Name = "Hidden";
>       arrayOfPropertyValue[0].Value = new Boolean(true);
>       arrayOfPropertyValue[1] = new PropertyValue();
>       arrayOfPropertyValue[1].Name = "ReadOnly";
>       arrayOfPropertyValue[1].Value = new Boolean(true);
>       
>       XComponent localXComponent1 = localXComponentLoader.loadComponentFromURL(str1, "_blank", 0, arrayOfPropertyValue);
>       
>       arrayOfPropertyValue[0] = new PropertyValue();
>       arrayOfPropertyValue[0].Name = "FilterName";
>       arrayOfPropertyValue[0].Value = str2;
>       arrayOfPropertyValue[1] = new PropertyValue();
>       arrayOfPropertyValue[1].Name = "Overwrite";
>       arrayOfPropertyValue[1].Value = new Boolean(true);
>       
>       XStorable localXStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, localXComponent1);
>       
>       localXStorable.storeToURL(str3, arrayOfPropertyValue);
>       
>       XCloseable localXCloseable = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, localXStorable);
>       if (localXCloseable != null)
>       {
>         localXCloseable.close(false);
>       }
>       else
>       {
>         XComponent localXComponent2 = (XComponent)UnoRuntime.queryInterface(XComponent.class, localXStorable);
>         
>         localXComponent2.dispose();
>       }
23,24c63
< 
<     public static void main(String args[])
---
>     catch (Exception localException)
26,69c65,66
<         String s = convertToURL(args[0]);
<         String s1 = getFilterName(args[0]);
<         String s2 = s.replaceAll("\\..{3}\\Z", ".pdf");
<         XComponentContext xcomponentcontext = Bootstrap.bootstrap();
<         XMultiComponentFactory xmulticomponentfactory = xcomponentcontext.getServiceManager();
<         Object obj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext);
<         XComponentLoader xcomponentloader = (XComponentLoader)UnoRuntime.queryInterface(com/sun/star/frame/XComponentLoader, obj);
<         PropertyValue apropertyvalue[] = new PropertyValue[2];
<         apropertyvalue[0] = new PropertyValue();
<         apropertyvalue[0].Name = "Hidden";
<         apropertyvalue[0].Value = new Boolean(true);
<         apropertyvalue[1] = new PropertyValue();
<         apropertyvalue[1].Name = "ReadOnly";
<         apropertyvalue[1].Value = new Boolean(true);
<         XComponent xcomponent = xcomponentloader.loadComponentFromURL(s, "_blank", 0, apropertyvalue);
<         apropertyvalue[0] = new PropertyValue();
<         apropertyvalue[0].Name = "FilterName";
<         apropertyvalue[0].Value = s1;
<         apropertyvalue[1] = new PropertyValue();
<         apropertyvalue[1].Name = "Overwrite";
<         apropertyvalue[1].Value = new Boolean(true);
<         XStorable xstorable = (XStorable)UnoRuntime.queryInterface(com/sun/star/frame/XStorable, xcomponent);
<         xstorable.storeToURL(s2, apropertyvalue);
<         XCloseable xcloseable = (XCloseable)UnoRuntime.queryInterface(com/sun/star/util/XCloseable, xstorable);
<         if(xcloseable != null)
<         {
<             xcloseable.close(false);
<         } else
<         {
<             XComponent xcomponent1 = (XComponent)UnoRuntime.queryInterface(com/sun/star/lang/XComponent, xstorable);
<             xcomponent1.dispose();
<         }
<         System.exit(0);
<         break MISSING_BLOCK_LABEL_321;
<         Exception exception;
<         exception;
<         exception.printStackTrace();
<         System.exit(1);
<         System.exit(0);
<         break MISSING_BLOCK_LABEL_321;
<         Exception exception1;
<         exception1;
<         System.exit(0);
<         throw exception1;
---
>       localException.printStackTrace();
>       System.exit(1);
71,73c68
< 
<     private static String convertToURL(String s)
<         throws Exception
---
>     finally
75,78c70
<         File file = new File(s);
<         StringBuffer stringbuffer = new StringBuffer("file:///");
<         stringbuffer.append(file.getCanonicalPath().replace('\\', '/'));
<         return stringbuffer.toString();
---
>       System.exit(0);
80,99c72,97
< 
<     private static String getFilterName(String s)
<         throws Exception
<     {
<         String s1 = s.substring(s.length() - 4).toLowerCase();
<         String s2 = "";
<         if(s1.equals(".doc") || s1.equals(".dot") || s1.equals(".sxw") || s1.equals(".stw") || s1.equals(".odt") || s1.equals(".ott"))
<             s2 = "writer_pdf_Export";
<         else
<         if(s1.equals(".xls") || s1.equals(".xlt") || s1.equals(".sxc") || s1.equals(".stc") || s1.equals(".ods") || s1.equals(".ots"))
<             s2 = "calc_pdf_Export";
<         else
<         if(s1.equals(".ppt") || s1.equals(".pot") || s1.equals(".sxi") || s1.equals(".sti") || s1.equals(".odp") || s1.equals(".otp"))
<             s2 = "impress_pdf_Export";
<         else
<         if(s1.equals(".sxd") || s1.equals(".std") || s1.equals(".odg") || s1.equals(".otg"))
<             s2 = "draw_pdf_Export";
<         else
<             throw new Exception("There is no corresponding filter.");
<         return s2;
---
>   }
>   
>   private static String convertToURL(String paramString)
>     throws Exception
>   {
>     File localFile = new File(paramString);
>     StringBuffer localStringBuffer = new StringBuffer("file:///");
>     localStringBuffer.append(localFile.getCanonicalPath().replace('\\', '/'));
>     return localStringBuffer.toString();
>   }
>   
>   private static String getFilterName(String paramString)
>     throws Exception
>   {
>     String str1 = paramString.substring(paramString.length() - 4).toLowerCase();
>     String str2 = "";
>     if ((str1.equals(".doc")) || (str1.equals(".dot")) || (str1.equals(".sxw")) || (str1.equals(".stw")) || (str1.equals(".odt")) || (str1.equals(".ott"))) {
>       str2 = "writer_pdf_Export";
>     } else if ((str1.equals(".xls")) || (str1.equals(".xlt")) || (str1.equals(".sxc")) || (str1.equals(".stc")) || (str1.equals(".ods")) || (str1.equals(".ots"))) {
>       str2 = "calc_pdf_Export";
>     } else if ((str1.equals(".ppt")) || (str1.equals(".pot")) || (str1.equals(".sxi")) || (str1.equals(".sti")) || (str1.equals(".odp")) || (str1.equals(".otp"))) {
>       str2 = "impress_pdf_Export";
>     } else if ((str1.equals(".sxd")) || (str1.equals(".std")) || (str1.equals(".odg")) || (str1.equals(".otg"))) {
>       str2 = "draw_pdf_Export";
>     } else {
>       throw new Exception("There is no corresponding filter.");
100a99,100
>     return str2;
>   }
```