Commit c97678a4 authored by Skylot's avatar Skylot

refactor: make ErrorsCounter non static

parent 2ad73927
package jadx.cli; package jadx.cli;
import jadx.api.JadxDecompiler; import jadx.api.JadxDecompiler;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import java.io.File; import java.io.File;
...@@ -12,33 +11,29 @@ import org.slf4j.LoggerFactory; ...@@ -12,33 +11,29 @@ import org.slf4j.LoggerFactory;
public class JadxCLI { public class JadxCLI {
private static final Logger LOG = LoggerFactory.getLogger(JadxCLI.class); private static final Logger LOG = LoggerFactory.getLogger(JadxCLI.class);
public static void main(String[] args) { public static void main(String[] args) throws JadxException {
try { try {
JadxCLIArgs jadxArgs = new JadxCLIArgs(); JadxCLIArgs jadxArgs = new JadxCLIArgs();
if (processArgs(jadxArgs, args)) { if (processArgs(jadxArgs, args)) {
processAndSave(jadxArgs); processAndSave(jadxArgs);
} }
} catch (JadxException e) { } catch (Throwable e) {
LOG.error(e.getMessage()); LOG.error("jadx error: " + e.getMessage(), e);
System.exit(1); System.exit(1);
} }
} }
static void processAndSave(JadxCLIArgs jadxArgs) throws JadxException { static void processAndSave(JadxCLIArgs jadxArgs) throws JadxException {
try {
JadxDecompiler jadx = new JadxDecompiler(jadxArgs); JadxDecompiler jadx = new JadxDecompiler(jadxArgs);
jadx.loadFiles(jadxArgs.getInput());
jadx.setOutputDir(jadxArgs.getOutDir()); jadx.setOutputDir(jadxArgs.getOutDir());
jadx.loadFiles(jadxArgs.getInput());
jadx.save(); jadx.save();
} catch (Throwable e) { if (jadx.getErrorsCount() != 0) {
throw new JadxException("jadx error: " + e.getMessage(), e); jadx.printErrorsReport();
} LOG.error("finished with errors");
if (ErrorsCounter.getErrorCount() != 0) { } else {
ErrorsCounter.printReport();
throw new JadxException("finished with errors");
}
LOG.info("done"); LOG.info("done");
}
} }
static boolean processArgs(JadxCLIArgs jadxArgs, String[] args) throws JadxException { static boolean processArgs(JadxCLIArgs jadxArgs, String[] args) throws JadxException {
......
package jadx.cli; package jadx.cli;
import jadx.api.IJadxArgs; import jadx.api.IJadxArgs;
import jadx.core.Consts; import jadx.api.JadxDecompiler;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import java.io.File; import java.io.File;
...@@ -105,7 +105,7 @@ public final class JadxCLIArgs implements IJadxArgs { ...@@ -105,7 +105,7 @@ public final class JadxCLIArgs implements IJadxArgs {
// print usage in not sorted fields order (by default its sorted by description) // print usage in not sorted fields order (by default its sorted by description)
PrintStream out = System.out; PrintStream out = System.out;
out.println(); out.println();
out.println("jadx - dex to java decompiler, version: " + Consts.JADX_VERSION); out.println("jadx - dex to java decompiler, version: " + JadxDecompiler.getVersion());
out.println(); out.println();
out.println("usage: jadx [options] " + jc.getMainParameterDescription()); out.println("usage: jadx [options] " + jc.getMainParameterDescription());
out.println("options:"); out.println("options:");
...@@ -148,6 +148,7 @@ public final class JadxCLIArgs implements IJadxArgs { ...@@ -148,6 +148,7 @@ public final class JadxCLIArgs implements IJadxArgs {
return input; return input;
} }
@Override
public File getOutDir() { public File getOutDir() {
return outputDir; return outputDir;
} }
......
package jadx.api; package jadx.api;
import java.io.File;
public class DefaultJadxArgs implements IJadxArgs { public class DefaultJadxArgs implements IJadxArgs {
@Override @Override
public File getOutDir() {
return new File("jadx-output");
}
@Override
public int getThreadsCount() { public int getThreadsCount() {
return Runtime.getRuntime().availableProcessors(); return Runtime.getRuntime().availableProcessors();
} }
......
package jadx.api; package jadx.api;
import java.io.File;
public interface IJadxArgs { public interface IJadxArgs {
File getOutDir();
int getThreadsCount(); int getThreadsCount();
boolean isCFGOutput(); boolean isCFGOutput();
......
...@@ -7,7 +7,6 @@ import jadx.core.dex.nodes.ClassNode; ...@@ -7,7 +7,6 @@ import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.RootNode; import jadx.core.dex.nodes.RootNode;
import jadx.core.dex.visitors.IDexTreeVisitor; import jadx.core.dex.visitors.IDexTreeVisitor;
import jadx.core.dex.visitors.SaveCode; import jadx.core.dex.visitors.SaveCode;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.DecodeException; import jadx.core.utils.exceptions.DecodeException;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
...@@ -57,12 +56,13 @@ public final class JadxDecompiler { ...@@ -57,12 +56,13 @@ public final class JadxDecompiler {
private List<JavaClass> classes; private List<JavaClass> classes;
public JadxDecompiler() { public JadxDecompiler() {
this.args = new DefaultJadxArgs(); this(new DefaultJadxArgs());
init();
} }
public JadxDecompiler(IJadxArgs jadxArgs) { public JadxDecompiler(IJadxArgs jadxArgs) {
this.args = jadxArgs; this.args = jadxArgs;
this.outDir = jadxArgs.getOutDir();
reset();
init(); init();
} }
...@@ -72,17 +72,20 @@ public final class JadxDecompiler { ...@@ -72,17 +72,20 @@ public final class JadxDecompiler {
} }
void init() { void init() {
reset();
if (outDir == null) { if (outDir == null) {
outDir = new File("jadx-output"); outDir = new DefaultJadxArgs().getOutDir();
} }
this.passes = Jadx.getPassesList(args, outDir); this.passes = Jadx.getPassesList(args, outDir);
} }
void reset() { void reset() {
ClassInfo.clearCache(); ClassInfo.clearCache();
ErrorsCounter.reset();
classes = null; classes = null;
root = null;
}
public static String getVersion() {
return Jadx.getVersion();
} }
public void loadFile(File file) throws JadxException { public void loadFile(File file) throws JadxException {
...@@ -182,7 +185,17 @@ public final class JadxDecompiler { ...@@ -182,7 +185,17 @@ public final class JadxDecompiler {
} }
public int getErrorsCount() { public int getErrorsCount() {
return ErrorsCounter.getErrorCount(); if (root == null) {
return 0;
}
return root.getErrorsCounter().getErrorCount();
}
public void printErrorsReport() {
if (root == null) {
return;
}
root.getErrorsCounter().printReport();
} }
void parse() throws DecodeException { void parse() throws DecodeException {
...@@ -214,6 +227,6 @@ public final class JadxDecompiler { ...@@ -214,6 +227,6 @@ public final class JadxDecompiler {
@Override @Override
public String toString() { public String toString() {
return "jadx decompiler"; return "jadx decompiler " + getVersion();
} }
} }
package jadx.core; package jadx.core;
public class Consts { public class Consts {
public static final String JADX_VERSION = Jadx.getVersion();
public static final boolean DEBUG = false; public static final boolean DEBUG = false;
public static final String CLASS_OBJECT = "java.lang.Object"; public static final String CLASS_OBJECT = "java.lang.Object";
......
...@@ -172,7 +172,7 @@ public class NameGen { ...@@ -172,7 +172,7 @@ public class NameGen {
} }
} }
public static String getAliasForObject(String name) { private static String getAliasForObject(String name) {
return OBJ_ALIAS.get(name); return OBJ_ALIAS.get(name);
} }
......
...@@ -3,6 +3,7 @@ package jadx.core.dex.nodes; ...@@ -3,6 +3,7 @@ package jadx.core.dex.nodes;
import jadx.core.clsp.ClspGraph; import jadx.core.clsp.ClspGraph;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.DecodeException; import jadx.core.utils.exceptions.DecodeException;
import jadx.core.utils.files.InputFile; import jadx.core.utils.files.InputFile;
...@@ -14,6 +15,7 @@ import java.util.Map; ...@@ -14,6 +15,7 @@ import java.util.Map;
public class RootNode { public class RootNode {
private final Map<String, ClassNode> names = new HashMap<String, ClassNode>(); private final Map<String, ClassNode> names = new HashMap<String, ClassNode>();
private final ErrorsCounter errorsCounter = new ErrorsCounter();
private List<DexNode> dexNodes; private List<DexNode> dexNodes;
public void load(List<InputFile> dexFiles) throws DecodeException { public void load(List<InputFile> dexFiles) throws DecodeException {
...@@ -101,4 +103,8 @@ public class RootNode { ...@@ -101,4 +103,8 @@ public class RootNode {
String fullName = cls.getFullName(); String fullName = cls.getFullName();
return searchClassByName(fullName); return searchClassByName(fullName);
} }
public ErrorsCounter getErrorsCounter() {
return errorsCounter;
}
} }
...@@ -20,20 +20,20 @@ import org.slf4j.LoggerFactory; ...@@ -20,20 +20,20 @@ import org.slf4j.LoggerFactory;
public class ErrorsCounter { public class ErrorsCounter {
private static final Logger LOG = LoggerFactory.getLogger(ErrorsCounter.class); private static final Logger LOG = LoggerFactory.getLogger(ErrorsCounter.class);
private static final Set<Object> ERROR_NODES = new HashSet<Object>(); private final Set<Object> errorNodes = new HashSet<Object>();
private static int errorsCount; private int errorsCount;
public static int getErrorCount() { public int getErrorCount() {
return errorsCount; return errorsCount;
} }
public static void reset() { public void reset() {
ERROR_NODES.clear(); errorNodes.clear();
errorsCount = 0; errorsCount = 0;
} }
private static void addError(IAttributeNode node, String msg, Throwable e) { private void addError(IAttributeNode node, String msg, Throwable e) {
ERROR_NODES.add(node); errorNodes.add(node);
errorsCount++; errorsCount++;
if (e != null) { if (e != null) {
...@@ -53,13 +53,13 @@ public class ErrorsCounter { ...@@ -53,13 +53,13 @@ public class ErrorsCounter {
public static String classError(ClassNode cls, String errorMsg, Throwable e) { public static String classError(ClassNode cls, String errorMsg, Throwable e) {
String msg = formatErrorMsg(cls, errorMsg); String msg = formatErrorMsg(cls, errorMsg);
addError(cls, msg, e); cls.dex().root().getErrorsCounter().addError(cls, msg, e);
return msg; return msg;
} }
public static String methodError(MethodNode mth, String errorMsg, Throwable e) { public static String methodError(MethodNode mth, String errorMsg, Throwable e) {
String msg = formatErrorMsg(mth, errorMsg); String msg = formatErrorMsg(mth, errorMsg);
addError(mth, msg, e); mth.dex().root().getErrorsCounter().addError(mth, msg, e);
return msg; return msg;
} }
...@@ -67,10 +67,10 @@ public class ErrorsCounter { ...@@ -67,10 +67,10 @@ public class ErrorsCounter {
return methodError(mth, errorMsg, null); return methodError(mth, errorMsg, null);
} }
public static void printReport() { public void printReport() {
if (getErrorCount() > 0) { if (getErrorCount() > 0) {
LOG.error(getErrorCount() + " errors occured in following nodes:"); LOG.error(getErrorCount() + " errors occured in following nodes:");
List<Object> nodes = new ArrayList<Object>(ERROR_NODES); List<Object> nodes = new ArrayList<Object>(errorNodes);
Collections.sort(nodes, new Comparator<Object>() { Collections.sort(nodes, new Comparator<Object>() {
@Override @Override
public int compare(Object o1, Object o2) { public int compare(Object o1, Object o2) {
...@@ -92,7 +92,7 @@ public class ErrorsCounter { ...@@ -92,7 +92,7 @@ public class ErrorsCounter {
return msg + " in method: " + mth; return msg + " in method: " + mth;
} }
private static String formatException(Throwable e) { private String formatException(Throwable e) {
if (e == null || e.getMessage() == null) { if (e == null || e.getMessage() == null) {
return ""; return "";
} else { } else {
...@@ -100,11 +100,11 @@ public class ErrorsCounter { ...@@ -100,11 +100,11 @@ public class ErrorsCounter {
} }
} }
public static String formatErrorMsg(ClassNode cls, String msg, Throwable e) { public String formatErrorMsg(ClassNode cls, String msg, Throwable e) {
return formatErrorMsg(cls, msg) + formatException(e); return formatErrorMsg(cls, msg) + formatException(e);
} }
public static String formatErrorMsg(MethodNode mth, String msg, Throwable e) { public String formatErrorMsg(MethodNode mth, String msg, Throwable e) {
return formatErrorMsg(mth, msg) + formatException(e); return formatErrorMsg(mth, msg) + formatException(e);
} }
} }
package jadx.tests package jadx.tests
import jadx.api.JadxDecompiler
import jadx.api.IJadxArgs import jadx.api.IJadxArgs
import jadx.core.dex.nodes.MethodNode import jadx.api.JadxDecompiler
import jadx.core.utils.ErrorsCounter
import jadx.core.utils.exceptions.JadxException import jadx.core.utils.exceptions.JadxException
import jadx.core.utils.exceptions.JadxRuntimeException import jadx.core.utils.exceptions.JadxRuntimeException
import spock.lang.Specification import spock.lang.Specification
...@@ -67,20 +64,4 @@ class TestAPI extends Specification { ...@@ -67,20 +64,4 @@ class TestAPI extends Specification {
expect: expect:
new JadxDecompiler().getErrorsCount() == 0 new JadxDecompiler().getErrorsCount() == 0
} }
def "get errors count after one more init"() {
setup:
new JadxDecompiler()
def mth = Mock(MethodNode)
when:
ErrorsCounter.methodError(mth, "")
def d = new JadxDecompiler()
then:
d.getErrorsCount() == 0
}
def "decompiler toString()"() {
expect:
new JadxDecompiler().toString() == "jadx decompiler"
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment