Skip to content

Commit

Permalink
In unit tests, check that the decompiled source code can be recompiled.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Jul 29, 2019
1 parent 87ecfad commit 518515c
Show file tree
Hide file tree
Showing 9 changed files with 1,034 additions and 782 deletions.
1,705 changes: 939 additions & 766 deletions src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/test/java/org/jd/core/v1/ControlFlowGraphTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -22,7 +22,7 @@
import org.jd.core.v1.service.converter.classfiletojavasyntax.processor.ConvertClassFileProcessor;
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.*;
import org.jd.core.v1.service.deserializer.classfile.DeserializeClassFileProcessor;
import org.jd.core.v1.util.ControlFlowGraphPlantUMLWriter;
import org.jd.core.v1.cfg.ControlFlowGraphPlantUMLWriter;
import org.junit.Test;

import java.io.FileInputStream;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jd/core/v1/JavaFragmentToTokenTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -19,7 +19,7 @@
import org.jd.core.v1.service.writer.WriteTokenProcessor;
import org.jd.core.v1.services.tokenizer.javafragmenttotoken.TestTokenizeJavaFragmentProcessor;
import org.jd.core.v1.util.DefaultList;
import org.jd.core.v1.util.PatternMaker;
import org.jd.core.v1.regex.PatternMaker;
import org.junit.Assert;
import org.junit.Test;

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jd/core/v1/LayoutFragmentProcessorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -18,7 +18,7 @@
import org.jd.core.v1.service.tokenizer.javafragmenttotoken.JavaFragmentToTokenProcessor;
import org.jd.core.v1.service.writer.WriteTokenProcessor;
import org.jd.core.v1.services.tokenizer.javafragmenttotoken.TestTokenizeJavaFragmentProcessor;
import org.jd.core.v1.util.PatternMaker;
import org.jd.core.v1.regex.PatternMaker;
import org.junit.Assert;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1.util;
package org.jd.core.v1.cfg;

import org.jd.core.v1.model.classfile.Method;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.cfg.BasicBlock;
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.cfg.ControlFlowGraph;
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.ByteCodeWriter;
import org.jd.core.v1.util.DefaultList;

import java.util.Comparator;
import java.util.HashSet;
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/jd/core/v1/compiler/CompilerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1.compiler;

import javax.tools.*;
import java.io.File;
import java.io.StringWriter;
import java.util.Arrays;

public class CompilerUtil {
protected static final File DESTINATION_DIRECTORY = new File("build/test-recompiled");
protected static final String DESTINATION_DIRECTORY_PATH = DESTINATION_DIRECTORY.getAbsolutePath();

public static boolean compile(String javaVersion, JavaFileObject... JavaFileObjects) throws Exception {
boolean compilationSuccess = false;

DESTINATION_DIRECTORY.mkdirs();

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StringWriter writer = new StringWriter();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
Iterable<String> options = Arrays.asList("-source", javaVersion, "-target", javaVersion, "-d", DESTINATION_DIRECTORY_PATH);
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(JavaFileObjects);
compilationSuccess = compiler.getTask(writer, fileManager, diagnostics, options, null, compilationUnits).call();

for (Diagnostic d : diagnostics.getDiagnostics()) {
if (d.getLineNumber() > 0) {
System.err.print(String.format("%-7s - line %-4d- %s%n", d.getKind(), d.getLineNumber(), d.getMessage(null)));
} else {
System.err.print(String.format("%-7s - - %s%n", d.getKind(), d.getMessage(null)));
}
}
}

return compilationSuccess;
}
}
34 changes: 34 additions & 0 deletions src/test/java/org/jd/core/v1/compiler/JavaSourceFileObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1.compiler;

import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import java.net.URI;

public class JavaSourceFileObject extends SimpleJavaFileObject {
/**
* The source code of this "file".
*/
final String code;

/**
* Constructs a new JavaSourceFromString.
* @param name the name of the compilation unit represented by this file object
* @param code the source code for the compilation unit represented by this file object
*/
public JavaSourceFileObject(String name, String code) {
super(URI.create("string:///" + name.replace('.','/') + JavaFileObject.Kind.SOURCE.extension), JavaFileObject.Kind.SOURCE);
this.code = code;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
12 changes: 6 additions & 6 deletions src/test/java/org/jd/core/v1/printer/PlainTextPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -11,10 +11,10 @@
import org.jd.core.v1.api.printer.Printer;

public class PlainTextPrinter implements Printer {
protected static final String TAB = " ";
protected static final String NEWLINE = "\n";
protected static final String TAB = " ";
protected static final String NEWLINE = "\n";

protected int indentationCount;
protected int indentationCount;
protected StringBuilder sb = new StringBuilder();
protected int realLineNumber = 0;
protected String format;
Expand Down Expand Up @@ -117,10 +117,10 @@ public void startMarker(int type) {}
public void endMarker(int type) {}

protected void printLineNumber(int lineNumber) {
sb.append('[');
sb.append("/*");
sb.append(String.format(format, ++realLineNumber));
sb.append(':');
sb.append(String.format(format, lineNumber));
sb.append("] ");
sb.append(" */ ");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1.util;
package org.jd.core.v1.regex;

public class PatternMaker {
public static String make (String first, String... next) {
Expand Down

0 comments on commit 518515c

Please sign in to comment.