Skip to content

Commit 518515c

Browse files
author
emmanue1
committed
In unit tests, check that the decompiled source code can be recompiled.
1 parent 87ecfad commit 518515c

9 files changed

+1034
-782
lines changed

src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java

Lines changed: 939 additions & 766 deletions
Large diffs are not rendered by default.

src/test/java/org/jd/core/v1/ControlFlowGraphTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
@@ -22,7 +22,7 @@
2222
import org.jd.core.v1.service.converter.classfiletojavasyntax.processor.ConvertClassFileProcessor;
2323
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.*;
2424
import org.jd.core.v1.service.deserializer.classfile.DeserializeClassFileProcessor;
25-
import org.jd.core.v1.util.ControlFlowGraphPlantUMLWriter;
25+
import org.jd.core.v1.cfg.ControlFlowGraphPlantUMLWriter;
2626
import org.junit.Test;
2727

2828
import java.io.FileInputStream;

src/test/java/org/jd/core/v1/JavaFragmentToTokenTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
@@ -19,7 +19,7 @@
1919
import org.jd.core.v1.service.writer.WriteTokenProcessor;
2020
import org.jd.core.v1.services.tokenizer.javafragmenttotoken.TestTokenizeJavaFragmentProcessor;
2121
import org.jd.core.v1.util.DefaultList;
22-
import org.jd.core.v1.util.PatternMaker;
22+
import org.jd.core.v1.regex.PatternMaker;
2323
import org.junit.Assert;
2424
import org.junit.Test;
2525

src/test/java/org/jd/core/v1/LayoutFragmentProcessorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
@@ -18,7 +18,7 @@
1818
import org.jd.core.v1.service.tokenizer.javafragmenttotoken.JavaFragmentToTokenProcessor;
1919
import org.jd.core.v1.service.writer.WriteTokenProcessor;
2020
import org.jd.core.v1.services.tokenizer.javafragmenttotoken.TestTokenizeJavaFragmentProcessor;
21-
import org.jd.core.v1.util.PatternMaker;
21+
import org.jd.core.v1.regex.PatternMaker;
2222
import org.junit.Assert;
2323
import org.junit.Test;
2424

src/test/java/org/jd/core/v1/util/ControlFlowGraphPlantUMLWriter.java renamed to src/test/java/org/jd/core/v1/cfg/ControlFlowGraphPlantUMLWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
66
*/
77

8-
package org.jd.core.v1.util;
8+
package org.jd.core.v1.cfg;
99

1010
import org.jd.core.v1.model.classfile.Method;
1111
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.cfg.BasicBlock;
1212
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.cfg.ControlFlowGraph;
1313
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.ByteCodeWriter;
14+
import org.jd.core.v1.util.DefaultList;
1415

1516
import java.util.Comparator;
1617
import java.util.HashSet;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.compiler;
9+
10+
import javax.tools.*;
11+
import java.io.File;
12+
import java.io.StringWriter;
13+
import java.util.Arrays;
14+
15+
public class CompilerUtil {
16+
protected static final File DESTINATION_DIRECTORY = new File("build/test-recompiled");
17+
protected static final String DESTINATION_DIRECTORY_PATH = DESTINATION_DIRECTORY.getAbsolutePath();
18+
19+
public static boolean compile(String javaVersion, JavaFileObject... JavaFileObjects) throws Exception {
20+
boolean compilationSuccess = false;
21+
22+
DESTINATION_DIRECTORY.mkdirs();
23+
24+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
25+
StringWriter writer = new StringWriter();
26+
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
27+
28+
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
29+
Iterable<String> options = Arrays.asList("-source", javaVersion, "-target", javaVersion, "-d", DESTINATION_DIRECTORY_PATH);
30+
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(JavaFileObjects);
31+
compilationSuccess = compiler.getTask(writer, fileManager, diagnostics, options, null, compilationUnits).call();
32+
33+
for (Diagnostic d : diagnostics.getDiagnostics()) {
34+
if (d.getLineNumber() > 0) {
35+
System.err.print(String.format("%-7s - line %-4d- %s%n", d.getKind(), d.getLineNumber(), d.getMessage(null)));
36+
} else {
37+
System.err.print(String.format("%-7s - - %s%n", d.getKind(), d.getMessage(null)));
38+
}
39+
}
40+
}
41+
42+
return compilationSuccess;
43+
}
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.compiler;
9+
10+
import javax.tools.JavaFileObject;
11+
import javax.tools.SimpleJavaFileObject;
12+
import java.net.URI;
13+
14+
public class JavaSourceFileObject extends SimpleJavaFileObject {
15+
/**
16+
* The source code of this "file".
17+
*/
18+
final String code;
19+
20+
/**
21+
* Constructs a new JavaSourceFromString.
22+
* @param name the name of the compilation unit represented by this file object
23+
* @param code the source code for the compilation unit represented by this file object
24+
*/
25+
public JavaSourceFileObject(String name, String code) {
26+
super(URI.create("string:///" + name.replace('.','/') + JavaFileObject.Kind.SOURCE.extension), JavaFileObject.Kind.SOURCE);
27+
this.code = code;
28+
}
29+
30+
@Override
31+
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
32+
return code;
33+
}
34+
}

src/test/java/org/jd/core/v1/printer/PlainTextPrinter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
@@ -11,10 +11,10 @@
1111
import org.jd.core.v1.api.printer.Printer;
1212

1313
public class PlainTextPrinter implements Printer {
14-
protected static final String TAB = " ";
15-
protected static final String NEWLINE = "\n";
14+
protected static final String TAB = " ";
15+
protected static final String NEWLINE = "\n";
1616

17-
protected int indentationCount;
17+
protected int indentationCount;
1818
protected StringBuilder sb = new StringBuilder();
1919
protected int realLineNumber = 0;
2020
protected String format;
@@ -117,10 +117,10 @@ public void startMarker(int type) {}
117117
public void endMarker(int type) {}
118118

119119
protected void printLineNumber(int lineNumber) {
120-
sb.append('[');
120+
sb.append("/*");
121121
sb.append(String.format(format, ++realLineNumber));
122122
sb.append(':');
123123
sb.append(String.format(format, lineNumber));
124-
sb.append("] ");
124+
sb.append(" */ ");
125125
}
126126
}

src/test/java/org/jd/core/v1/util/PatternMaker.java renamed to src/test/java/org/jd/core/v1/regex/PatternMaker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
66
*/
77

8-
package org.jd.core.v1.util;
8+
package org.jd.core.v1.regex;
99

1010
public class PatternMaker {
1111
public static String make (String first, String... next) {

0 commit comments

Comments
 (0)