Skip to content

Commit 72800d5

Browse files
nbauma109nbauma109
andauthored
Fixed: JDK version should start with "1." for JDK < 5 (java-decompiler#52)
* Fixed: JDK version should start with "1." for JDK < 5 * test with REALIGN_LINE_NUMBERS "true" Co-authored-by: nbauma109 <nbauma109@github.com>
1 parent 3b18ebb commit 72800d5

2 files changed

Lines changed: 179 additions & 5 deletions

File tree

src/main/java/org/jd/core/v1/printer/LineNumberStringBuilderPrinter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,21 @@ public void startLine(int lineNumber) {
107107
public void extraLine(int count) {
108108
if (realignmentLineNumber) {
109109
while (count-- > 0) {
110-
if (maxLineNumber > 0) {
111-
stringBuffer.append(lineNumberBeginPrefix);
112-
stringBuffer.append(unknownLineNumberPrefix);
113-
stringBuffer.append(lineNumberEndPrefix);
114-
}
110+
printLineNumber();
115111

116112
stringBuffer.append(NEWLINE);
117113
}
118114
}
119115
}
120116

117+
private void printLineNumber() {
118+
if (maxLineNumber > 0) {
119+
stringBuffer.append(lineNumberBeginPrefix);
120+
stringBuffer.append(unknownLineNumberPrefix);
121+
stringBuffer.append(lineNumberEndPrefix);
122+
}
123+
}
124+
121125
public String buildDecompiledOutput(Map<String, String> preferences, Loader loader, String entryPath, Decompiler decompiler) throws IOException {
122126
// Init preferences
123127
boolean realignmentLineNumbers = Boolean.parseBoolean(preferences.getOrDefault(REALIGN_LINE_NUMBERS, Boolean.FALSE.toString()));
@@ -149,6 +153,7 @@ public String buildDecompiledOutput(Map<String, String> preferences, Loader load
149153
if (majorVersion >= MAJOR_1_5) {
150154
stringBuffer.append(majorVersion - (MAJOR_1_5 - 5));
151155
} else {
156+
stringBuffer.append("1.");
152157
stringBuffer.append(majorVersion - (MAJOR_1_1 - 1));
153158
}
154159

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
3+
* This project is distributed under the GPLv3 license.
4+
* This is a Copyleft license that gives the user the right to use,
5+
* copy and modify the code freely for non-commercial purposes.
6+
*/
7+
8+
package org.jd.core.v1;
9+
10+
import org.jd.core.v1.api.loader.Loader;
11+
import org.jd.core.v1.compiler.CompilerUtil;
12+
import org.jd.core.v1.compiler.InMemoryJavaSourceFileObject;
13+
import org.jd.core.v1.loader.ZipLoader;
14+
import org.jd.core.v1.printer.ClassFilePrinter;
15+
import org.junit.Test;
16+
17+
import java.io.InputStream;
18+
import java.util.Map;
19+
20+
import static jd.core.preferences.Preferences.WRITE_METADATA;
21+
import static jd.core.preferences.Preferences.REALIGN_LINE_NUMBERS;;
22+
23+
public class JavaMetaInfTest extends AbstractJdTest {
24+
25+
@Test
26+
public void testJdk170Basic() throws Exception {
27+
String internalClassName = "org/jd/core/test/Basic";
28+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip")) {
29+
Loader loader = new ZipLoader(is);
30+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
31+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
32+
33+
// Check decompiled source code
34+
assertTrue(source.contains("Java compiler version: 7 (51.0)"));
35+
36+
// Recompile decompiled source code and check errors
37+
assertTrue(CompilerUtil.compile("1.7", new InMemoryJavaSourceFileObject(internalClassName, source)));
38+
}
39+
}
40+
41+
@Test
42+
public void testJdk170NoDebugInfoBasic() throws Exception {
43+
String internalClassName = "org/jd/core/test/Basic";
44+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0-no-debug-info.zip")) {
45+
Loader loader = new ZipLoader(is);
46+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true");
47+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
48+
49+
// Check decompiled source code
50+
assertTrue(source.contains("Java compiler version: 7 (51.0)"));
51+
52+
53+
// Recompile decompiled source code and check errors
54+
assertTrue(CompilerUtil.compile("1.7", new InMemoryJavaSourceFileObject(internalClassName, source)));
55+
}
56+
}
57+
58+
@Test
59+
public void testJdk170Constructors() throws Exception {
60+
String internalClassName = "org/jd/core/test/Constructors";
61+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip")) {
62+
Loader loader = new ZipLoader(is);
63+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
64+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
65+
66+
// Check decompiled source code
67+
assertTrue(source.contains("Java compiler version: 7 (51.0)"));
68+
69+
// Recompile decompiled source code and check errors
70+
assertTrue(CompilerUtil.compile("1.7", new InMemoryJavaSourceFileObject(internalClassName, source)));
71+
}
72+
}
73+
74+
@Test
75+
public void testJdk170Interface() throws Exception {
76+
String internalClassName = "org/jd/core/test/Interface";
77+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip")) {
78+
Loader loader = new ZipLoader(is);
79+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
80+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
81+
82+
// Check decompiled source code
83+
assertTrue(source.contains("Java compiler version: 7 (51.0)"));
84+
85+
// Recompile decompiled source code and check errors
86+
assertTrue(CompilerUtil.compile("1.7", new InMemoryJavaSourceFileObject(internalClassName, source)));
87+
}
88+
}
89+
90+
@Test
91+
public void testJdk118Basic() throws Exception {
92+
String internalClassName = "org/jd/core/test/Basic";
93+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.1.8.zip")) {
94+
Loader loader = new ZipLoader(is);
95+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
96+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
97+
98+
// Check decompiled source code
99+
assertTrue(source.contains("Java compiler version: 1.1 (45.3)"));
100+
101+
// Recompile decompiled source code and check errors
102+
assertTrue(CompilerUtil.compile("1.3", new InMemoryJavaSourceFileObject(internalClassName, source)));
103+
}
104+
}
105+
106+
@Test
107+
public void testJdk131Basic() throws Exception {
108+
String internalClassName = "org/jd/core/test/Basic";
109+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.3.1.zip")) {
110+
Loader loader = new ZipLoader(is);
111+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
112+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
113+
114+
// Check decompiled source code
115+
assertTrue(source.contains("Java compiler version: 1.1 (45.3)"));
116+
117+
// Recompile decompiled source code and check errors
118+
assertTrue(CompilerUtil.compile("1.4", new InMemoryJavaSourceFileObject(internalClassName, source)));
119+
}
120+
}
121+
122+
@Test
123+
public void testJdk142Basic() throws Exception {
124+
String internalClassName = "org/jd/core/test/Basic";
125+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.4.2.zip")) {
126+
Loader loader = new ZipLoader(is);
127+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
128+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
129+
130+
// Check decompiled source code
131+
assertTrue(source.contains("Java compiler version: 1.2 (46.0)"));
132+
133+
// Recompile decompiled source code and check errors
134+
assertTrue(CompilerUtil.compile("1.4", new InMemoryJavaSourceFileObject(internalClassName, source)));
135+
}
136+
}
137+
138+
@Test
139+
public void testJdk901Basic() throws Exception {
140+
String internalClassName = "org/jd/core/test/Basic";
141+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-9.0.1.zip")) {
142+
Loader loader = new ZipLoader(is);
143+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
144+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
145+
146+
// Check decompiled source code
147+
assertTrue(source.contains("Java compiler version: 9 (53.0)"));
148+
149+
// Recompile decompiled source code and check errors
150+
assertTrue(CompilerUtil.compile("1.8", new InMemoryJavaSourceFileObject(internalClassName, source)));
151+
}
152+
}
153+
154+
@Test
155+
public void testJdk1002Basic() throws Exception {
156+
String internalClassName = "org/jd/core/test/Basic";
157+
try (InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-10.0.2.zip")) {
158+
Loader loader = new ZipLoader(is);
159+
Map<String, String> configuration = Map.of(WRITE_METADATA, "true", REALIGN_LINE_NUMBERS, "true");
160+
String source = new ClassFilePrinter().buildDecompiledOutput(configuration, loader, internalClassName + ".class", classFileToJavaSourceDecompiler);
161+
162+
// Check decompiled source code
163+
assertTrue(source.contains("Java compiler version: 10 (54.0)"));
164+
165+
// Recompile decompiled source code and check errors
166+
assertTrue(CompilerUtil.compile("1.8", new InMemoryJavaSourceFileObject(internalClassName, source)));
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)