提交 dbd00d5a 编写于 作者: S Skylot

refactor: use instance methods for error and warning notifications

上级 2da772df
......@@ -7,7 +7,6 @@ import jadx.core.codegen.CodeGen;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.visitors.DepthTraversal;
import jadx.core.dex.visitors.IDexTreeVisitor;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.dex.nodes.ProcessState.LOADED;
......@@ -43,7 +42,7 @@ public final class ProcessClass {
cls.setState(PROCESS_COMPLETE);
}
} catch (Throwable e) {
ErrorsCounter.classError(cls, e.getClass().getSimpleName(), e);
cls.addError("Class process error: " + e.getClass().getSimpleName(), e);
}
}
}
......
......@@ -268,7 +268,7 @@ public class ClassGen {
inClGen.addClassCode(code);
imports.addAll(inClGen.getImports());
} catch (Exception e) {
ErrorsCounter.classError(innerCls, "Inner class code generation error", e);
innerCls.addError("Inner class code generation error", e);
}
}
......@@ -293,7 +293,7 @@ public class ClassGen {
throw new JadxRuntimeException("Method generation error", e);
}
code.newLine().add("/*");
code.newLine().addMultiLine(ErrorsCounter.methodError(mth, "Method generation error", e));
code.newLine().addMultiLine(ErrorsCounter.error(mth, "Method generation error", e));
Utils.appendStackTrace(code, e);
code.newLine().add("*/");
code.setIndent(savedIndent);
......@@ -455,7 +455,7 @@ public class ClassGen {
try {
insnGen.makeInsn(insn, code, InsnGen.Flags.BODY_ONLY_NOWRAP);
} catch (Exception e) {
ErrorsCounter.classError(cls, "Failed to generate init code", e);
cls.addError("Failed to generate init code", e);
}
}
......
......@@ -16,7 +16,6 @@ import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.regions.conditions.Compare;
import jadx.core.dex.regions.conditions.IfCondition;
import jadx.core.dex.regions.conditions.IfCondition.Mode;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.exceptions.JadxRuntimeException;
......@@ -123,7 +122,7 @@ public class ConditionGen extends InsnGen {
wrap(code, firstArg);
return;
}
ErrorsCounter.methodWarn(mth, "Unsupported boolean condition " + op.getSymbol());
mth.addWarn("Unsupported boolean condition " + op.getSymbol());
}
addArg(code, firstArg, isArgWrapNeeded(firstArg));
......
......@@ -39,7 +39,6 @@ import jadx.core.dex.regions.loops.LoopRegion;
import jadx.core.dex.regions.loops.LoopType;
import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.utils.BlockUtils;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.RegionUtils;
import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.exceptions.JadxRuntimeException;
......@@ -186,7 +185,7 @@ public class RegionGen extends InsnGen {
if (header != null) {
List<InsnNode> headerInsns = header.getInstructions();
if (headerInsns.size() > 1) {
ErrorsCounter.methodWarn(mth, "Found not inlined instructions from loop header");
mth.addWarn("Found not inlined instructions from loop header");
int last = headerInsns.size() - 1;
for (int i = 0; i < last; i++) {
InsnNode insn = headerInsns.get(i);
......
package jadx.core.dex.attributes.nodes;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.nodes.ICodeNode;
import jadx.core.utils.ErrorsCounter;
public abstract class NotificationAttrNode extends LineAttrNode implements ICodeNode {
private static final Logger LOG = LoggerFactory.getLogger(NotificationAttrNode.class);
public void addError(String errStr, Throwable e) {
ErrorsCounter.error(this, errStr, e);
}
public void addWarn(String warnStr) {
ErrorsCounter.warning(this, warnStr);
}
public void addWarnComment(String warn) {
addWarnComment(warn, null);
}
public void addWarnComment(String warn, @Nullable Throwable exc) {
String commentStr = "JADX WARN: " + warn;
addAttr(AType.COMMENTS, commentStr);
if (exc != null) {
LOG.warn("{} in {}", warn, this, exc);
} else {
LOG.warn("{} in {}", warn, this);
}
}
public void addComment(String commentStr) {
addAttr(AType.COMMENTS, commentStr);
LOG.info("{} in {}", commentStr, this);
}
}
......@@ -16,7 +16,6 @@ import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.DexNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.parser.FieldInitAttr;
import jadx.core.utils.ErrorsCounter;
public class ConstStorage {
......@@ -155,7 +154,7 @@ public class ConstStorage {
return innerClass.searchFieldByName(fieldName);
}
}
ErrorsCounter.classWarn(appResClass, "Not found resource field with id: " + value + ", name: " + str.replace('/', '.'));
appResClass.addWarn("Not found resource field with id: " + value + ", name: " + str.replace('/', '.'));
return null;
}
......
......@@ -25,7 +25,7 @@ import jadx.core.Consts;
import jadx.core.ProcessClass;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.annotations.Annotation;
import jadx.core.dex.attributes.nodes.LineAttrNode;
import jadx.core.dex.attributes.nodes.NotificationAttrNode;
import jadx.core.dex.attributes.nodes.SourceFileAttr;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.AccessInfo.AFType;
......@@ -45,7 +45,7 @@ import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.dex.nodes.ProcessState.LOADED;
import static jadx.core.dex.nodes.ProcessState.NOT_LOADED;
public class ClassNode extends LineAttrNode implements ILoadable, ICodeNode {
public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeNode {
private static final Logger LOG = LoggerFactory.getLogger(ClassNode.class);
private final DexNode dex;
......
......@@ -79,7 +79,7 @@ public class DexNode implements IDexNode {
name = "CLASS_" + typeIndex;
}
ClassNode clsNode = new ClassNode(this, name, classDef.getAccessFlags());
ErrorsCounter.classError(clsNode, "Load error", exc);
ErrorsCounter.error(clsNode, "Load error", exc);
addClassNode(clsNode);
}
......
......@@ -22,8 +22,8 @@ import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.annotations.Annotation;
import jadx.core.dex.attributes.nodes.JumpInfo;
import jadx.core.dex.attributes.nodes.LineAttrNode;
import jadx.core.dex.attributes.nodes.LoopInfo;
import jadx.core.dex.attributes.nodes.NotificationAttrNode;
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.AccessInfo.AFType;
import jadx.core.dex.info.ClassInfo;
......@@ -42,14 +42,13 @@ import jadx.core.dex.regions.Region;
import jadx.core.dex.trycatch.ExcHandlerAttr;
import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.dex.trycatch.TryCatchBlock;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.DecodeException;
import jadx.core.utils.exceptions.JadxRuntimeException;
import static jadx.core.utils.Utils.lockList;
public class MethodNode extends LineAttrNode implements IMethodDetails, ILoadable, ICodeNode {
public class MethodNode extends NotificationAttrNode implements IMethodDetails, ILoadable, ICodeNode {
private static final Logger LOG = LoggerFactory.getLogger(MethodNode.class);
private final MethodInfo mthInfo;
......@@ -203,9 +202,7 @@ public class MethodNode extends LineAttrNode implements IMethodDetails, ILoadabl
return null;
}
if (!tryFixArgsCounts(argsTypes, mthArgs)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Incorrect method signature, types: ({}), method: {}", Utils.listToString(argsTypes), this);
}
addComment("Incorrect method signature, types: " + Utils.listToString(argsTypes));
return null;
}
}
......@@ -715,33 +712,6 @@ public class MethodNode extends LineAttrNode implements IMethodDetails, ILoadabl
return "method";
}
public void addWarn(String warnStr) {
ErrorsCounter.methodWarn(this, warnStr);
}
public void addWarnComment(String warn) {
addWarnComment(warn, null);
}
public void addWarnComment(String warn, @Nullable Throwable exc) {
String commentStr = "JADX WARN: " + warn;
addAttr(AType.COMMENTS, commentStr);
if (exc != null) {
LOG.warn("{} in {}", warn, this, exc);
} else {
LOG.warn("{} in {}", warn, this);
}
}
public void addComment(String commentStr) {
addAttr(AType.COMMENTS, commentStr);
LOG.info("{} in {}", commentStr, this);
}
public void addError(String errStr, Throwable e) {
ErrorsCounter.methodError(this, errStr, e);
}
@Override
public MethodInfo getMethodInfo() {
return mthInfo;
......
......@@ -4,8 +4,6 @@ import jadx.core.dex.attributes.AType;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.DebugChecks;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.JadxOverflowException;
public class DepthTraversal {
......@@ -15,11 +13,8 @@ public class DepthTraversal {
cls.getInnerClasses().forEach(inCls -> visit(visitor, inCls));
cls.getMethods().forEach(mth -> visit(visitor, mth));
}
} catch (StackOverflowError e) {
ErrorsCounter.classError(cls, "StackOverflow in pass: " + visitor.getClass().getSimpleName(), new JadxOverflowException(""));
} catch (Exception e) {
ErrorsCounter.classError(cls,
e.getClass().getSimpleName() + " in pass: " + visitor.getClass().getSimpleName(), e);
} catch (StackOverflowError | Exception e) {
cls.addError(e.getClass().getSimpleName() + " in pass: " + visitor.getClass().getSimpleName(), e);
}
}
......@@ -32,11 +27,8 @@ public class DepthTraversal {
if (DebugChecks.checksEnabled) {
DebugChecks.runChecksAfterVisitor(mth, visitor);
}
} catch (StackOverflowError e) {
ErrorsCounter.methodError(mth, "StackOverflow in pass: " + visitor.getClass().getSimpleName(), new JadxOverflowException(""));
} catch (Exception e) {
ErrorsCounter.methodError(mth,
e.getClass().getSimpleName() + " in pass: " + visitor.getClass().getSimpleName(), e);
} catch (StackOverflowError | Exception e) {
mth.addError(e.getClass().getSimpleName() + " in pass: " + visitor.getClass().getSimpleName(), e);
}
}
......
......@@ -46,7 +46,6 @@ import jadx.core.dex.trycatch.ExcHandlerAttr;
import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.dex.visitors.regions.variables.ProcessVariables;
import jadx.core.dex.visitors.shrink.CodeShrinkVisitor;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.InsnRemover;
import jadx.core.utils.InsnUtils;
import jadx.core.utils.exceptions.JadxException;
......@@ -467,9 +466,8 @@ public class ModVisitor extends AbstractVisitor {
elType = insnElementType;
}
if (!elType.equals(insnElementType) && !insnArrayType.equals(ArgType.OBJECT)) {
ErrorsCounter.methodWarn(mth,
"Incorrect type for fill-array insn " + InsnUtils.formatOffset(insn.getOffset())
+ ", element type: " + elType + ", insn element type: " + insnElementType);
mth.addWarn("Incorrect type for fill-array insn " + InsnUtils.formatOffset(insn.getOffset())
+ ", element type: " + elType + ", insn element type: " + insnElementType);
}
if (!elType.isTypeKnown()) {
LOG.warn("Unknown array element type: {} in mth: {}", elType, mth);
......
......@@ -23,7 +23,6 @@ import jadx.core.dex.trycatch.ExceptionHandler;
import jadx.core.dex.trycatch.SplitterBlockAttr;
import jadx.core.dex.trycatch.TryCatchBlock;
import jadx.core.utils.BlockUtils;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.RegionUtils;
/**
......@@ -105,7 +104,7 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
if (region.getSubBlocks().contains(dominator)) {
TryCatchBlock tb = tryBlocksMap.get(dominator);
if (!wrapBlocks(region, tb, dominator)) {
ErrorsCounter.methodWarn(mth, "Can't wrap try/catch for region: " + region);
mth.addWarn("Can't wrap try/catch for region: " + region);
}
tryBlocksMap.remove(dominator);
return true;
......
......@@ -574,7 +574,7 @@ public class RegionMaker {
BlockNode body = getNextBlock(block);
if (body == null) {
ErrorsCounter.methodWarn(mth, "Unexpected end of synchronized block");
mth.addWarn("Unexpected end of synchronized block");
return null;
}
BlockNode exit = null;
......
......@@ -14,7 +14,6 @@ import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
import jadx.core.dex.attributes.IAttributeNode;
import jadx.core.dex.attributes.nodes.JadxError;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.IDexNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.utils.exceptions.JadxOverflowException;
......@@ -28,12 +27,16 @@ public class ErrorsCounter {
private final Set<IAttributeNode> warnNodes = new HashSet<>();
private int warnsCount;
public int getErrorCount() {
return errorsCount;
public static <N extends IDexNode & IAttributeNode> String error(N node, String warnMsg, Throwable th) {
return node.root().getErrorsCounter().addError(node, warnMsg, th);
}
public int getWarnsCount() {
return warnsCount;
public static <N extends IDexNode & IAttributeNode> String warning(N node, String warnMsg) {
return node.root().getErrorsCounter().addWarning(node, warnMsg);
}
public static String formatMsg(IDexNode node, String msg) {
return msg + " in " + node.typeName() + ": " + node + ", dex: " + node.dex().getDexFile().getName();
}
private synchronized <N extends IDexNode & IAttributeNode> String addError(N node, String error, @Nullable Throwable e) {
......@@ -47,10 +50,17 @@ public class ErrorsCounter {
}
if (e == null) {
LOG.error(msg);
} else if (e instanceof StackOverflowError) {
LOG.error(msg);
} else if (e instanceof JadxOverflowException) {
// don't print full stack trace
e = new JadxOverflowException(e.getMessage());
LOG.error("{}, details: {}", msg, e.getMessage());
String details = e.getMessage();
e = new JadxOverflowException(details);
if (details == null || details.isEmpty()) {
LOG.error("{}", msg);
} else {
LOG.error("{}, details: {}", msg, details);
}
} else {
LOG.error(msg, e);
}
......@@ -74,26 +84,6 @@ public class ErrorsCounter {
return msg;
}
public static String classError(ClassNode cls, String errorMsg, Throwable e) {
return cls.dex().root().getErrorsCounter().addError(cls, errorMsg, e);
}
public static String classWarn(ClassNode cls, String warnMsg) {
return cls.dex().root().getErrorsCounter().addWarning(cls, warnMsg);
}
public static String methodError(MethodNode mth, String errorMsg, Throwable e) {
return mth.root().getErrorsCounter().addError(mth, errorMsg, e);
}
public static String methodWarn(MethodNode mth, String warnMsg) {
return mth.root().getErrorsCounter().addWarning(mth, warnMsg);
}
public static String formatMsg(IDexNode node, String msg) {
return msg + " in " + node.typeName() + ": " + node + ", dex: " + node.dex().getDexFile().getName();
}
public void printReport() {
if (getErrorCount() > 0) {
LOG.error("{} errors occurred in following nodes:", getErrorCount());
......@@ -111,4 +101,20 @@ public class ErrorsCounter {
LOG.warn("{} warnings in {} nodes", getWarnsCount(), warnNodes.size());
}
}
public int getErrorCount() {
return errorsCount;
}
public int getWarnsCount() {
return warnsCount;
}
public Set<IAttributeNode> getErrorNodes() {
return errorNodes;
}
public Set<IAttributeNode> getWarnNodes() {
return warnNodes;
}
}
......@@ -158,14 +158,17 @@ public class Utils {
private static void filter(Throwable th) {
StackTraceElement[] stackTrace = th.getStackTrace();
int length = stackTrace.length;
StackTraceElement prevElement = null;
for (int i = 0; i < length; i++) {
StackTraceElement stackTraceElement = stackTrace[i];
String clsName = stackTraceElement.getClassName();
if (clsName.equals(STACKTRACE_STOP_CLS_NAME)
|| clsName.startsWith(JADX_API_PACKAGE)) {
|| clsName.startsWith(JADX_API_PACKAGE)
|| Objects.equals(prevElement, stackTraceElement)) {
th.setStackTrace(Arrays.copyOfRange(stackTrace, 0, i));
return;
}
prevElement = stackTraceElement;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册