JD-Core is a JAVA decompiler written in JAVA.
- Java Decompiler project home page: http://java-decompiler.github.io
- JD-Core source code: https://github.com/java-decompiler/jd-core
- JCenter Maven repository: https://jcenter.bintray.com/
JD-Core is a standalone JAVA library containing the JAVA decompiler of "Java Decompiler project". It support Java 1.1.8 to Java 12.0, including Lambda expressions, method references and default methods. JD-Core is the engine of JD-GUI.
> git clone https://github.com/java-decompiler/jd-core.git
> cd jd-core
> ./gradlew build
generate "build/libs/jd-core-x.y.z.jar"
- Implement the org.jd.core.loader.Loader interface,
- Implement the org.jd.core.printer.Printer interface,
- And call the method "decompile(loader, printer, internalTypeName);"
- Implement the Loader interface:
Loaderloader = newLoader() {
@Overridepublicbyte[] load(StringinternalName) throwsLoaderException {
InputStreamis = this.getClass().getResourceAsStream("/" + internalName + ".class");
if (is == null) {
returnnull;
} else {
try (InputStreamin=is; ByteArrayOutputStreamout=newByteArrayOutputStream()) {
byte[] buffer = newbyte[1024];
intread = in.read(buffer);
while (read > 0) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
returnout.toByteArray();
} catch (IOExceptione) {
thrownewLoaderException(e);
}
}
}
@OverridepublicbooleancanLoad(StringinternalName) {
returnthis.getClass().getResource("/" + internalName + ".class") != null;
}
};- Implement the Printer interface
Printerprinter = newPrinter() {
protectedstaticfinalStringTAB = " ";
protectedstaticfinalStringNEWLINE = "\n";
protectedintindentationCount = 0;
protectedStringBuildersb = newStringBuilder();
@OverridepublicStringtoString() { returnsb.toString(); }
@Overridepublicvoidstart(intmaxLineNumber, intmajorVersion, intminorVersion) {}
@Overridepublicvoidend() {}
@OverridepublicvoidprintText(Stringtext) { sb.append(text); }
@OverridepublicvoidprintNumericConstant(Stringconstant) { sb.append(constant); }
@OverridepublicvoidprintStringConstant(Stringconstant, StringownerInternalName) { sb.append(constant); }
@OverridepublicvoidprintKeyword(Stringkeyword) { sb.append(keyword); }
@OverridepublicvoidprintDeclaration(inttype, StringinternalTypeName, Stringname, Stringdescriptor) { sb.append(name); }
@OverridepublicvoidprintReference(inttype, StringinternalTypeName, Stringname, Stringdescriptor, StringownerInternalName) { sb.append(name); }
@Overridepublicvoidindent() { this.indentationCount++; }
@Overridepublicvoidunindent() { this.indentationCount--; }
@OverridepublicvoidstartLine(intlineNumber) { for (inti=0; i<indentationCount; i++) sb.append(TAB); }
@OverridepublicvoidendLine() { sb.append(NEWLINE); }
@OverridepublicvoidextraLine(intcount) { while (count-- > 0) sb.append(NEWLINE); }
@OverridepublicvoidstartMarker(inttype) {}
@OverridepublicvoidendMarker(inttype) {}
};- And call the method "decompile(loader, printer, internalTypeName);"
ClassFileToJavaSourceDecompilerdecompiler = newClassFileToJavaSourceDecompiler();
decompiler.decompile(loader, printer, "path/to/YourClass");
Stringsource = printer.toString();Released under the GNU GPL v3.