Skip to content

Repository files navigation

JD-Core

JD-Core is a JAVA decompiler written in JAVA.

Description

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.

How to build JD-Core ?

> git clone https://github.com/java-decompiler/jd-core.git
> cd jd-core
> ./gradlew build

generate "build/libs/jd-core-x.y.z.jar"

How to use JD-Core ?

  1. Implement the org.jd.core.loader.Loader interface,
  2. Implement the org.jd.core.printer.Printer interface,
  3. And call the method "decompile(loader, printer, internalTypeName);"

Example

  1. 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;
}
};
  1. 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) {}
};
  1. And call the method "decompile(loader, printer, internalTypeName);"
ClassFileToJavaSourceDecompilerdecompiler = newClassFileToJavaSourceDecompiler();
decompiler.decompile(loader, printer, "path/to/YourClass");
Stringsource = printer.toString();

License

Released under the GNU GPL v3.

About

JD-Core is a JAVA decompiler written in JAVA.

Resources

Stars

586 stars

Watchers

22 watching

Forks

Releases

Packages

Contributors

Languages