From 75d8a01cabf2248295617022b9666f550fba1dea Mon Sep 17 00:00:00 2001 From: Skylot Date: Mon, 28 Jul 2014 22:24:25 +0400 Subject: [PATCH] core: improve error reporting --- .../main/java/jadx/core/codegen/ClassGen.java | 8 ++- .../java/jadx/core/codegen/MethodGen.java | 57 +++++++------------ .../main/java/jadx/core/codegen/NameGen.java | 2 +- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java b/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java index e664162b9..f2c5ed829 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java +++ b/jadx-core/src/main/java/jadx/core/codegen/ClassGen.java @@ -252,8 +252,8 @@ public class ClassGen { } private void addMethod(CodeWriter code, MethodNode mth) throws CodegenException { - MethodGen mthGen = new MethodGen(this, mth); if (mth.getAccessFlags().isAbstract() || mth.getAccessFlags().isNative()) { + MethodGen mthGen = new MethodGen(this, mth); mthGen.addDefinition(code); if (cls.getAccessFlags().isAnnotation()) { Object def = annotationGen.getAnnotationDefaultValue(mth.getName()); @@ -270,6 +270,12 @@ public class ClassGen { code.startLine("/* Code decompiled incorrectly, please refer to instructions dump. */"); ErrorsCounter.methodError(mth, "Inconsistent code"); } + MethodGen mthGen; + if (badCode || mth.contains(AType.JADX_ERROR)) { + mthGen = MethodGen.getFallbackMethodGen(mth); + } else { + mthGen = new MethodGen(this, mth); + } if (mthGen.addDefinition(code)) { code.add(' '); } diff --git a/jadx-core/src/main/java/jadx/core/codegen/MethodGen.java b/jadx-core/src/main/java/jadx/core/codegen/MethodGen.java index 3c8b00bee..780c15720 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/MethodGen.java +++ b/jadx-core/src/main/java/jadx/core/codegen/MethodGen.java @@ -9,7 +9,6 @@ import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.RegisterArg; import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.MethodNode; -import jadx.core.dex.regions.Region; import jadx.core.dex.trycatch.CatchAttr; import jadx.core.dex.visitors.DepthTraversal; import jadx.core.dex.visitors.FallbackModeVisitor; @@ -151,49 +150,33 @@ public class MethodGen { } public void addInstructions(CodeWriter code) throws CodegenException { - if (mth.contains(AType.JADX_ERROR)) { - code.startLine("throw new UnsupportedOperationException(\"Method not decompiled: "); - code.add(mth.toString()); - code.add("\");"); + if (mth.contains(AType.JADX_ERROR) + || mth.contains(AFlag.INCONSISTENT_CODE) + || mth.getRegion() == null) { + code.startLine("throw new UnsupportedOperationException(\"Method not decompiled: ") + .add(mth.toString()) + .add("\");"); - JadxErrorAttr err = mth.get(AType.JADX_ERROR); - code.startLine("/* JADX: method processing error */"); - Throwable cause = err.getCause(); - if (cause != null) { - code.newLine(); - code.add("/*"); - code.startLine("Error: ").add(Utils.getStackTrace(cause)); - code.add("*/"); + if (mth.contains(AType.JADX_ERROR)) { + JadxErrorAttr err = mth.get(AType.JADX_ERROR); + code.startLine("/* JADX: method processing error */"); + Throwable cause = err.getCause(); + if (cause != null) { + code.newLine(); + code.add("/*"); + code.startLine("Error: ").add(Utils.getStackTrace(cause)); + code.add("*/"); + } } - makeMethodDump(code); - } else if (mth.contains(AFlag.INCONSISTENT_CODE)) { code.startLine("/*"); addFallbackMethodCode(code); code.startLine("*/"); - code.newLine(); } else { - Region startRegion = mth.getRegion(); - if (startRegion != null) { - (new RegionGen(this)).makeRegion(code, startRegion); - } else { - addFallbackMethodCode(code); - } + RegionGen regionGen = new RegionGen(this); + regionGen.makeRegion(code, mth.getRegion()); } } - private void makeMethodDump(CodeWriter code) { - code.startLine("/*"); - getFallbackMethodGen(mth).addDefinition(code); - code.add(" {"); - code.incIndent(); - - addFallbackMethodCode(code); - - code.decIndent(); - code.startLine('}'); - code.startLine("*/"); - } - public void addFallbackMethodCode(CodeWriter code) { if (mth.getInstructions() == null) { // load original instructions @@ -212,7 +195,7 @@ public class MethodGen { return; } if (mth.getThisArg() != null) { - code.startLine(getFallbackMethodGen(mth).nameGen.useArg(mth.getThisArg())).add(" = this;"); + code.startLine(nameGen.useArg(mth.getThisArg())).add(" = this;"); } addFallbackInsns(code, mth, insnArr, true); } @@ -248,7 +231,7 @@ public class MethodGen { /** * Return fallback variant of method codegen */ - private static MethodGen getFallbackMethodGen(MethodNode mth) { + static MethodGen getFallbackMethodGen(MethodNode mth) { ClassGen clsGen = new ClassGen(mth.getParentClass(), null, true); return new MethodGen(clsGen, mth); } diff --git a/jadx-core/src/main/java/jadx/core/codegen/NameGen.java b/jadx-core/src/main/java/jadx/core/codegen/NameGen.java index a59e4741a..844d20496 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/NameGen.java +++ b/jadx-core/src/main/java/jadx/core/codegen/NameGen.java @@ -91,7 +91,7 @@ public class NameGen { String name = arg.getName(); if (fallback) { String base = "r" + arg.getRegNum(); - if (name != null) { + if (name != null && !name.equals("this")) { return base + "_" + name; } return base;