提交 c7ed9857 编写于 作者: S Skylot

Various code improvements

上级 a6f61151
......@@ -54,9 +54,6 @@ public class InsnGen {
NO_RESULT,
BODY_ONLY,
INC_INDENT,
DEC_INDENT,
}
public InsnGen(MethodGen mgen, MethodNode mth, boolean fallback) {
......@@ -92,12 +89,12 @@ public class InsnGen {
if (insn.getAttributes().contains(AttributeType.DECLARE_VARIABLE)) {
return declareVar(arg);
} else {
return arg(arg);
return mgen.makeArgName(arg);
}
} catch (CodegenException e) {
LOG.error("Assign var codegen error", e);
return "<error>";
}
return "<error>";
}
public String declareVar(RegisterArg arg) throws CodegenException {
......@@ -142,11 +139,11 @@ public class InsnGen {
return TypeGen.translate(mgen.getClassGen(), type);
}
public void makeInsn(InsnNode insn, CodeWriter code) throws CodegenException {
makeInsn(insn, code, false);
public boolean makeInsn(InsnNode insn, CodeWriter code) throws CodegenException {
return makeInsn(insn, code, false);
}
private void makeInsn(InsnNode insn, CodeWriter code, boolean bodyOnly) throws CodegenException {
private boolean makeInsn(InsnNode insn, CodeWriter code, boolean bodyOnly) throws CodegenException {
try {
EnumSet<InsnGenState> state = EnumSet.noneOf(InsnGenState.class);
if (bodyOnly) {
......@@ -156,10 +153,8 @@ public class InsnGen {
CodeWriter body = new CodeWriter(code.getIndent());
makeInsnBody(body, insn, state);
if (state.contains(InsnGenState.SKIP))
return;
return false;
if (state.contains(InsnGenState.DEC_INDENT))
code.decIndent();
if (insn.getResult() != null && !state.contains(InsnGenState.NO_RESULT))
code.startLine(assignVar(insn)).add(" = ");
else
......@@ -169,12 +164,11 @@ public class InsnGen {
if (!state.contains(InsnGenState.NO_SEMICOLON))
code.add(';');
if (state.contains(InsnGenState.INC_INDENT))
code.incIndent();
}
} catch (Throwable th) {
throw new CodegenException(mth, "Error generate insn: " + insn, th);
}
return true;
}
private void makeInsnBody(CodeWriter code, InsnNode insn, EnumSet<InsnGenState> state) throws CodegenException {
......@@ -549,7 +543,7 @@ public class InsnGen {
private void addArgs(CodeWriter code, InsnNode insn, int k) throws CodegenException {
code.add('(');
for (int i = k; i < insn.getArgsCount(); i++) {
code.add(arg(insn.getArg(i)));
code.add(arg(insn, i));
if (i < insn.getArgsCount() - 1)
code.add(", ");
}
......@@ -562,6 +556,7 @@ public class InsnGen {
if (op == ArithOp.INC || op == ArithOp.DEC) {
code.add(v1 + op.getSymbol());
state.add(InsnGenState.NO_RESULT);
} else {
String res = arg(insn.getResult());
String v2 = arg(insn.getArg(1));
......
......@@ -60,9 +60,9 @@ public class MethodGen {
if (mth.getMethodInfo().isClassInit()) {
code.startLine("static");
} else {
if (mth.getAttributes().contains(AttributeFlag.INCONSISTENT_CODE)) {
code.startLine("// FIXME: Jadx generate inconsistent code");
LOG.debug(ErrorsCounter.formatErrorMsg(mth, " Inconsistent code"));
if (mth.getAttributes().contains(AttributeFlag.INCONSISTENT_CODE)
&& !mth.getAttributes().contains(AttributeType.JADX_ERROR)) {
code.startLine("// jadx: inconsistent code");
}
annotationGen.addForMethod(code, mth);
......@@ -217,32 +217,24 @@ public class MethodGen {
code.add("\");");
JadxErrorAttr err = (JadxErrorAttr) mth.getAttributes().get(AttributeType.JADX_ERROR);
code.startLine("// FIXME: Jadx error processing method");
code.startLine("// jadx: method processing error");
Throwable cause = err.getCause();
if (cause != null) {
code.endl().add("/*");
code.startLine("Message: ").add(cause.getMessage());
code.endl();
code.add("/*");
code.startLine("Error: ").add(Utils.getStackTrace(cause));
code.add("*/");
}
// load original instructions
try {
mth.load();
DepthTraverser.visit(new FallbackModeVisitor(), mth);
} catch (DecodeException e) {
// ignore
return code;
}
code.startLine("/*");
makeFullMethodDump(code, mth);
code.startLine("*/");
makeMethodDump(code, mth);
} else {
if (mth.getRegion() != null) {
CodeWriter insns = new CodeWriter(mthIndent + 1);
(new RegionGen(this, mth)).makeRegion(insns, mth.getRegion());
if (mth.getAttributes().contains(AttributeFlag.INCONSISTENT_CODE)) {
LOG.debug(ErrorsCounter.formatErrorMsg(mth, " Inconsistent code"));
// makeMethodDump(code, mth);
}
makeInitCode(code);
code.add(insns);
} else {
......@@ -252,7 +244,8 @@ public class MethodGen {
return code;
}
private void makeFullMethodDump(CodeWriter code, MethodNode mth) {
public void makeMethodDump(CodeWriter code, MethodNode mth) {
code.startLine("/*");
getFallbackMethodGen(mth).addDefinition(code);
code.add(" {");
code.incIndent();
......@@ -261,9 +254,21 @@ public class MethodGen {
code.decIndent();
code.startLine("}");
code.startLine("*/");
}
private void makeFallbackMethod(CodeWriter code, MethodNode mth) {
if (mth.getInstructions() == null) {
// load original instructions
try {
mth.load();
DepthTraverser.visit(new FallbackModeVisitor(), mth);
} catch (DecodeException e) {
// ignore
code.startLine("Can't load method instructions");
return;
}
}
if (mth.getThisArg() != null) {
code.startLine(getFallbackMethodGen(mth).makeArgName(mth.getThisArg())).add(" = this;");
}
......@@ -283,13 +288,14 @@ public class MethodGen {
}
}
try {
insnGen.makeInsn(insn, code);
if (insnGen.makeInsn(insn, code)) {
CatchAttr _catch = (CatchAttr) attrs.get(AttributeType.CATCH_BLOCK);
if (_catch != null)
code.add("\t //" + _catch);
}
} catch (CodegenException e) {
code.startLine("// error: " + insn);
}
CatchAttr _catch = (CatchAttr) attrs.get(AttributeType.CATCH_BLOCK);
if (_catch != null)
code.add("\t // " + _catch);
}
}
......
......@@ -28,6 +28,8 @@ public class ExcHandlerAttr implements IAttribute {
@Override
public String toString() {
return "ExcHandler: " + (handler.isCatchAll() ? "all" : handler.getCatchType());
return "ExcHandler: "
+ (handler.isCatchAll() ? "all" : handler.getCatchType())
+ " " + handler.getArg();
}
}
......@@ -71,7 +71,7 @@ public class ExceptionHandler {
@Override
public int hashCode() {
return 31 * (catchType == null ? 0 : catchType.hashCode()) + handleOffset;
return (catchType == null ? 0 : 31 * catchType.hashCode()) + handleOffset;
}
@Override
......@@ -88,8 +88,8 @@ public class ExceptionHandler {
@Override
public String toString() {
return (catchType == null ? "all" : catchType.getShortName())
+ " -> " + InsnUtils.formatOffset(handleOffset);
return (catchType == null ? "all"
: catchType.getShortName()) + " -> " + InsnUtils.formatOffset(handleOffset);
}
}
......@@ -117,9 +117,7 @@ public class TryCatchBlock {
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TryCatchBlock other = (TryCatchBlock) obj;
if (handlers == null) {
if (other.handlers != null) return false;
} else if (!handlers.equals(other.handlers)) return false;
if (!handlers.equals(other.handlers)) return false;
return true;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册