From 609582454f0f8847cd04593980517228dd42e649 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:35:37 +0100 Subject: [PATCH 01/16] Add --realignLineNumbers option using jd-core's realignLineNumbers Closes intoolswetrust/jd-cli#102 Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 ++ .../com/github/kwart/jd/cli/CLIArguments.java | 8 ++++ .../com/github/kwart/jd/JavaDecompiler.java | 8 +++- .../kwart/jd/options/DecompilerOptions.java | 7 +++ .../github/kwart/jd/JavaDecompilerTest.java | 47 +++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9fce999..4e4e6e7 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,10 @@ 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 + inserts empty lines so that decompiled line numbers match the original + source + 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..bd3ee6e 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,10 @@ public class CLIArguments implements DecompilerOptions { @Parameter(names = { "--displayLineNumbers", "-n" }, description = "displays line numbers in decompiled classes") private boolean displayLineNumbers; + @Parameter(names = { "--realignLineNumbers", "-rn" }, + description = "inserts empty lines so that decompiled line numbers match the original source") + private boolean realignLineNumbers; + @Parameter(names = { "--escapeUnicodeCharacters", "-eu" }, description = "escape unicode characters in decompiled classes") private boolean escapeUnicodeCharacters; @@ -116,6 +120,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..0311fe5 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,12 @@ public interface DecompilerOptions { boolean isEscapeUnicodeCharacters(); + /** + * Returns {@code true} if jd-core should realign the line numbers of the decompiled code to match the original source. + */ + 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..27e2748 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 @@ -94,6 +94,53 @@ public void patternMatchingTest() throws IOException { assertDecompiled(tmpFolder); } + @Test + public void realignLineNumbersTest() throws IOException { + DecompilerOptions realign = new DecompilerOptions() { + @Override + public boolean isSkipResources() { + return false; + } + + @Override + public boolean isEscapeUnicodeCharacters() { + return false; + } + + @Override + public boolean isDisplayLineNumbers() { + return false; + } + + @Override + public boolean isRealignLineNumbers() { + return true; + } + + @Override + public boolean isParallelProcessingAllowed() { + return true; + } + }; + JDInput input = new DirInput("target/test-classes", "jd.Hello.*World\\."); + File tmpFolder = temporaryFolder.getRoot(); + input.decompile(new JavaDecompiler(realign), new DirOutput(tmpFolder.getAbsoluteFile())); + List decompiled = Files.readAllLines(new File(tmpFolder, "com/github/kwart/jd/HelloWorld.java").toPath()); + List original = Files.readAllLines(new File("src/test/java/com/github/kwart/jd/HelloWorld.java").toPath()); + int origLine = indexOfLine(original, "public static void main(String[] args)"); + assertTrue(origLine >= 0); + assertTrue(decompiled.get(origLine).contains("public static void main(String[] args)")); + } + + private static int indexOfLine(List lines, String text) { + for (int i = 0; i < lines.size(); i++) { + if (lines.get(i).contains(text)) { + return i; + } + } + return -1; + } + private void assertDecompiled(File outputFolder) throws IOException { File decompiledFile = new File(outputFolder, "com/github/kwart/jd/HelloWorld.java"); assertTrue(decompiledFile.isFile()); From 932adea7264e722466176e822c1fcb5a177b7ca4 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:39:48 +0100 Subject: [PATCH 02/16] Document realignLineNumbers behavior in more detail Co-Authored-By: Claude Sonnet 5.5 --- README.md | 10 ++++++++-- .../java/com/github/kwart/jd/cli/CLIArguments.java | 7 ++++++- .../com/github/kwart/jd/options/DecompilerOptions.java | 10 +++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4e4e6e7..e444a0f 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,14 @@ Usage: java -jar jd-cli.jar [options] [Files to decompile] RegExp pattern which the to-be-decompiled file has to match. Not matching entries are skipped. --realignLineNumbers, -rn - inserts empty lines so that decompiled line numbers match the original - source + 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, where the original numbering leaves no + room, members may be moved; 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. + Can be combined with --displayLineNumbers. Default: false --serialProcessing, -sp don't use parallel processing 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 bd3ee6e..3f8d437 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 @@ -65,7 +65,12 @@ public class CLIArguments implements DecompilerOptions { private boolean displayLineNumbers; @Parameter(names = { "--realignLineNumbers", "-rn" }, - description = "inserts empty lines so that decompiled line numbers match the original source") + 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, where the original numbering leaves no room, members may be " + + "moved; 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. Can be combined with " + + "--displayLineNumbers") private boolean realignLineNumbers; @Parameter(names = { "--escapeUnicodeCharacters", "-eu" }, description = "escape unicode characters in decompiled classes") 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 0311fe5..c39d854 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 @@ -30,7 +30,15 @@ public interface DecompilerOptions { boolean isEscapeUnicodeCharacters(); /** - * Returns {@code true} if jd-core should realign the line numbers of the decompiled code to match the original source. + * 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 can be moved to neighbouring sections. 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. + *

+ * 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; From 0ee50b03eab8b0cda70e72f8ffbecc1426d44b0e Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:53:04 +0100 Subject: [PATCH 03/16] Add member re-ordering test for realignLineNumbers and document re-ordering Co-Authored-By: Claude Sonnet 5.5 --- README.md | 8 +- .../com/github/kwart/jd/cli/CLIArguments.java | 6 +- .../kwart/jd/options/DecompilerOptions.java | 4 +- .../github/kwart/jd/JavaDecompilerTest.java | 81 ++++++++++++++----- .../test/resources/realign/DensePacked.java | 11 +++ 5 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 jd-lib/src/test/resources/realign/DensePacked.java diff --git a/README.md b/README.md index e444a0f..9f98de3 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,11 @@ Usage: java -jar jd-cli.jar [options] [Files to decompile] 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, where the original numbering leaves no - room, members may be moved; it is a best-effort layout, not a plain - padding of blank lines. Needs line number debug info in the classes + between members is adjusted, members and inner classes which shared a + line in the original are joined on one line again and members may be + re-ordered (e.g. the constructor of an inner class moved behind its + methods) where the original numbering requires it. 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. Can be combined with --displayLineNumbers. 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 3f8d437..1b5d1de 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 @@ -67,8 +67,10 @@ public class CLIArguments implements DecompilerOptions { @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, where the original numbering leaves no room, members may be " - + "moved; it is a best-effort layout, not a plain padding of blank lines. Needs line number debug info in " + + "it. Spacing between members is adjusted, members and inner classes which shared a line in the original are " + + "joined on one line again and members may be re-ordered (e.g. the constructor of an inner class moved behind " + + "its methods) where the original numbering requires it. 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. Can be combined with " + "--displayLineNumbers") private boolean realignLineNumbers; 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 c39d854..57caabc 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 @@ -34,7 +34,9 @@ public interface DecompilerOptions { *

* 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 can be moved to neighbouring sections. The layout is best effort + * original numbering leaves too little room, members and inner classes which shared a line in the original source are joined + * on one line again and members are re-ordered (e.g. the constructor of an inner class is moved behind its methods). 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. *

* It requires line number debug information in the class files; without it this option has no effect. It can be 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 27e2748..e3049b0 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,6 +1,8 @@ package com.github.kwart.jd; +import static org.hamcrest.CoreMatchers.allOf; 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; @@ -9,7 +11,13 @@ import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.tools.JavaCompiler; +import javax.tools.ToolProvider; import org.junit.Rule; import org.junit.Test; @@ -51,6 +59,12 @@ public boolean isParallelProcessingAllowed() { } }; + /** Number of the println statements in DensePacked.java. */ + private static final int PRINTLN_COUNT = 9; + /** Zero-based index of the line with method b() in DensePacked.java. */ + private static final int LINE_OF_B = 3; + private static final Pattern PRINTLN = Pattern.compile("System\\.out\\.println\\(\"\\w+\"\\);"); + /** * Temporary folder. */ @@ -94,12 +108,49 @@ public void patternMatchingTest() throws IOException { assertDecompiled(tmpFolder); } + /** + * Decompiles a class in which several members (and inner classes) share the same source lines. To keep every statement on + * its original line, jd-core has to join members on one line and to reorder them (e.g. the synthetic constructor of an inner + * class is moved behind its method). This is not possible by merely adding blank lines. + */ @Test - public void realignLineNumbersTest() throws IOException { - DecompilerOptions realign = new DecompilerOptions() { + public void realignLineNumbersReordersMembersTest() throws IOException { + File sourceFile = new File("src/test/resources/realign/DensePacked.java"); + File classesDir = temporaryFolder.newFolder("classes"); + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + assertTrue("A JDK is required to run this test", compiler != null); + assertEquals(0, compiler.run(null, null, null, "-g", "-d", classesDir.getPath(), sourceFile.getPath())); + + String plain = decompileDensePacked(classesDir, false); + String realigned = decompileDensePacked(classesDir, true); + + // without realignment the (synthetic) constructor of the inner class precedes its method + assertTrue(plain.indexOf("In1(DensePacked") < plain.indexOf("void x()")); + // with realignment the members are reordered, so that the method keeps its original line + assertTrue(realigned.indexOf("void x()") < realigned.indexOf("In1(DensePacked")); + + // each statement is on the same line as in the original source + List original = Files.readAllLines(sourceFile.toPath()); + List decompiled = Arrays.asList(realigned.split("\\R", -1)); + int checked = 0; + for (int i = 0; i < original.size(); i++) { + Matcher matcher = PRINTLN.matcher(original.get(i)); + while (matcher.find()) { + assertThat("line " + (i + 1) + " of the realigned output", decompiled.get(i), containsString(matcher.group())); + checked++; + } + } + assertEquals(PRINTLN_COUNT, checked); + // members are joined on shared lines, exactly as in the original source + assertThat(decompiled.get(LINE_OF_B), allOf(containsString("void b()"), containsString("class In1"))); + assertThat(decompiled.get(LINE_OF_B + 1), containsString("void c()")); + } + + private String decompileDensePacked(File classesDir, boolean realign) throws IOException { + DecompilerOptions options = new DecompilerOptions() { @Override public boolean isSkipResources() { - return false; + return true; } @Override @@ -114,31 +165,17 @@ public boolean isDisplayLineNumbers() { @Override public boolean isRealignLineNumbers() { - return true; + return realign; } @Override public boolean isParallelProcessingAllowed() { - return true; + return false; } }; - JDInput input = new DirInput("target/test-classes", "jd.Hello.*World\\."); - File tmpFolder = temporaryFolder.getRoot(); - input.decompile(new JavaDecompiler(realign), new DirOutput(tmpFolder.getAbsoluteFile())); - List decompiled = Files.readAllLines(new File(tmpFolder, "com/github/kwart/jd/HelloWorld.java").toPath()); - List original = Files.readAllLines(new File("src/test/java/com/github/kwart/jd/HelloWorld.java").toPath()); - int origLine = indexOfLine(original, "public static void main(String[] args)"); - assertTrue(origLine >= 0); - assertTrue(decompiled.get(origLine).contains("public static void main(String[] args)")); - } - - private static int indexOfLine(List lines, String text) { - for (int i = 0; i < lines.size(); i++) { - if (lines.get(i).contains(text)) { - return i; - } - } - return -1; + File outDir = temporaryFolder.newFolder(); + new DirInput(classesDir.getPath(), "DensePacked\\.").decompile(new JavaDecompiler(options), new DirOutput(outDir)); + return new String(Files.readAllBytes(new File(outDir, "com/github/kwart/jd/sample/DensePacked.java").toPath())); } private void assertDecompiled(File outputFolder) throws IOException { diff --git a/jd-lib/src/test/resources/realign/DensePacked.java b/jd-lib/src/test/resources/realign/DensePacked.java new file mode 100644 index 0000000..96eccf2 --- /dev/null +++ b/jd-lib/src/test/resources/realign/DensePacked.java @@ -0,0 +1,11 @@ +package com.github.kwart.jd.sample; +public class DensePacked { + public void a() { System.out.println("a"); } + public void b() { System.out.println("b"); } class In1 { void x() { System.out.println("x"); } } + public void c() { System.out.println("c"); } public void d() { + System.out.println("d"); + } + class In2 { void y() { System.out.println("y"); } } public void e() { System.out.println("e"); } + private int f = 1; private int g = 2; + DensePacked() { System.out.println("ctor"); } public void h() { System.out.println("h"); } +} From e962cc160d80b43c816c0f33b9333cb61123dff9 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:53:32 +0100 Subject: [PATCH 04/16] Fix line length in realignLineNumbers description Co-Authored-By: Claude Sonnet 5.5 --- .../java/com/github/kwart/jd/cli/CLIArguments.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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 1b5d1de..8795022 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 @@ -65,13 +65,12 @@ public class CLIArguments implements DecompilerOptions { 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, members and inner classes which shared a line in the original are " - + "joined on one line again and members may be re-ordered (e.g. the constructor of an inner class moved behind " - + "its methods) where the original numbering requires it. 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. Can be combined with " + 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, members and inner classes which shared a line in the original are joined on one line again and members " + + "may be re-ordered (e.g. the constructor of an inner class moved behind its methods) where the original numbering " + + "requires it. 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. Can be combined with " + "--displayLineNumbers") private boolean realignLineNumbers; From 0c91f6ec64d4a14035973ba97e17f01bd194127e Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:53:58 +0100 Subject: [PATCH 05/16] Wrap realignLineNumbers description to satisfy checkstyle Co-Authored-By: Claude Sonnet 5.5 --- .../com/github/kwart/jd/cli/CLIArguments.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 8795022..538e5a3 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 @@ -65,13 +65,15 @@ public class CLIArguments implements DecompilerOptions { 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, members and inner classes which shared a line in the original are joined on one line again and members " - + "may be re-ordered (e.g. the constructor of an inner class moved behind its methods) where the original numbering " - + "requires it. 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. Can be combined with " - + "--displayLineNumbers") + 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, members and inner classes which shared a line in the original are " + + "joined on one line again and members may be re-ordered (e.g. the constructor of " + + "an inner class moved behind its methods) where the original numbering requires " + + "it. 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. Can be combined with --displayLineNumbers") private boolean realignLineNumbers; @Parameter(names = { "--escapeUnicodeCharacters", "-eu" }, description = "escape unicode characters in decompiled classes") From b9b14aa8929cfc9fbc38f891ef1b351c349b6b8e Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:58:40 +0100 Subject: [PATCH 06/16] Make realign test independent of the javac version Compile the sample with --release 17 and showcase member re-ordering with explicit members (the implicit inner class constructor is not emitted by javac 17). Co-Authored-By: Claude Sonnet 5.5 --- README.md | 18 +++++++-------- .../com/github/kwart/jd/cli/CLIArguments.java | 10 ++++---- .../kwart/jd/options/DecompilerOptions.java | 5 ++-- .../github/kwart/jd/JavaDecompilerTest.java | 23 +++++++++---------- .../test/resources/realign/DensePacked.java | 12 ++++------ 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 9f98de3..8859c0d 100644 --- a/README.md +++ b/README.md @@ -51,15 +51,15 @@ Usage: java -jar jd-cli.jar [options] [Files to decompile] 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, members and inner classes which shared a - line in the original are joined on one line again and members may be - re-ordered (e.g. the constructor of an inner class moved behind its - methods) where the original numbering requires it. 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. - Can be combined with --displayLineNumbers. + 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, members and inner classes which shared a line in the + original are joined on one line again and members may be re-ordered (e.g. a + field moved behind the constructor) where the original numbering requires + it. 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. Can be combined with + --displayLineNumbers. Default: false --serialProcessing, -sp don't use parallel processing 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 538e5a3..cf73f4f 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 @@ -69,11 +69,11 @@ public class CLIArguments implements DecompilerOptions { + "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, members and inner classes which shared a line in the original are " - + "joined on one line again and members may be re-ordered (e.g. the constructor of " - + "an inner class moved behind its methods) where the original numbering requires " - + "it. 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. Can be combined with --displayLineNumbers") + + "joined on one line again and members may be re-ordered (e.g. a field moved " + + "behind the constructor) where the original numbering requires it. 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. Can be combined with --displayLineNumbers") private boolean realignLineNumbers; @Parameter(names = { "--escapeUnicodeCharacters", "-eu" }, description = "escape unicode characters in decompiled classes") 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 57caabc..d31066a 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 @@ -35,9 +35,8 @@ public interface DecompilerOptions { * 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 and members are re-ordered (e.g. the constructor of an inner class is moved behind its methods). 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. + * on one line again and members are re-ordered (e.g. a field is moved behind the constructor). + * 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. *

* It requires line number debug information in the class files; without it this option has no effect. It can be * combined with {@link #isDisplayLineNumbers()}. 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 e3049b0..2af9e9f 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 @@ -60,9 +60,7 @@ public boolean isParallelProcessingAllowed() { }; /** Number of the println statements in DensePacked.java. */ - private static final int PRINTLN_COUNT = 9; - /** Zero-based index of the line with method b() in DensePacked.java. */ - private static final int LINE_OF_B = 3; + private static final int PRINTLN_COUNT = 8; private static final Pattern PRINTLN = Pattern.compile("System\\.out\\.println\\(\"\\w+\"\\);"); /** @@ -110,8 +108,8 @@ public void patternMatchingTest() throws IOException { /** * Decompiles a class in which several members (and inner classes) share the same source lines. To keep every statement on - * its original line, jd-core has to join members on one line and to reorder them (e.g. the synthetic constructor of an inner - * class is moved behind its method). This is not possible by merely adding blank lines. + * its original line, jd-core has to join members on one line and to reorder them (e.g. the field of an inner class is moved + * behind its constructor). This is not possible by merely adding blank lines. */ @Test public void realignLineNumbersReordersMembersTest() throws IOException { @@ -119,15 +117,16 @@ public void realignLineNumbersReordersMembersTest() throws IOException { File classesDir = temporaryFolder.newFolder("classes"); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); assertTrue("A JDK is required to run this test", compiler != null); - assertEquals(0, compiler.run(null, null, null, "-g", "-d", classesDir.getPath(), sourceFile.getPath())); + int result = compiler.run(null, null, null, "-g", "--release", "17", "-d", classesDir.getPath(), sourceFile.getPath()); + assertEquals(0, result); String plain = decompileDensePacked(classesDir, false); String realigned = decompileDensePacked(classesDir, true); - // without realignment the (synthetic) constructor of the inner class precedes its method - assertTrue(plain.indexOf("In1(DensePacked") < plain.indexOf("void x()")); - // with realignment the members are reordered, so that the method keeps its original line - assertTrue(realigned.indexOf("void x()") < realigned.indexOf("In1(DensePacked")); + // without realignment the field of the inner class precedes its constructor + assertTrue(plain.indexOf("int z") < plain.indexOf("In3()")); + // with realignment the members sharing a line are re-ordered, so that each of them keeps its original line + assertTrue(realigned.indexOf("In3()") < realigned.indexOf("int z")); // each statement is on the same line as in the original source List original = Files.readAllLines(sourceFile.toPath()); @@ -142,8 +141,8 @@ public void realignLineNumbersReordersMembersTest() throws IOException { } assertEquals(PRINTLN_COUNT, checked); // members are joined on shared lines, exactly as in the original source - assertThat(decompiled.get(LINE_OF_B), allOf(containsString("void b()"), containsString("class In1"))); - assertThat(decompiled.get(LINE_OF_B + 1), containsString("void c()")); + assertThat(decompiled.get(2), allOf(containsString("void a()"), containsString("class In1"))); + assertThat(decompiled.get(3), allOf(containsString("DensePacked()"), containsString("class In2"))); } private String decompileDensePacked(File classesDir, boolean realign) throws IOException { diff --git a/jd-lib/src/test/resources/realign/DensePacked.java b/jd-lib/src/test/resources/realign/DensePacked.java index 96eccf2..826f622 100644 --- a/jd-lib/src/test/resources/realign/DensePacked.java +++ b/jd-lib/src/test/resources/realign/DensePacked.java @@ -1,11 +1,7 @@ package com.github.kwart.jd.sample; public class DensePacked { - public void a() { System.out.println("a"); } - public void b() { System.out.println("b"); } class In1 { void x() { System.out.println("x"); } } - public void c() { System.out.println("c"); } public void d() { - System.out.println("d"); - } - class In2 { void y() { System.out.println("y"); } } public void e() { System.out.println("e"); } - private int f = 1; private int g = 2; - DensePacked() { System.out.println("ctor"); } public void h() { System.out.println("h"); } + public void a() { System.out.println("a"); } class In1 { In1() { System.out.println("in1"); } void x() { System.out.println("x"); } } + class In2 { void y() { System.out.println("y"); } } DensePacked() { System.out.println("ctor"); } + public void b() { System.out.println("b"); } private int f = 1; + class In3 { int z = 3; In3() { System.out.println("in3"); } void w() { System.out.println("w"); } } } From 84ed38caefd379d1db15c08827268fa033ba14a9 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 07:59:06 +0100 Subject: [PATCH 07/16] Fix javadoc line length Co-Authored-By: Claude Sonnet 5.5 --- .../java/com/github/kwart/jd/options/DecompilerOptions.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 d31066a..200b5cc 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 @@ -36,7 +36,8 @@ public interface DecompilerOptions { * 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 and members are re-ordered (e.g. a field is moved behind the constructor). - * 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. + * 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. *

* It requires line number debug information in the class files; without it this option has no effect. It can be * combined with {@link #isDisplayLineNumbers()}. From c66bd69837d156c85c7b0852e77df7a871f61f10 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:06:44 +0100 Subject: [PATCH 08/16] Document that jd-core orders members by line number regardless of realignLineNumbers Co-Authored-By: Claude Sonnet 5.5 --- README.md | 13 +++++++------ .../java/com/github/kwart/jd/cli/CLIArguments.java | 12 +++++++----- .../github/kwart/jd/options/DecompilerOptions.java | 10 +++++++--- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8859c0d..b6d2aca 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,13 @@ Usage: java -jar jd-cli.jar [options] [Files to decompile] 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, members and inner classes which shared a line in the - original are joined on one line again and members may be re-ordered (e.g. a - field moved behind the constructor) where the original numbering requires - it. 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. Can be combined with - --displayLineNumbers. + original are joined on one line again and members sharing a line may be re- + ordered (e.g. a field moved behind the constructor). 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 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 cf73f4f..ae852ae 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 @@ -69,11 +69,13 @@ public class CLIArguments implements DecompilerOptions { + "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, members and inner classes which shared a line in the original are " - + "joined on one line again and members may be re-ordered (e.g. a field moved " - + "behind the constructor) where the original numbering requires it. 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. Can be combined with --displayLineNumbers") + + "joined on one line again and members sharing a line may be re-ordered (e.g. a " + + "field moved behind the constructor). 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") 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 200b5cc..df555a8 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 @@ -35,9 +35,13 @@ public interface DecompilerOptions { * 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 and members are re-ordered (e.g. a field is moved behind the constructor). - * 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. + * on one line again and members sharing a line may be re-ordered (e.g. a field + * is moved behind the constructor). 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()}. From 7c5f3c21cd023210180e508545259c1d220dc213 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:18:31 +0100 Subject: [PATCH 09/16] Compare the whole output in the realign test and explain the DensePacked sample Co-Authored-By: Claude Sonnet 5.5 --- .../github/kwart/jd/JavaDecompilerTest.java | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) 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 2af9e9f..cdd9c67 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,6 +1,5 @@ package com.github.kwart.jd; -import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -11,10 +10,7 @@ import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; @@ -59,10 +55,6 @@ public boolean isParallelProcessingAllowed() { } }; - /** Number of the println statements in DensePacked.java. */ - private static final int PRINTLN_COUNT = 8; - private static final Pattern PRINTLN = Pattern.compile("System\\.out\\.println\\(\"\\w+\"\\);"); - /** * Temporary folder. */ @@ -107,9 +99,17 @@ public void patternMatchingTest() throws IOException { } /** - * Decompiles a class in which several members (and inner classes) share the same source lines. To keep every statement on - * its original line, jd-core has to join members on one line and to reorder them (e.g. the field of an inner class is moved - * behind its constructor). This is not possible by merely adding blank lines. + * Decompiles DensePacked.java with realigned line numbers and compares the whole output. + *

+ * Why this sample: jd-core sorts fields, methods and inner classes by their original line number whether the option is + * enabled or not, so a class with one member per line (e.g. methods interleaved with inner classes) comes out in the same + * order with and without realignment, and could only prove that the lines match. In DensePacked several members share + * a line, which is where the realignment layout actually does something on top of the sort: members are joined on one line + * again, and members sharing a line are re-ordered (the field {@code z} of {@code In3} is moved behind its constructor, + * which is not the case without realignment). The sample is compiled in the test (with {@code --release 17}, so that the + * output does not depend on the javac version), because such a layout would not pass checkstyle as a source file. + *

+ * The numbers in the expected output are the original line numbers: line 3 of DensePacked.java is line 3 of the output, etc. */ @Test public void realignLineNumbersReordersMembersTest() throws IOException { @@ -120,32 +120,28 @@ public void realignLineNumbersReordersMembersTest() throws IOException { int result = compiler.run(null, null, null, "-g", "--release", "17", "-d", classesDir.getPath(), sourceFile.getPath()); assertEquals(0, result); - String plain = decompileDensePacked(classesDir, false); - String realigned = decompileDensePacked(classesDir, true); - - // without realignment the field of the inner class precedes its constructor - assertTrue(plain.indexOf("int z") < plain.indexOf("In3()")); - // with realignment the members sharing a line are re-ordered, so that each of them keeps its original line - assertTrue(realigned.indexOf("In3()") < realigned.indexOf("int z")); - - // each statement is on the same line as in the original source - List original = Files.readAllLines(sourceFile.toPath()); - List decompiled = Arrays.asList(realigned.split("\\R", -1)); - int checked = 0; - for (int i = 0; i < original.size(); i++) { - Matcher matcher = PRINTLN.matcher(original.get(i)); - while (matcher.find()) { - assertThat("line " + (i + 1) + " of the realigned output", decompiled.get(i), containsString(matcher.group())); - checked++; - } - } - assertEquals(PRINTLN_COUNT, checked); - // members are joined on shared lines, exactly as in the original source - assertThat(decompiled.get(2), allOf(containsString("void a()"), containsString("class In1"))); - assertThat(decompiled.get(3), allOf(containsString("DensePacked()"), containsString("class In2"))); + String expected = String.join("\n", + "package com.github.kwart.jd.sample;", + "public class DensePacked {", + // line 3 + " public void a() { System.out.println(\"a\"); } class In1 { In1() { System.out.println(\"in1\"); } " + + "void x() { System.out.println(\"x\"); } }", + // line 4 + " DensePacked() { System.out.println(\"ctor\"); } class In2 { void y() { System.out.println(\"y\"); } }", + // line 5 + " private int f = 1; public void b() { System.out.println(\"b\"); }", + // line 6: the field z is moved behind the constructor + " class In3 { In3() { this.z = 3; System.out.println(\"in3\"); } int z; " + + "void w() { System.out.println(\"w\"); }", + " }", + "", + "}", + ""); + + assertEquals(expected, decompileDensePacked(classesDir).replace("\r\n", "\n")); } - private String decompileDensePacked(File classesDir, boolean realign) throws IOException { + private String decompileDensePacked(File classesDir) throws IOException { DecompilerOptions options = new DecompilerOptions() { @Override public boolean isSkipResources() { @@ -164,7 +160,7 @@ public boolean isDisplayLineNumbers() { @Override public boolean isRealignLineNumbers() { - return realign; + return true; } @Override From 950f5aaf3b60289bdceccc6e4c950da0c1e50068 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:23:08 +0100 Subject: [PATCH 10/16] Realign test: display line numbers and compare with an expected output file Co-Authored-By: Claude Sonnet 5.5 --- .../github/kwart/jd/JavaDecompilerTest.java | 29 +++++-------------- .../realign/DensePacked.expected.txt | 9 ++++++ 2 files changed, 17 insertions(+), 21 deletions(-) create mode 100644 jd-lib/src/test/resources/realign/DensePacked.expected.txt 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 cdd9c67..a366415 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 @@ -8,6 +8,7 @@ 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; @@ -109,7 +110,8 @@ public void patternMatchingTest() throws IOException { * which is not the case without realignment). The sample is compiled in the test (with {@code --release 17}, so that the * output does not depend on the javac version), because such a layout would not pass checkstyle as a source file. *

- * The numbers in the expected output are the original line numbers: line 3 of DensePacked.java is line 3 of the output, etc. + * Line numbers are displayed: the expected output (DensePacked.expected.txt) shows the original line numbers, so line 3 of + * DensePacked.java is on line 3 of the output, etc. (0 for the lines without original line number). */ @Test public void realignLineNumbersReordersMembersTest() throws IOException { @@ -120,25 +122,10 @@ public void realignLineNumbersReordersMembersTest() throws IOException { int result = compiler.run(null, null, null, "-g", "--release", "17", "-d", classesDir.getPath(), sourceFile.getPath()); assertEquals(0, result); - String expected = String.join("\n", - "package com.github.kwart.jd.sample;", - "public class DensePacked {", - // line 3 - " public void a() { System.out.println(\"a\"); } class In1 { In1() { System.out.println(\"in1\"); } " - + "void x() { System.out.println(\"x\"); } }", - // line 4 - " DensePacked() { System.out.println(\"ctor\"); } class In2 { void y() { System.out.println(\"y\"); } }", - // line 5 - " private int f = 1; public void b() { System.out.println(\"b\"); }", - // line 6: the field z is moved behind the constructor - " class In3 { In3() { this.z = 3; System.out.println(\"in3\"); } int z; " - + "void w() { System.out.println(\"w\"); }", - " }", - "", - "}", - ""); - - assertEquals(expected, decompileDensePacked(classesDir).replace("\r\n", "\n")); + String expected = new String(Files.readAllBytes(new File("src/test/resources/realign/DensePacked.expected.txt").toPath()), + StandardCharsets.UTF_8); + + assertEquals(expected.replace("\r\n", "\n"), decompileDensePacked(classesDir).replace("\r\n", "\n")); } private String decompileDensePacked(File classesDir) throws IOException { @@ -155,7 +142,7 @@ public boolean isEscapeUnicodeCharacters() { @Override public boolean isDisplayLineNumbers() { - return false; + return true; } @Override diff --git a/jd-lib/src/test/resources/realign/DensePacked.expected.txt b/jd-lib/src/test/resources/realign/DensePacked.expected.txt new file mode 100644 index 0000000..ec2ee90 --- /dev/null +++ b/jd-lib/src/test/resources/realign/DensePacked.expected.txt @@ -0,0 +1,9 @@ +/* 0 */ package com.github.kwart.jd.sample; +/* 0 */ public class DensePacked { +/* 3 */ public void a() { System.out.println("a"); } class In1 { In1() { System.out.println("in1"); } void x() { System.out.println("x"); } } +/* 4 */ DensePacked() { System.out.println("ctor"); } class In2 { void y() { System.out.println("y"); } } +/* 5 */ private int f = 1; public void b() { System.out.println("b"); } +/* 6 */ class In3 { In3() { this.z = 3; System.out.println("in3"); } int z; void w() { System.out.println("w"); } +/* 0 */ } +/* 0 */ +/* 0 */ } From d9c3716042575bc3cb7d948c61f76043e3563e2e Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:29:54 +0100 Subject: [PATCH 11/16] Minimize the realign test sample Co-Authored-By: Claude Sonnet 5.5 --- .../github/kwart/jd/JavaDecompilerTest.java | 19 +++++++-------- .../realign/DensePacked.expected.txt | 23 +++++++++++-------- .../test/resources/realign/DensePacked.java | 15 ++++++++---- 3 files changed, 33 insertions(+), 24 deletions(-) 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 a366415..2ccfd50 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 @@ -100,21 +100,18 @@ public void patternMatchingTest() throws IOException { } /** - * Decompiles DensePacked.java with realigned line numbers and compares the whole output. + * Decompiles DensePacked.java with realigned line numbers and compares the whole output with an expected file. *

- * Why this sample: jd-core sorts fields, methods and inner classes by their original line number whether the option is - * enabled or not, so a class with one member per line (e.g. methods interleaved with inner classes) comes out in the same - * order with and without realignment, and could only prove that the lines match. In DensePacked several members share - * a line, which is where the realignment layout actually does something on top of the sort: members are joined on one line - * again, and members sharing a line are re-ordered (the field {@code z} of {@code In3} is moved behind its constructor, - * which is not the case without realignment). The sample is compiled in the test (with {@code --release 17}, so that the - * output does not depend on the javac version), because such a layout would not pass checkstyle as a source file. + * Why this sample: it is the smallest one showing what the realignment does. The two statements are on the lines 6 and 12 + * of the source (separated by a gap of blank lines), whereas without realignment they come out on the lines 5 and 9 of the + * output. With realignment the layout inserts blank lines and moves the opening braces to their own lines, so that the + * statements land on the lines 6 and 12 again (see the line numbers displayed in DensePacked.expected.txt, 0 stands for the + * lines without original line number). Neither members sharing a line nor inner classes are needed to show this. *

- * Line numbers are displayed: the expected output (DensePacked.expected.txt) shows the original line numbers, so line 3 of - * DensePacked.java is on line 3 of the output, etc. (0 for the lines without original line number). + * The sample is compiled in the test with {@code --release 17} (so that the output does not depend on the javac version). */ @Test - public void realignLineNumbersReordersMembersTest() throws IOException { + public void realignLineNumbersTest() throws IOException { File sourceFile = new File("src/test/resources/realign/DensePacked.java"); File classesDir = temporaryFolder.newFolder("classes"); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); diff --git a/jd-lib/src/test/resources/realign/DensePacked.expected.txt b/jd-lib/src/test/resources/realign/DensePacked.expected.txt index ec2ee90..3c7b043 100644 --- a/jd-lib/src/test/resources/realign/DensePacked.expected.txt +++ b/jd-lib/src/test/resources/realign/DensePacked.expected.txt @@ -1,9 +1,14 @@ -/* 0 */ package com.github.kwart.jd.sample; -/* 0 */ public class DensePacked { -/* 3 */ public void a() { System.out.println("a"); } class In1 { In1() { System.out.println("in1"); } void x() { System.out.println("x"); } } -/* 4 */ DensePacked() { System.out.println("ctor"); } class In2 { void y() { System.out.println("y"); } } -/* 5 */ private int f = 1; public void b() { System.out.println("b"); } -/* 6 */ class In3 { In3() { this.z = 3; System.out.println("in3"); } int z; void w() { System.out.println("w"); } -/* 0 */ } -/* 0 */ -/* 0 */ } +/* 0 */ package com.github.kwart.jd.sample; +/* 0 */ +/* 0 */ public class DensePacked +/* 0 */ { +/* 0 */ public void a() { +/* 6 */ System.out.println("a"); +/* 0 */ } +/* 0 */ +/* 0 */ +/* 0 */ public void b() +/* 0 */ { +/* 12 */ System.out.println("b"); +/* 0 */ } +/* 0 */ } diff --git a/jd-lib/src/test/resources/realign/DensePacked.java b/jd-lib/src/test/resources/realign/DensePacked.java index 826f622..462b51e 100644 --- a/jd-lib/src/test/resources/realign/DensePacked.java +++ b/jd-lib/src/test/resources/realign/DensePacked.java @@ -1,7 +1,14 @@ package com.github.kwart.jd.sample; + public class DensePacked { - public void a() { System.out.println("a"); } class In1 { In1() { System.out.println("in1"); } void x() { System.out.println("x"); } } - class In2 { void y() { System.out.println("y"); } } DensePacked() { System.out.println("ctor"); } - public void b() { System.out.println("b"); } private int f = 1; - class In3 { int z = 3; In3() { System.out.println("in3"); } void w() { System.out.println("w"); } } + + public void a() { + System.out.println("a"); + } + + + + public void b() { + System.out.println("b"); + } } From 22ea09c8be92715c880af304ddb0c197ed183d45 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:34:15 +0100 Subject: [PATCH 12/16] Use a realistic builder chain sample for the realign test Co-Authored-By: Claude Sonnet 5.5 --- .../github/kwart/jd/JavaDecompilerTest.java | 43 +++++++------------ .../github/kwart/jd/sample/BuilderChain.java | 33 ++++++++++++++ .../realign/BuilderChain.expected.txt | 33 ++++++++++++++ .../realign/DensePacked.expected.txt | 14 ------ .../test/resources/realign/DensePacked.java | 14 ------ 5 files changed, 82 insertions(+), 55 deletions(-) create mode 100644 jd-lib/src/test/java/com/github/kwart/jd/sample/BuilderChain.java create mode 100644 jd-lib/src/test/resources/realign/BuilderChain.expected.txt delete mode 100644 jd-lib/src/test/resources/realign/DensePacked.expected.txt delete mode 100644 jd-lib/src/test/resources/realign/DensePacked.java 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 2ccfd50..9c0e533 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 @@ -13,9 +13,6 @@ import java.util.ArrayList; import java.util.List; -import javax.tools.JavaCompiler; -import javax.tools.ToolProvider; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -100,32 +97,18 @@ public void patternMatchingTest() throws IOException { } /** - * Decompiles DensePacked.java with realigned line numbers and compares the whole output with an expected file. - *

- * Why this sample: it is the smallest one showing what the realignment does. The two statements are on the lines 6 and 12 - * of the source (separated by a gap of blank lines), whereas without realignment they come out on the lines 5 and 9 of the - * output. With realignment the layout inserts blank lines and moves the opening braces to their own lines, so that the - * statements land on the lines 6 and 12 again (see the line numbers displayed in DensePacked.expected.txt, 0 stands for the - * lines without original line number). Neither members sharing a line nor inner classes are needed to show this. + * Decompiles {@link com.github.kwart.jd.sample.BuilderChain} with realigned line numbers and compares the whole output with + * an expected file. *

- * The sample is compiled in the test with {@code --release 17} (so that the output does not depend on the javac version). + * Why this sample: it is a typical real-life case, builders called with one call per line ({@code append}, {@code set...}), + * 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 7 instead of 17). 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 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 { - File sourceFile = new File("src/test/resources/realign/DensePacked.java"); - File classesDir = temporaryFolder.newFolder("classes"); - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - assertTrue("A JDK is required to run this test", compiler != null); - int result = compiler.run(null, null, null, "-g", "--release", "17", "-d", classesDir.getPath(), sourceFile.getPath()); - assertEquals(0, result); - - String expected = new String(Files.readAllBytes(new File("src/test/resources/realign/DensePacked.expected.txt").toPath()), - StandardCharsets.UTF_8); - - assertEquals(expected.replace("\r\n", "\n"), decompileDensePacked(classesDir).replace("\r\n", "\n")); - } - - private String decompileDensePacked(File classesDir) throws IOException { DecompilerOptions options = new DecompilerOptions() { @Override public boolean isSkipResources() { @@ -153,8 +136,14 @@ public boolean isParallelProcessingAllowed() { } }; File outDir = temporaryFolder.newFolder(); - new DirInput(classesDir.getPath(), "DensePacked\\.").decompile(new JavaDecompiler(options), new DirOutput(outDir)); - return new String(Files.readAllBytes(new File(outDir, "com/github/kwart/jd/sample/DensePacked.java").toPath())); + 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 { 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..d4f25cf --- /dev/null +++ b/jd-lib/src/test/java/com/github/kwart/jd/sample/BuilderChain.java @@ -0,0 +1,33 @@ +package com.github.kwart.jd.sample; + +import java.util.Locale; + +/** + * Builders called in the usual multi-line style. + */ +public final class BuilderChain { + + private BuilderChain() { + } + + /** + * Builds a greeting. + */ + public static String greeting(String name) { + return new StringBuilder() + .append("Hello, ") + .append(name) + .append('!') + .toString(); + } + + /** + * Builds a locale. + */ + public static Locale swissGerman() { + return new Locale.Builder() + .setLanguage("de") + .setRegion("CH") + .build(); + } +} 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..e42bbb4 --- /dev/null +++ b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt @@ -0,0 +1,33 @@ +/* 0 */ package com.github.kwart.jd.sample; +/* 0 */ +/* 0 */ import java.util.Locale; +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ public final class BuilderChain +/* 0 */ { +/* 0 */ public static String greeting(String name) { +/* 17 */ return "Hello, " + +/* 18 */ name + +/* 19 */ '!'; +/* 0 */ } +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ public static Locale swissGerman() +/* 0 */ { +/* 28 */ return new Locale.Builder() +/* 29 */ .setLanguage("de") +/* 30 */ .setRegion("CH") +/* 31 */ .build(); +/* 0 */ } +/* 0 */ } diff --git a/jd-lib/src/test/resources/realign/DensePacked.expected.txt b/jd-lib/src/test/resources/realign/DensePacked.expected.txt deleted file mode 100644 index 3c7b043..0000000 --- a/jd-lib/src/test/resources/realign/DensePacked.expected.txt +++ /dev/null @@ -1,14 +0,0 @@ -/* 0 */ package com.github.kwart.jd.sample; -/* 0 */ -/* 0 */ public class DensePacked -/* 0 */ { -/* 0 */ public void a() { -/* 6 */ System.out.println("a"); -/* 0 */ } -/* 0 */ -/* 0 */ -/* 0 */ public void b() -/* 0 */ { -/* 12 */ System.out.println("b"); -/* 0 */ } -/* 0 */ } diff --git a/jd-lib/src/test/resources/realign/DensePacked.java b/jd-lib/src/test/resources/realign/DensePacked.java deleted file mode 100644 index 462b51e..0000000 --- a/jd-lib/src/test/resources/realign/DensePacked.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.kwart.jd.sample; - -public class DensePacked { - - public void a() { - System.out.println("a"); - } - - - - public void b() { - System.out.println("b"); - } -} From 37bd9b7d8e385816132ef561bcf338b9331d045a Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:40:47 +0100 Subject: [PATCH 13/16] Drop the re-ordering claim from the realign docs; add a StringBuffer chain to the test Co-Authored-By: Claude Sonnet 5.5 --- README.md | 15 +++++++-------- .../com/github/kwart/jd/cli/CLIArguments.java | 15 +++++++-------- .../kwart/jd/options/DecompilerOptions.java | 5 ++--- .../github/kwart/jd/JavaDecompilerTest.java | 1 + .../github/kwart/jd/sample/BuilderChain.java | 11 +++++++++++ .../realign/BuilderChain.expected.txt | 19 +++++++++++++++---- 6 files changed, 43 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b6d2aca..eef2c41 100644 --- a/README.md +++ b/README.md @@ -53,14 +53,13 @@ Usage: java -jar jd-cli.jar [options] [Files to decompile] 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, members and inner classes which shared a line in the - original are joined on one line again and members sharing a line may be re- - ordered (e.g. a field moved behind the constructor). 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. + 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 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 ae852ae..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 @@ -68,14 +68,13 @@ public class CLIArguments implements DecompilerOptions { 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, members and inner classes which shared a line in the original are " - + "joined on one line again and members sharing a line may be re-ordered (e.g. a " - + "field moved behind the constructor). 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") + + "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") 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 df555a8..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 @@ -35,9 +35,8 @@ public interface DecompilerOptions { * 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 and members sharing a line may be re-ordered (e.g. a field - * is moved behind the constructor). 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. + * 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 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 9c0e533..4372f95 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 @@ -104,6 +104,7 @@ public void patternMatchingTest() throws IOException { * 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 7 instead of 17). 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. + * Note that jd-core turns the {@code StringBuilder} and {@code StringBuffer} chains back into string concatenations. * 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. */ 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 index d4f25cf..b16f963 100644 --- 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 @@ -21,6 +21,17 @@ public static String greeting(String name) { .toString(); } + /** + * Builds a greeting (the old way). + */ + public static String legacyGreeting(String name) { + return new StringBuffer() + .append("Hello, ") + .append(name) + .append('!') + .toString(); + } + /** * Builds a locale. */ diff --git a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt index e42bbb4..10e6354 100644 --- a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt +++ b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt @@ -22,12 +22,23 @@ /* 0 */ /* 0 */ /* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ public static String legacyGreeting(String name) { +/* 28 */ return "Hello, " + +/* 29 */ name + +/* 30 */ '!'; +/* 0 */ } +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ /* 0 */ /* 0 */ public static Locale swissGerman() /* 0 */ { -/* 28 */ return new Locale.Builder() -/* 29 */ .setLanguage("de") -/* 30 */ .setRegion("CH") -/* 31 */ .build(); +/* 39 */ return new Locale.Builder() +/* 40 */ .setLanguage("de") +/* 41 */ .setRegion("CH") +/* 42 */ .build(); /* 0 */ } /* 0 */ } From 814c7d3f8d666bc4de263f77880ad836fe432096 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 08:52:17 +0100 Subject: [PATCH 14/16] Simplify the realign fixture: drop the private constructor and the string builder chains Co-Authored-By: Claude Sonnet 5.5 --- .../github/kwart/jd/JavaDecompilerTest.java | 5 +-- .../github/kwart/jd/sample/BuilderChain.java | 38 ++++++------------- .../realign/BuilderChain.expected.txt | 36 ++++++------------ 3 files changed, 25 insertions(+), 54 deletions(-) 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 4372f95..37673cf 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 @@ -100,11 +100,10 @@ public void patternMatchingTest() throws IOException { * 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 append}, {@code set...}), + * 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 7 instead of 17). With realignment the layout inserts blank + * 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. - * Note that jd-core turns the {@code StringBuilder} and {@code StringBuffer} chains back into string concatenations. * 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. */ 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 index b16f963..3a153a8 100644 --- 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 @@ -1,5 +1,7 @@ package com.github.kwart.jd.sample; +import java.time.LocalDate; +import java.time.Month; import java.util.Locale; /** @@ -7,38 +9,22 @@ */ public final class BuilderChain { - private BuilderChain() { - } - - /** - * Builds a greeting. - */ - public static String greeting(String name) { - return new StringBuilder() - .append("Hello, ") - .append(name) - .append('!') - .toString(); - } - - /** - * Builds a greeting (the old way). - */ - public static String legacyGreeting(String name) { - return new StringBuffer() - .append("Hello, ") - .append(name) - .append('!') - .toString(); - } - /** * Builds a locale. */ - public static Locale swissGerman() { + 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); + } } diff --git a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt index 10e6354..a59131b 100644 --- a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt +++ b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt @@ -1,5 +1,7 @@ /* 0 */ package com.github.kwart.jd.sample; /* 0 */ +/* 0 */ import java.time.LocalDate; +/* 0 */ import java.time.Month; /* 0 */ import java.util.Locale; /* 0 */ /* 0 */ @@ -8,37 +10,21 @@ /* 0 */ /* 0 */ /* 0 */ -/* 0 */ -/* 0 */ -/* 0 */ /* 0 */ public final class BuilderChain /* 0 */ { -/* 0 */ public static String greeting(String name) { -/* 17 */ return "Hello, " + -/* 18 */ name + -/* 19 */ '!'; +/* 0 */ public Locale swissGerman() { +/* 16 */ return new Locale.Builder() +/* 17 */ .setLanguage("de") +/* 18 */ .setRegion("CH") +/* 19 */ .build(); /* 0 */ } /* 0 */ /* 0 */ /* 0 */ -/* 0 */ -/* 0 */ -/* 0 */ -/* 0 */ public static String legacyGreeting(String name) { -/* 28 */ return "Hello, " + -/* 29 */ name + -/* 30 */ '!'; -/* 0 */ } -/* 0 */ -/* 0 */ -/* 0 */ -/* 0 */ /* 0 */ -/* 0 */ public static Locale swissGerman() -/* 0 */ { -/* 39 */ return new Locale.Builder() -/* 40 */ .setLanguage("de") -/* 41 */ .setRegion("CH") -/* 42 */ .build(); +/* 0 */ public LocalDate swissNationalDay() { +/* 26 */ return LocalDate.now() +/* 27 */ .withMonth(Month.AUGUST.getValue()) +/* 28 */ .withDayOfMonth(1); /* 0 */ } /* 0 */ } From 4dda3b510d518cf80279a6d747ee2e971f41cc2e Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 09:15:32 +0100 Subject: [PATCH 15/16] Showcase a method with signature and body on the same line in the realign fixture Co-Authored-By: Claude Sonnet 5.5 --- .../test/java/com/github/kwart/jd/JavaDecompilerTest.java | 2 ++ .../test/java/com/github/kwart/jd/sample/BuilderChain.java | 6 ++++++ .../src/test/resources/realign/BuilderChain.expected.txt | 7 +++++++ 3 files changed, 15 insertions(+) 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 37673cf..7da6528 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 @@ -104,6 +104,8 @@ public void patternMatchingTest() throws IOException { * 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 last method has its signature and its body on the same line, as many projects write their one-liners: the decompiler + * prints the signature and the opening brace on the lines before the statement, which stays on its original line (35). * 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. */ 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 index 3a153a8..b31dd6d 100644 --- 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 @@ -27,4 +27,10 @@ public LocalDate swissNationalDay() { .withMonth(Month.AUGUST.getValue()) .withDayOfMonth(1); } + + /** + * Returns the root locale (signature and body on the same line, as many projects write their one-liners). + */ + @SuppressWarnings({ "checkstyle:leftcurly", "checkstyle:rightcurly" }) + public Locale rootLocale() { return Locale.ROOT; } } diff --git a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt index a59131b..5be734e 100644 --- a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt +++ b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt @@ -27,4 +27,11 @@ /* 27 */ .withMonth(Month.AUGUST.getValue()) /* 28 */ .withDayOfMonth(1); /* 0 */ } +/* 0 */ +/* 0 */ +/* 0 */ +/* 0 */ public Locale rootLocale() +/* 0 */ { +/* 35 */ return Locale.ROOT; +/* 0 */ } /* 0 */ } From f19e4a594828eabae8a651cbaf015899fe0a47e5 Mon Sep 17 00:00:00 2001 From: Nicolas Baumann Date: Sat, 21 Nov 2026 09:17:21 +0100 Subject: [PATCH 16/16] Add one-line getter, setter, hashCode, equals and toString to the realign fixture Co-Authored-By: Claude Sonnet 5.5 --- .../github/kwart/jd/JavaDecompilerTest.java | 6 ++-- .../github/kwart/jd/sample/BuilderChain.java | 23 ++++++++---- .../realign/BuilderChain.expected.txt | 35 ++++++++++++------- 3 files changed, 44 insertions(+), 20 deletions(-) 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 7da6528..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 @@ -104,8 +104,10 @@ public void patternMatchingTest() throws IOException { * 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 last method has its signature and its body on the same line, as many projects write their one-liners: the decompiler - * prints the signature and the opening brace on the lines before the statement, which stays on its original line (35). + * 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. */ 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 index b31dd6d..4c8032a 100644 --- 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 @@ -3,12 +3,16 @@ import java.time.LocalDate; import java.time.Month; import java.util.Locale; +import java.util.Objects; /** - * Builders called in the usual multi-line style. + * 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. */ @@ -28,9 +32,16 @@ public LocalDate swissNationalDay() { .withDayOfMonth(1); } - /** - * Returns the root locale (signature and body on the same line, as many projects write their one-liners). - */ - @SuppressWarnings({ "checkstyle:leftcurly", "checkstyle:rightcurly" }) - public Locale rootLocale() { return Locale.ROOT; } + 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 index 5be734e..a2f287f 100644 --- a/jd-lib/src/test/resources/realign/BuilderChain.expected.txt +++ b/jd-lib/src/test/resources/realign/BuilderChain.expected.txt @@ -3,6 +3,8 @@ /* 0 */ import java.time.LocalDate; /* 0 */ import java.time.Month; /* 0 */ import java.util.Locale; +/* 0 */ import java.util.Objects; +/* 0 */ /* 0 */ /* 0 */ /* 0 */ @@ -12,26 +14,35 @@ /* 0 */ /* 0 */ public final class BuilderChain /* 0 */ { +/* 0 */ private String name; +/* 0 */ /* 0 */ public Locale swissGerman() { -/* 16 */ return new Locale.Builder() -/* 17 */ .setLanguage("de") -/* 18 */ .setRegion("CH") -/* 19 */ .build(); +/* 20 */ return new Locale.Builder() +/* 21 */ .setLanguage("de") +/* 22 */ .setRegion("CH") +/* 23 */ .build(); /* 0 */ } /* 0 */ /* 0 */ /* 0 */ /* 0 */ /* 0 */ public LocalDate swissNationalDay() { -/* 26 */ return LocalDate.now() -/* 27 */ .withMonth(Month.AUGUST.getValue()) -/* 28 */ .withDayOfMonth(1); +/* 30 */ return LocalDate.now() +/* 31 */ .withMonth(Month.AUGUST.getValue()) +/* 32 */ .withDayOfMonth(1); /* 0 */ } -/* 0 */ -/* 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 Locale rootLocale() -/* 0 */ { -/* 35 */ return Locale.ROOT; +/* 0 */ public String toString() { +/* 46 */ return "BuilderChain[" + this.name + "]"; /* 0 */ } /* 0 */ }