test: allow to pass additional compiler options

This commit is contained in:
Skylot
2022-02-24 20:52:34 +00:00
parent a3e9744364
commit 3b781e41ad
4 changed files with 89 additions and 22 deletions
@@ -48,6 +48,7 @@ import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.core.utils.files.FileUtils;
import jadx.core.xmlgen.ResourceStorage;
import jadx.core.xmlgen.entry.ResourceEntry;
import jadx.tests.api.compiler.CompilerOptions;
import jadx.tests.api.compiler.DynamicCompiler;
import jadx.tests.api.compiler.JavaUtils;
import jadx.tests.api.compiler.StaticCompiler;
@@ -93,9 +94,7 @@ public abstract class IntegrationTest extends TestUtils {
protected JadxArgs args;
protected boolean compile;
protected boolean withDebugInfo;
protected boolean useEclipseCompiler;
private int targetJavaVersion = 8;
private CompilerOptions compilerOptions;
private boolean saveTestJar = false;
@@ -118,9 +117,8 @@ public abstract class IntegrationTest extends TestUtils {
@BeforeEach
public void init() {
this.withDebugInfo = true;
this.compile = true;
this.useEclipseCompiler = false;
this.compilerOptions = new CompilerOptions();
this.resMap = Collections.emptyMap();
args = new JadxArgs();
@@ -424,7 +422,7 @@ public abstract class IntegrationTest extends TestUtils {
}
try {
dynamicCompiler = new DynamicCompiler(clsList);
boolean result = dynamicCompiler.compile();
boolean result = dynamicCompiler.compile(compilerOptions);
assertTrue(result, "Compilation failed");
System.out.println("Compilation: PASSED");
} catch (Exception e) {
@@ -459,7 +457,7 @@ public abstract class IntegrationTest extends TestUtils {
List<File> compileFileList = Collections.singletonList(file);
Path outTmp = FileUtils.createTempDir("jadx-tmp-classes");
List<File> files = StaticCompiler.compile(compileFileList, outTmp.toFile(), withDebugInfo, useEclipseCompiler, targetJavaVersion);
List<File> files = StaticCompiler.compile(compileFileList, outTmp.toFile(), compilerOptions);
files.forEach(File::deleteOnExit);
if (saveTestJar) {
saveToJar(files, outTmp);
@@ -489,6 +487,10 @@ public abstract class IntegrationTest extends TestUtils {
return args;
}
public CompilerOptions getCompilerOptions() {
return compilerOptions;
}
public void setArgs(JadxArgs args) {
this.args = args;
}
@@ -498,16 +500,16 @@ public abstract class IntegrationTest extends TestUtils {
}
protected void noDebugInfo() {
this.withDebugInfo = false;
this.compilerOptions.setIncludeDebugInfo(false);
}
protected void useEclipseCompiler() {
this.useEclipseCompiler = true;
this.compilerOptions.setUseEclipseCompiler(true);
}
public void useTargetJavaVersion(int version) {
Assumptions.assumeTrue(JavaUtils.checkJavaVersion(version), "skip test for higher java version");
this.targetJavaVersion = version;
this.compilerOptions.setJavaVersion(version);
}
protected void setFallback() {
@@ -0,0 +1,56 @@
package jadx.tests.api.compiler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CompilerOptions {
private boolean includeDebugInfo = true;
private boolean useEclipseCompiler = false;
private int javaVersion = 8;
List<String> arguments = Collections.emptyList();
public boolean isIncludeDebugInfo() {
return includeDebugInfo;
}
public void setIncludeDebugInfo(boolean includeDebugInfo) {
this.includeDebugInfo = includeDebugInfo;
}
public boolean isUseEclipseCompiler() {
return useEclipseCompiler;
}
public void setUseEclipseCompiler(boolean useEclipseCompiler) {
this.useEclipseCompiler = useEclipseCompiler;
}
public int getJavaVersion() {
return javaVersion;
}
public void setJavaVersion(int javaVersion) {
this.javaVersion = javaVersion;
}
public List<String> getArguments() {
return Collections.unmodifiableList(arguments);
}
public void addArgument(String argName) {
if (arguments.isEmpty()) {
arguments = new ArrayList<>();
}
arguments.add(argName);
}
public void addArgument(String argName, String argValue) {
if (arguments.isEmpty()) {
arguments = new ArrayList<>();
}
arguments.add(argName);
arguments.add(argValue);
}
}
@@ -31,7 +31,7 @@ public class DynamicCompiler {
this.clsNodeList = clsNodeList;
}
public boolean compile() {
public boolean compile(CompilerOptions compilerOptions) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
LOG.error("Can not find compiler, please use JDK instead");
@@ -44,7 +44,7 @@ public class DynamicCompiler {
jFiles.add(new CharSequenceJavaFileObject(clsNode.getFullName(), clsNode.getCode().toString()));
}
CompilationTask compilerTask = compiler.getTask(null, fileManager, null, null, null, jFiles);
CompilationTask compilerTask = compiler.getTask(null, fileManager, null, compilerOptions.getArguments(), null, jFiles);
return Boolean.TRUE.equals(compilerTask.call());
}
@@ -8,6 +8,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
@@ -23,15 +25,15 @@ import jadx.core.utils.files.FileUtils;
public class StaticCompiler {
public static List<File> compile(List<File> files, File outDir, boolean includeDebugInfo,
boolean useEclipseCompiler, int javaVersion) throws IOException {
public static List<File> compile(List<File> files, File outDir, CompilerOptions options) throws IOException {
int javaVersion = options.getJavaVersion();
if (!JavaUtils.checkJavaVersion(javaVersion)) {
throw new IllegalArgumentException("Current java version not meet requirement: "
+ "current: " + JavaUtils.JAVA_VERSION_INT + ", required: " + javaVersion);
}
JavaCompiler compiler;
if (useEclipseCompiler) {
if (options.isUseEclipseCompiler()) {
compiler = new EclipseCompiler();
} else {
compiler = ToolProvider.getSystemJavaCompiler();
@@ -44,15 +46,22 @@ public class StaticCompiler {
StaticFileManager staticFileManager = new StaticFileManager(fileManager, outDir);
List<String> options = new ArrayList<>();
options.add(includeDebugInfo ? "-g" : "-g:none");
List<String> arguments = new ArrayList<>();
arguments.add(options.isIncludeDebugInfo() ? "-g" : "-g:none");
String javaVerStr = javaVersion <= 8 ? "1." + javaVersion : Integer.toString(javaVersion);
options.add("-source");
options.add(javaVerStr);
options.add("-target");
options.add(javaVerStr);
arguments.add("-source");
arguments.add(javaVerStr);
arguments.add("-target");
arguments.add(javaVerStr);
arguments.addAll(options.getArguments());
CompilationTask task = compiler.getTask(null, staticFileManager, null, options, null, compilationUnits);
DiagnosticListener<? super JavaFileObject> diag = new DiagnosticListener<JavaFileObject>() {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
System.out.println(diagnostic);
}
};
CompilationTask task = compiler.getTask(null, staticFileManager, diag, arguments, null, compilationUnits);
Boolean result = task.call();
fileManager.close();
if (Boolean.TRUE.equals(result)) {