diff --git a/README.md b/README.md index 9fce999..eef2c41 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,18 @@ Usage: java -jar jd-cli.jar [options] [Files to decompile] --pattern, -p RegExp pattern which the to-be-decompiled file has to match. Not matching entries are skipped. + --realignLineNumbers, -rn + re-lays out the decompiled code (using jd-core's line number realignment) + so that statements land on the same line numbers as in the original source, + e.g. to set breakpoints or read stack traces against it. Spacing between + members is adjusted and members and inner classes which shared a line in + the original are joined on one line again. It is a best-effort layout, not + a plain padding of blank lines. Needs line number debug info in the classes + (compiled with -g or default javac options); without it nothing changes. + Note that, with or without this option, jd-core orders fields, methods and + inner classes by their original line number rather than by their order in + the bytecode. Can be combined with --displayLineNumbers. + Default: false --serialProcessing, -sp don't use parallel processing Default: false diff --git a/jd-cli/src/main/java/com/github/kwart/jd/cli/CLIArguments.java b/jd-cli/src/main/java/com/github/kwart/jd/cli/CLIArguments.java index c4948ff..7790029 100644 --- a/jd-cli/src/main/java/com/github/kwart/jd/cli/CLIArguments.java +++ b/jd-cli/src/main/java/com/github/kwart/jd/cli/CLIArguments.java @@ -64,6 +64,19 @@ public class CLIArguments implements DecompilerOptions { @Parameter(names = { "--displayLineNumbers", "-n" }, description = "displays line numbers in decompiled classes") private boolean displayLineNumbers; + @Parameter(names = { "--realignLineNumbers", "-rn" }, + description = "re-lays out the decompiled code (using jd-core's line number realignment) so " + + "that statements land on the same line numbers as in the original source, e.g. to" + + " set breakpoints or read stack traces against it. Spacing between members is " + + "adjusted and members and inner classes which shared a line in the original are " + + "joined on one line again. It is a best-effort layout, not a plain padding of " + + "blank lines. Needs line number debug info in the classes (compiled with -g or " + + "default javac options); without it nothing changes. Note that, with or without " + + "this option, jd-core orders fields, methods and inner classes by their original " + + "line number rather than by their order in the bytecode. Can be combined with " + + "--displayLineNumbers") + private boolean realignLineNumbers; + @Parameter(names = { "--escapeUnicodeCharacters", "-eu" }, description = "escape unicode characters in decompiled classes") private boolean escapeUnicodeCharacters; @@ -116,6 +129,10 @@ public boolean isDisplayLineNumbers() { return displayLineNumbers; } + public boolean isRealignLineNumbers() { + return realignLineNumbers; + } + public boolean isEscapeUnicodeCharacters() { return escapeUnicodeCharacters; } diff --git a/jd-lib/src/main/java/com/github/kwart/jd/JavaDecompiler.java b/jd-lib/src/main/java/com/github/kwart/jd/JavaDecompiler.java index 4af0cdd..2a9dc17 100755 --- a/jd-lib/src/main/java/com/github/kwart/jd/JavaDecompiler.java +++ b/jd-lib/src/main/java/com/github/kwart/jd/JavaDecompiler.java @@ -16,8 +16,12 @@ *******************************************************************************/ package com.github.kwart.jd; +import java.util.HashMap; +import java.util.Map; + import org.jd.core.v1.ClassFileToJavaSourceDecompiler; import org.jd.core.v1.api.loader.Loader; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +60,9 @@ public String decompileClass(Loader loader, String internalName) { StringBuilderPrinter sbp = new StringBuilderPrinter(options); try { - DECOMPILER_TL.get().decompile(loader, sbp, internalName); + Map configuration = new HashMap<>(); + configuration.put("realignLineNumbers", options.isRealignLineNumbers()); + DECOMPILER_TL.get().decompile(loader, sbp, internalName, configuration); } catch (Exception e) { LOGGER.error("Can't decompile " + internalName, e); return null; diff --git a/jd-lib/src/main/java/com/github/kwart/jd/options/DecompilerOptions.java b/jd-lib/src/main/java/com/github/kwart/jd/options/DecompilerOptions.java index 1f08427..01d7d58 100644 --- a/jd-lib/src/main/java/com/github/kwart/jd/options/DecompilerOptions.java +++ b/jd-lib/src/main/java/com/github/kwart/jd/options/DecompilerOptions.java @@ -29,5 +29,25 @@ public interface DecompilerOptions { boolean isEscapeUnicodeCharacters(); + /** + * Returns {@code true} if jd-core should realign the decompiled code with the line numbers of the original source. + *

+ * This is delegated to jd-core's {@code realignLineNumbers} configuration. The layouter treats the lines with known + * original line numbers as fixed anchors and fits the flexible parts in between (spacing, members) to them. Where the + * original numbering leaves too little room, members and inner classes which shared a line in the original source are joined + * on one line again. The layout is best effort (the search is bounded), so it is not a plain padding with blank lines and + * a perfect match is not guaranteed. + *

+ * Note that ordering the fields, methods and inner classes by their original line number (instead of their order in the + * bytecode) is done by jd-core in any case, whether this option is enabled or not; this option only adds the re-layout + * described above. + *

+ * It requires line number debug information in the class files; without it this option has no effect. It can be + * combined with {@link #isDisplayLineNumbers()}. + */ + default boolean isRealignLineNumbers() { + return false; + } + boolean isParallelProcessingAllowed(); } diff --git a/jd-lib/src/test/java/com/github/kwart/jd/JavaDecompilerTest.java b/jd-lib/src/test/java/com/github/kwart/jd/JavaDecompilerTest.java index d0b455a..ac10ed9 100644 --- a/jd-lib/src/test/java/com/github/kwart/jd/JavaDecompilerTest.java +++ b/jd-lib/src/test/java/com/github/kwart/jd/JavaDecompilerTest.java @@ -1,12 +1,14 @@ package com.github.kwart.jd; import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -94,6 +96,60 @@ public void patternMatchingTest() throws IOException { assertDecompiled(tmpFolder); } + /** + * Decompiles {@link com.github.kwart.jd.sample.BuilderChain} with realigned line numbers and compares the whole output with + * an expected file. + *

+ * Why this sample: it is a typical real-life case, builders called with one call per line ({@code set...}, {@code with...}), + * with javadoc and imports around. The decompiler does not output comments and, without realignment, the statements would + * come out a few lines too early (e.g. the first one on the line 9 instead of 16). With realignment the layout inserts blank + * lines (and moves the opening braces to their own lines where needed), so that every call is back on its original line. + * The getter, setter, {@code hashCode}, {@code equals} and {@code toString} have their signature and their body on the same + * line, as many projects write them. The decompiler cannot always keep them on one line: where the original leaves no room + * (e.g. {@code setName} and {@code equals}) the layout joins signature and body on one line, otherwise the signature and the + * opening brace are printed on the lines before the statement. Either way, each statement stays on its original line. + * The expected file displays the original line numbers (0 stands for the lines without original line number), which are + * the same as the numbers of the lines they are on. + */ + @Test + public void realignLineNumbersTest() throws IOException { + DecompilerOptions options = new DecompilerOptions() { + @Override + public boolean isSkipResources() { + return true; + } + + @Override + public boolean isEscapeUnicodeCharacters() { + return false; + } + + @Override + public boolean isDisplayLineNumbers() { + return true; + } + + @Override + public boolean isRealignLineNumbers() { + return true; + } + + @Override + public boolean isParallelProcessingAllowed() { + return false; + } + }; + File outDir = temporaryFolder.newFolder(); + new DirInput("target/test-classes", "BuilderChain\\.").decompile(new JavaDecompiler(options), new DirOutput(outDir)); + + File actualFile = new File(outDir, "com/github/kwart/jd/sample/BuilderChain.java"); + File expectedFile = new File("src/test/resources/realign/BuilderChain.expected.txt"); + String actual = new String(Files.readAllBytes(actualFile.toPath()), StandardCharsets.UTF_8); + String expected = new String(Files.readAllBytes(expectedFile.toPath()), StandardCharsets.UTF_8); + + assertEquals(expected.replace("\r\n", "\n"), actual.replace("\r\n", "\n")); + } + private void assertDecompiled(File outputFolder) throws IOException { File decompiledFile = new File(outputFolder, "com/github/kwart/jd/HelloWorld.java"); assertTrue(decompiledFile.isFile()); diff --git a/jd-lib/src/test/java/com/github/kwart/jd/sample/BuilderChain.java b/jd-lib/src/test/java/com/github/kwart/jd/sample/BuilderChain.java new file mode 100644 index 0000000..4c8032a --- /dev/null +++ b/jd-lib/src/test/java/com/github/kwart/jd/sample/BuilderChain.java @@ -0,0 +1,47 @@ +package com.github.kwart.jd.sample; + +import java.time.LocalDate; +import java.time.Month; +import java.util.Locale; +import java.util.Objects; + +/** + * Builders called in the usual multi-line style, and one-liners with the signature and the body on the same line. + */ +@SuppressWarnings({ "checkstyle:leftcurly", "checkstyle:rightcurly" }) +public final class BuilderChain { + + private String name; + + /** + * Builds a locale. + */ + public Locale swissGerman() { + return new Locale.Builder() + .setLanguage("de") + .setRegion("CH") + .build(); + } + + /** + * Builds a date. + */ + public LocalDate swissNationalDay() { + return LocalDate.now() + .withMonth(Month.AUGUST.getValue()) + .withDayOfMonth(1); + } + + public String getName() { return name; } + + public void setName(String value) { this.name = value; } + + @Override + public int hashCode() { return Objects.hash(name); } + + @Override + public boolean equals(Object o) { return o instanceof BuilderChain && Objects.equals(name, ((BuilderChain) o).name); } + + @Override + public String toString() { return "BuilderChain[" + name + "]"; } +} diff --git a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt new file mode 100644 index 0000000..a2f287f --- /dev/null +++ b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt @@ -0,0 +1,48 @@ +/* 0 */ package com.github.kwart.jd.sample; +/* 0 */ +/* 0 */ import java.time.LocalDate; +/* 0 */ import java.time.Month; +/* 0 */ import java.util.Locale; +/* 0 */ import java.util.Objects; +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ public final class BuilderChain +/* 0 */ { +/* 0 */ private String name; +/* 0 */ +/* 0 */ public Locale swissGerman() { +/* 20 */ return new Locale.Builder() +/* 21 */ .setLanguage("de") +/* 22 */ .setRegion("CH") +/* 23 */ .build(); +/* 0 */ } +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ public LocalDate swissNationalDay() { +/* 30 */ return LocalDate.now() +/* 31 */ .withMonth(Month.AUGUST.getValue()) +/* 32 */ .withDayOfMonth(1); +/* 0 */ } +/* 0 */ public String getName() { +/* 35 */ return this.name; +/* 0 */ } +/* 37 */ public void setName(String value) { this.name = value; } +/* 0 */ +/* 0 */ public int hashCode() { +/* 40 */ return Objects.hash(this.name); +/* 0 */ } +/* 0 */ +/* 43 */ public boolean equals(Object o) { return (o instanceof BuilderChain && Objects.equals(this.name, ((BuilderChain)o).name)); } +/* 0 */ +/* 0 */ public String toString() { +/* 46 */ return "BuilderChain[" + this.name + "]"; +/* 0 */ } +/* 0 */ }