JD-Core-java is a thin-wrapper for the Java Decompiler.
This is hack around the IntelliJ IDE plugin. It fakes the interfaces of the IDE, and provides access to JD-Core.
Since the Author of JD-Core is not willing to provide a library, as seen on this thread, and we all want to batch decompilation, this is pretty much our only option.
I hope this will motivate the author to release a proper library.
这个是抽取IntelliJ IDE jd plugin的插件中的jd-core来实现的,但是现在只有linux版本的。
受此启示,我下载eclipse的jd插件,应该也是包含的。果不其然,是包含的。
但是,抽取代码的发现了一些问题,就是反编译不完全。不知道是哪里出了问题。
改写了下面的代码:
package jd.ide.eclipse.editors;
public class JDSourceMapper
{
private final static String JAVA_CLASS_SUFFIX = ".class";
private final static String JAVA_SOURCE_SUFFIX = ".java";
private final static int JAVA_SOURCE_SUFFIX_LENGTH = 5;
private final static String JAR_SUFFIX = ".jar";
private final static String ZIP_SUFFIX = ".zip";
private static boolean loaded = true;
static {
System.load("d://jd-eclipse.dll");
}
public static void main(String[] args) {
JDSourceMapper jd = new JDSourceMapper();
String s = jd.decompile("d:/antlr-2.7.7.jar", "antlr/ActionElement.class");
System.out.println(s);
}
public native String decompile(String basePath, String classPath);
}
但是反编译出来的结构是这样的:
package antlr;
class ActionElement extends AlternativeElement
{
protected String actionText;
protected boolean isSemPred = false;
// ERROR //
public ActionElement(Grammar paramGrammar, Token paramToken)
{
// Byte code:
// 0: aload_0
// 1: aload_1
// 2: invokespecial 1antlr/AlternativeElement:(Lantlr/Grammar;)V
// 5: aload_0
// 6: iconst_0
// 7: putfield 2antlr/ActionElement:isSemPredZ
// 10: aload_0
// 11: aload_2
// 12: invokevirtual 3antlr/Token:getText()Ljava/lang/String;
// 15: putfield 4antlr/ActionElement:actionTextLjava/lang/String;
// 18: aload_0
// 19: aload_2
// 20: invokevirtual 5antlr/Token:getLine()I
// 23: putfield 6antlr/ActionElement:lineI
// 26: aload_0
// 27: aload_2
// 28: invokevirtual 7antlr/Token:getColumn()I
// 31: putfield 8antlr/ActionElement:columnI
// 34: return
}
// ERROR //
public void generate()
{
// Byte code:
// 0: aload_0
// 1: getfield 9antlr/ActionElement:grammarLantlr/Grammar;
// 4: getfield 10antlr/Grammar:generatorLantlr/CodeGenerator;
// 7: aload_0
// 8: invokevirtual 11antlr/CodeGenerator:gen(Lantlr/ActionElement;)V
// 11: return
}
// ERROR //
public Lookahead look(int paramInt)
{
// Byte code:
// 0: aload_0
// 1: getfield 9antlr/ActionElement:grammarLantlr/Grammar;
// 4: getfield 12antlr/Grammar:theLLkAnalyzerLantlr/LLkGrammarAnalyzer;
// 7: iload_1
// 8: aload_0
// 9: invokeinterface 13 3 0
// 14: areturn
}
// ERROR //
public String toString()
{
// Byte code:
// 0: new 14java/lang/StringBuffer
// 3: dup
// 4: invokespecial 15java/lang/StringBuffer:()V
// 7: ldc 16
// 9: invokevirtual 17java/lang/StringBuffer:append(Ljava/lang/String;)Ljava/lang/StringBuffer;
// 12: aload_0
// 13: getfield 4antlr/ActionElement:actionTextLjava/lang/String;
// 16: invokevirtual 17java/lang/StringBuffer:append(Ljava/lang/String;)Ljava/lang/StringBuffer;
// 19: aload_0
// 20: getfield 2antlr/ActionElement:isSemPredZ
// 23: ifeq +8 -> 31
// 26: ldc 18
// 28: goto +5 -> 33
// 31: ldc 19
// 33: invokevirtual 17java/lang/StringBuffer:append(Ljava/lang/String;)Ljava/lang/StringBuffer;
// 36: invokevirtual 20java/lang/StringBuffer:toString()Ljava/lang/String;
// 39: areturn
}
}
可以看出方法没有反编译出来,但是在jd-gui中出来的结果是好的。不知道哪里出了问题。