|
| 1 | +# JD-Core |
| 2 | + |
| 3 | +JD-Core is a JAVA decompiler written in JAVA. |
| 4 | + |
| 5 | +- Java Decompiler projects home page: |
| 6 | +[http://java-decompiler.github.io](http://java-decompiler.github.io) |
| 7 | +- JD-GUI source code: |
| 8 | +[https://github.com/java-decompiler/jd-core](https://github.com/java-decompiler/jd-core) |
| 9 | +- Maven repository: |
| 10 | +[https://raw.github.com/java-decompiler/mvn-repo/master](https://raw.github.com/java-decompiler/mvn-repo/master) |
| 11 | + |
| 12 | +## Description |
| 13 | +JD-Core is a standalone JAVA library containing the JAVA decompiler of |
| 14 | +"Java Decompiler project". It support Java 1.1.8 to Java 10.0, |
| 15 | +including Lambda expressions, method references and default methods. |
| 16 | +JD-Core is the engine of JD-GUI. |
| 17 | + |
| 18 | +## How to build JD-Core ? |
| 19 | +``` |
| 20 | +> git clone https://github.com/java-decompiler/jd-core.git |
| 21 | +> cd jd-core |
| 22 | +> ./gradlew build |
| 23 | +``` |
| 24 | +generate _"build/libs/jd-core-x.y.z.jar"_ |
| 25 | + |
| 26 | +## How to use JD-Core ? |
| 27 | + |
| 28 | +1. Implement the |
| 29 | +_[jd.core.loader.Loader](https://github.com/java-decompiler/jd-core/blob/master/src/main/java/org/jd/core/loader/Loader.java)_ |
| 30 | +interface, |
| 31 | +2. Implement the |
| 32 | +_[jd.core.printer.Printer](https://github.com/java-decompiler/jd-core/blob/master/src/main/java/org/jd/core/printer/Printer.java)_ |
| 33 | +interface, |
| 34 | +3. And call the method _"decompile(loader, printer, internalTypeName);"_ |
| 35 | + |
| 36 | +## Example |
| 37 | + |
| 38 | +1. Implement the _Loader_ interface: |
| 39 | +```java |
| 40 | +Loader loader = new Loader() { |
| 41 | + @Override |
| 42 | + public byte[] load(String internalName) throws LoaderException { |
| 43 | + InputStream is = this.getClass().getResourceAsStream("/" + internalName + ".class"); |
| 44 | + |
| 45 | + if (is == null) { |
| 46 | + return null; |
| 47 | + } else { |
| 48 | + try (InputStream in=is; ByteArrayOutputStream out=new ByteArrayOutputStream()) { |
| 49 | + byte[] buffer = new byte[1024]; |
| 50 | + int read = in.read(buffer); |
| 51 | + |
| 52 | + while (read > 0) { |
| 53 | + out.write(buffer, 0, read); |
| 54 | + read = in.read(buffer); |
| 55 | + } |
| 56 | + |
| 57 | + return out.toByteArray(); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new LoaderException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean canLoad(String internalName) { |
| 66 | + return this.getClass().getResource("/" + internalName + ".class") != null; |
| 67 | + } |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +2. Implement the _Printer_ interface |
| 72 | +```java |
| 73 | +Printer printer = new Printer() { |
| 74 | + protected static final String TAB = " "; |
| 75 | + protected static final String NEWLINE = "\n"; |
| 76 | + |
| 77 | + protected int indentationCount = 0; |
| 78 | + protected StringBuilder sb = new StringBuilder(); |
| 79 | + |
| 80 | + @Override public String toString() { return sb.toString(); } |
| 81 | + |
| 82 | + @Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {} |
| 83 | + @Override public void end() {} |
| 84 | + |
| 85 | + @Override public void printText(String text) { sb.append(text); } |
| 86 | + @Override public void printNumericConstant(String constant) { sb.append(constant); } |
| 87 | + @Override public void printStringConstant(String constant, String ownerInternalName) { sb.append(constant); } |
| 88 | + @Override public void printKeyword(String keyword) { sb.append(keyword); } |
| 89 | + @Override public void printDeclaration(int flags, String internalTypeName, String name, String descriptor) { sb.append(name); } |
| 90 | + @Override public void printReference(int flags, String internalTypeName, String name, String descriptor, String ownerInternalName) { sb.append(name); } |
| 91 | + |
| 92 | + @Override public void indent() { this.indentationCount++; } |
| 93 | + @Override public void unindent() { if (this.indentationCount > 0) this.indentationCount--; } |
| 94 | + |
| 95 | + @Override public void startLine(int lineNumber) { for (int i=0; i<indentationCount; i++) sb.append(TAB); } |
| 96 | + @Override public void endLine() { sb.append(NEWLINE); } |
| 97 | + @Override public void extraLine(int count) { while (count-- > 0) sb.append(NEWLINE); } |
| 98 | + |
| 99 | + @Override public void startMarker(int type) {} |
| 100 | + @Override public void endMarker(int type) {} |
| 101 | +}; |
| 102 | +``` |
| 103 | + |
| 104 | +3. And call the method _"decompile(loader, printer, internalTypeName);"_ |
| 105 | +```java |
| 106 | +ClassFileToJavaSourceDecompiler decompiler = new |
| 107 | +ClassFileToJavaSourceDecompiler(); |
| 108 | + |
| 109 | +decompiler.decompile(loader, printer, "path/to/YourClass"); |
| 110 | + |
| 111 | +String source = printer.toString(); |
| 112 | +``` |
0 commit comments