Commit 50d31444 authored by Skylot's avatar Skylot

core: fix code style

parent f8d57d92
...@@ -77,29 +77,28 @@ public class ClspGraph { ...@@ -77,29 +77,28 @@ public class ClspGraph {
return clsName; return clsName;
} }
NClass cls = nameMap.get(implClsName); NClass cls = nameMap.get(implClsName);
if (cls != null) { if (cls == null) {
LOG.debug("Missing class: {}", implClsName);
return null;
}
if (isImplements(clsName, implClsName)) { if (isImplements(clsName, implClsName)) {
return implClsName; return implClsName;
} }
Set<String> anc = getAncestors(clsName); Set<String> anc = getAncestors(clsName);
return searchCommonParent(anc, cls); return searchCommonParent(anc, cls);
} }
LOG.debug("Missing class: {}", implClsName);
return null;
}
private String searchCommonParent(Set<String> anc, NClass cls) { private String searchCommonParent(Set<String> anc, NClass cls) {
for (NClass p : cls.getParents()) { for (NClass p : cls.getParents()) {
String name = p.getName(); String name = p.getName();
if (anc.contains(name)) { if (anc.contains(name)) {
return name; return name;
} else { }
String r = searchCommonParent(anc, p); String r = searchCommonParent(anc, p);
if (r != null) { if (r != null) {
return r; return r;
} }
} }
}
return null; return null;
} }
...@@ -109,7 +108,10 @@ public class ClspGraph { ...@@ -109,7 +108,10 @@ public class ClspGraph {
return result; return result;
} }
NClass cls = nameMap.get(clsName); NClass cls = nameMap.get(clsName);
if (cls != null) { if (cls == null) {
LOG.debug("Missing class: {}", clsName);
return Collections.emptySet();
}
result = new HashSet<String>(); result = new HashSet<String>();
addAncestorsNames(cls, result); addAncestorsNames(cls, result);
if (result.isEmpty()) { if (result.isEmpty()) {
...@@ -118,9 +120,6 @@ public class ClspGraph { ...@@ -118,9 +120,6 @@ public class ClspGraph {
ancestorCache.put(clsName, result); ancestorCache.put(clsName, result);
return result; return result;
} }
LOG.debug("Missing class: {}", clsName);
return Collections.emptySet();
}
private void addAncestorsNames(NClass cls, Set<String> result) { private void addAncestorsNames(NClass cls, Set<String> result) {
result.add(cls.getName()); result.add(cls.getName());
......
...@@ -10,6 +10,7 @@ import jadx.core.dex.instructions.args.LiteralArg; ...@@ -10,6 +10,7 @@ import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.regions.Compare; import jadx.core.dex.regions.Compare;
import jadx.core.dex.regions.IfCondition; import jadx.core.dex.regions.IfCondition;
import jadx.core.dex.regions.IfCondition.Mode;
import jadx.core.utils.ErrorsCounter; import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.CodegenException; import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
...@@ -99,7 +100,7 @@ public class ConditionGen extends InsnGen { ...@@ -99,7 +100,7 @@ public class ConditionGen extends InsnGen {
} }
private void addAndOr(CodeWriter code, IfCondition condition) throws CodegenException { private void addAndOr(CodeWriter code, IfCondition condition) throws CodegenException {
String mode = condition.getMode() == IfCondition.Mode.AND ? " && " : " || "; String mode = condition.getMode() == Mode.AND ? " && " : " || ";
Iterator<IfCondition> it = condition.getArgs().iterator(); Iterator<IfCondition> it = condition.getArgs().iterator();
while (it.hasNext()) { while (it.hasNext()) {
wrap(code, it.next()); wrap(code, it.next());
...@@ -110,7 +111,7 @@ public class ConditionGen extends InsnGen { ...@@ -110,7 +111,7 @@ public class ConditionGen extends InsnGen {
} }
private boolean isWrapNeeded(IfCondition condition) { private boolean isWrapNeeded(IfCondition condition) {
return !condition.isCompare() && condition.getMode() != IfCondition.Mode.NOT; return !condition.isCompare() && condition.getMode() != Mode.NOT;
} }
private static boolean isArgWrapNeeded(InsnArg arg) { private static boolean isArgWrapNeeded(InsnArg arg) {
......
...@@ -58,7 +58,7 @@ public class InsnGen { ...@@ -58,7 +58,7 @@ public class InsnGen {
protected final RootNode root; protected final RootNode root;
protected final boolean fallback; protected final boolean fallback;
private static enum Flags { private enum Flags {
BODY_ONLY, BODY_ONLY,
BODY_ONLY_NOWRAP, BODY_ONLY_NOWRAP,
} }
......
...@@ -132,7 +132,7 @@ public class MethodGen { ...@@ -132,7 +132,7 @@ public class MethodGen {
if (type.isArray()) { if (type.isArray()) {
ArgType elType = type.getArrayElement(); ArgType elType = type.getArrayElement();
classGen.useType(argsCode, elType); classGen.useType(argsCode, elType);
argsCode.add(" ..."); argsCode.add("...");
} else { } else {
LOG.warn(ErrorsCounter.formatErrorMsg(mth, "Last argument in varargs method not array")); LOG.warn(ErrorsCounter.formatErrorMsg(mth, "Last argument in varargs method not array"));
classGen.useType(argsCode, arg.getType()); classGen.useType(argsCode, arg.getType());
......
...@@ -9,7 +9,7 @@ import jadx.core.utils.exceptions.JadxRuntimeException; ...@@ -9,7 +9,7 @@ import jadx.core.utils.exceptions.JadxRuntimeException;
public class TypeGen { public class TypeGen {
public static String signature(ArgType type) { public static String signature(ArgType type) {
final PrimitiveType stype = type.getPrimitiveType(); PrimitiveType stype = type.getPrimitiveType();
if (stype == PrimitiveType.OBJECT) { if (stype == PrimitiveType.OBJECT) {
return Utils.makeQualifiedObjectName(type.getObject()); return Utils.makeQualifiedObjectName(type.getObject());
} }
......
...@@ -6,7 +6,7 @@ import java.util.Map; ...@@ -6,7 +6,7 @@ import java.util.Map;
public class Annotation { public class Annotation {
public static enum Visibility { public enum Visibility {
BUILD, RUNTIME, SYSTEM BUILD, RUNTIME, SYSTEM
} }
......
...@@ -53,7 +53,7 @@ public class EnumClassAttr implements IAttribute { ...@@ -53,7 +53,7 @@ public class EnumClassAttr implements IAttribute {
private MethodNode staticMethod; private MethodNode staticMethod;
public EnumClassAttr(int fieldsCount) { public EnumClassAttr(int fieldsCount) {
this.fields = new ArrayList<EnumClassAttr.EnumField>(fieldsCount); this.fields = new ArrayList<EnumField>(fieldsCount);
} }
public List<EnumField> getFields() { public List<EnumField> getFields() {
......
...@@ -8,7 +8,7 @@ public class AccessInfo { ...@@ -8,7 +8,7 @@ public class AccessInfo {
private final int accFlags; private final int accFlags;
public static enum AFType { public enum AFType {
CLASS, FIELD, METHOD CLASS, FIELD, METHOD
} }
......
...@@ -17,7 +17,7 @@ public enum ArithOp { ...@@ -17,7 +17,7 @@ public enum ArithOp {
private final String symbol; private final String symbol;
private ArithOp(String symbol) { ArithOp(String symbol) {
this.symbol = symbol; this.symbol = symbol;
} }
......
...@@ -12,7 +12,7 @@ public enum IfOp { ...@@ -12,7 +12,7 @@ public enum IfOp {
private final String symbol; private final String symbol;
private IfOp(String symbol) { IfOp(String symbol) {
this.symbol = symbol; this.symbol = symbol;
} }
...@@ -23,19 +23,19 @@ public enum IfOp { ...@@ -23,19 +23,19 @@ public enum IfOp {
public IfOp invert() { public IfOp invert() {
switch (this) { switch (this) {
case EQ: case EQ:
return IfOp.NE; return NE;
case NE: case NE:
return IfOp.EQ; return EQ;
case LT: case LT:
return IfOp.GE; return GE;
case LE: case LE:
return IfOp.GT; return GT;
case GT: case GT:
return IfOp.LE; return LE;
case GE: case GE:
return IfOp.LT; return LT;
default: default:
throw new JadxRuntimeException("Unknown if operations type: " + this); throw new JadxRuntimeException("Unknown if operations type: " + this);
......
...@@ -48,6 +48,10 @@ public abstract class ArgType { ...@@ -48,6 +48,10 @@ public abstract class ArgType {
ArgType.clsp = clsp; ArgType.clsp = clsp;
} }
public static boolean isClspSet() {
return ArgType.clsp != null;
}
private static ArgType primitive(PrimitiveType stype) { private static ArgType primitive(PrimitiveType stype) {
return new PrimitiveArg(stype); return new PrimitiveArg(stype);
} }
...@@ -174,7 +178,7 @@ public abstract class ArgType { ...@@ -174,7 +178,7 @@ public abstract class ArgType {
private final int bounds; private final int bounds;
public WildcardType(ArgType obj, int bound) { public WildcardType(ArgType obj, int bound) {
super(ArgType.OBJECT.getObject()); super(OBJECT.getObject());
this.type = obj; this.type = obj;
this.bounds = bound; this.bounds = bound;
} }
...@@ -214,7 +218,7 @@ public abstract class ArgType { ...@@ -214,7 +218,7 @@ public abstract class ArgType {
if (bounds == 0) { if (bounds == 0) {
return "?"; return "?";
} }
return "? " + (bounds == -1 ? "super" : "extends") + " " + type.toString(); return "? " + (bounds == -1 ? "super" : "extends") + " " + type;
} }
} }
...@@ -398,7 +402,7 @@ public abstract class ArgType { ...@@ -398,7 +402,7 @@ public abstract class ArgType {
} }
/** /**
* @see jadx.core.dex.instructions.args.ArgType.WildcardType#getWildcardBounds() * @see WildcardType#getWildcardBounds()
*/ */
public int getWildcardBounds() { public int getWildcardBounds() {
return 0; return 0;
...@@ -513,7 +517,7 @@ public abstract class ArgType { ...@@ -513,7 +517,7 @@ public abstract class ArgType {
return OBJECT; return OBJECT;
} else { } else {
ArgType res = merge(ea, eb); ArgType res = merge(ea, eb);
return res == null ? null : ArgType.array(res); return res == null ? null : array(res);
} }
} else if (b.equals(OBJECT)) { } else if (b.equals(OBJECT)) {
return OBJECT; return OBJECT;
......
...@@ -2,7 +2,7 @@ package jadx.core.dex.instructions.args; ...@@ -2,7 +2,7 @@ package jadx.core.dex.instructions.args;
public class MthParameterArg extends RegisterArg { public class MthParameterArg extends RegisterArg {
private boolean isThis = false; private boolean isThis;
public MthParameterArg(int rn, ArgType type) { public MthParameterArg(int rn, ArgType type) {
super(rn, type); super(rn, type);
......
...@@ -16,7 +16,7 @@ public enum PrimitiveType { ...@@ -16,7 +16,7 @@ public enum PrimitiveType {
private final String shortName; private final String shortName;
private final String longName; private final String longName;
private PrimitiveType(String shortName, String longName) { PrimitiveType(String shortName, String longName) {
this.shortName = shortName; this.shortName = shortName;
this.longName = longName; this.longName = longName;
} }
......
...@@ -14,7 +14,7 @@ public class ConstructorInsn extends InsnNode { ...@@ -14,7 +14,7 @@ public class ConstructorInsn extends InsnNode {
private final CallType callType; private final CallType callType;
private final RegisterArg instanceArg; private final RegisterArg instanceArg;
private static enum CallType { private enum CallType {
CONSTRUCTOR, // just new instance CONSTRUCTOR, // just new instance
SUPER, // super call SUPER, // super call
THIS, // call constructor from other constructor THIS, // call constructor from other constructor
......
...@@ -48,12 +48,14 @@ public class RootNode { ...@@ -48,12 +48,14 @@ public class RootNode {
} }
private static void initClassPath(List<ClassNode> classes) throws IOException, DecodeException { private static void initClassPath(List<ClassNode> classes) throws IOException, DecodeException {
if (!ArgType.isClspSet()) {
ClspGraph clsp = new ClspGraph(); ClspGraph clsp = new ClspGraph();
clsp.load(); clsp.load();
clsp.addApp(classes); clsp.addApp(classes);
ArgType.setClsp(clsp); ArgType.setClsp(clsp);
} }
}
private void initInnerClasses(List<ClassNode> classes) { private void initInnerClasses(List<ClassNode> classes) {
// move inner classes // move inner classes
......
...@@ -20,7 +20,7 @@ import com.android.dex.Dex.Section; ...@@ -20,7 +20,7 @@ import com.android.dex.Dex.Section;
public class AnnotationsParser { public class AnnotationsParser {
private static final Annotation.Visibility[] VISIBILITIES = { private static final Visibility[] VISIBILITIES = {
Visibility.BUILD, Visibility.BUILD,
Visibility.RUNTIME, Visibility.RUNTIME,
Visibility.SYSTEM Visibility.SYSTEM
......
...@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory; ...@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
final class LocalVar { final class LocalVar {
private static final Logger LOG = LoggerFactory.getLogger(LocalVar.class); private static final Logger LOG = LoggerFactory.getLogger(LocalVar.class);
private int regNum; private final int regNum;
private String name; private String name;
private ArgType type; private ArgType type;
......
...@@ -20,7 +20,7 @@ import java.util.List; ...@@ -20,7 +20,7 @@ import java.util.List;
public final class IfCondition { public final class IfCondition {
public static enum Mode { public enum Mode {
COMPARE, COMPARE,
NOT, NOT,
AND, AND,
......
...@@ -7,6 +7,7 @@ import jadx.core.dex.nodes.IContainer; ...@@ -7,6 +7,7 @@ import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.IRegion; import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.regions.IfCondition; import jadx.core.dex.regions.IfCondition;
import jadx.core.dex.regions.IfCondition.Mode;
import jadx.core.dex.regions.IfRegion; import jadx.core.dex.regions.IfRegion;
import jadx.core.dex.regions.Region; import jadx.core.dex.regions.Region;
import jadx.core.dex.visitors.AbstractVisitor; import jadx.core.dex.visitors.AbstractVisitor;
...@@ -58,7 +59,7 @@ public class IfRegionVisitor extends AbstractVisitor implements IRegionVisitor, ...@@ -58,7 +59,7 @@ public class IfRegionVisitor extends AbstractVisitor implements IRegionVisitor,
private static void simplifyIfCondition(IfRegion ifRegion) { private static void simplifyIfCondition(IfRegion ifRegion) {
if (ifRegion.simplifyCondition()) { if (ifRegion.simplifyCondition()) {
IfCondition condition = ifRegion.getCondition(); IfCondition condition = ifRegion.getCondition();
if (condition.getMode() == IfCondition.Mode.NOT) { if (condition.getMode() == Mode.NOT) {
invertIfRegion(ifRegion); invertIfRegion(ifRegion);
} }
} }
......
...@@ -403,12 +403,12 @@ public class RegionMaker { ...@@ -403,12 +403,12 @@ public class RegionMaker {
// select 'then', 'else' and 'exit' blocks // select 'then', 'else' and 'exit' blocks
if (bElse.getPredecessors().size() != 1 if (bElse.getPredecessors().size() != 1
&& BlockUtils.isPathExists(bThen, bElse)) { && isPathExists(bThen, bElse)) {
thenBlock = bThen; thenBlock = bThen;
elseBlock = null; elseBlock = null;
out = bElse; out = bElse;
} else if (bThen.getPredecessors().size() != 1 } else if (bThen.getPredecessors().size() != 1
&& BlockUtils.isPathExists(bElse, bThen)) { && isPathExists(bElse, bThen)) {
ifnode.invertCondition(); ifnode.invertCondition();
thenBlock = ifnode.getThenBlock(); thenBlock = ifnode.getThenBlock();
elseBlock = null; elseBlock = null;
...@@ -511,7 +511,7 @@ public class RegionMaker { ...@@ -511,7 +511,7 @@ public class RegionMaker {
for (BlockNode maybeOut : block.getSuccessors()) { for (BlockNode maybeOut : block.getSuccessors()) {
boolean allReached = true; boolean allReached = true;
for (BlockNode s : block.getSuccessors()) { for (BlockNode s : block.getSuccessors()) {
if (!BlockUtils.isPathExists(s, maybeOut)) { if (!isPathExists(s, maybeOut)) {
allReached = false; allReached = false;
break; break;
} }
......
...@@ -51,6 +51,11 @@ public abstract class InternalJadxTest extends TestUtils { ...@@ -51,6 +51,11 @@ public abstract class InternalJadxTest extends TestUtils {
} }
// don't unload class // don't unload class
checkCode(cls);
return cls;
}
private static void checkCode(ClassNode cls) {
assertTrue("Inconsistent cls: " + cls, assertTrue("Inconsistent cls: " + cls,
!cls.contains(AFlag.INCONSISTENT_CODE) && !cls.contains(AType.JADX_ERROR)); !cls.contains(AFlag.INCONSISTENT_CODE) && !cls.contains(AType.JADX_ERROR));
for (MethodNode mthNode : cls.getMethods()) { for (MethodNode mthNode : cls.getMethods()) {
...@@ -58,7 +63,6 @@ public abstract class InternalJadxTest extends TestUtils { ...@@ -58,7 +63,6 @@ public abstract class InternalJadxTest extends TestUtils {
!mthNode.contains(AFlag.INCONSISTENT_CODE) && !mthNode.contains(AType.JADX_ERROR)); !mthNode.contains(AFlag.INCONSISTENT_CODE) && !mthNode.contains(AType.JADX_ERROR));
} }
assertThat(cls.getCode().toString(), not(containsString("inconsistent"))); assertThat(cls.getCode().toString(), not(containsString("inconsistent")));
return cls;
} }
protected List<IDexTreeVisitor> getPasses() { protected List<IDexTreeVisitor> getPasses() {
......
...@@ -5,6 +5,9 @@ import jadx.core.codegen.CodeWriter; ...@@ -5,6 +5,9 @@ import jadx.core.codegen.CodeWriter;
public class TestUtils { public class TestUtils {
public static String indent(int indent) { public static String indent(int indent) {
if (indent == 1) {
return CodeWriter.INDENT;
}
StringBuilder sb = new StringBuilder(indent * CodeWriter.INDENT.length()); StringBuilder sb = new StringBuilder(indent * CodeWriter.INDENT.length());
for (int i = 0; i < indent; i++) { for (int i = 0; i < indent; i++) {
sb.append(CodeWriter.INDENT); sb.append(CodeWriter.INDENT);
......
...@@ -5,6 +5,7 @@ import jadx.core.dex.nodes.ClassNode; ...@@ -5,6 +5,7 @@ import jadx.core.dex.nodes.ClassNode;
import org.junit.Test; import org.junit.Test;
import static jadx.tests.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -52,13 +53,14 @@ public class TestAnnotations extends InternalJadxTest { ...@@ -52,13 +53,14 @@ public class TestAnnotations extends InternalJadxTest {
System.out.println(code); System.out.println(code);
assertThat(code, not(containsString("@A(a = 255)"))); assertThat(code, not(containsString("@A(a = 255)")));
assertThat(code, containsString("@A(a = -1)")); assertThat(code, containsOne("@A(a = -1)"));
assertThat(code, containsString("@A(a = -253)")); assertThat(code, containsOne("@A(a = -253)"));
assertThat(code, containsString("@A(a = -11253)")); assertThat(code, containsOne("@A(a = -11253)"));
assertThat(code, containsString("@V(false)")); assertThat(code, containsOne("@V(false)"));
assertThat(code, not(containsString("@D()"))); assertThat(code, not(containsString("@D()")));
assertThat(code, containsOne("@D"));
assertThat(code, containsString("int a();")); assertThat(code, containsOne("int a();"));
assertThat(code, containsString("float value() default 1.1f;")); assertThat(code, containsOne("float value() default 1.1f;"));
} }
} }
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