Skip to content

Commit 683c2df

Browse files
committed
fix: improve ternary inline, resolve more enum cases (#1686)
1 parent 266cbcc commit 683c2df

12 files changed

Lines changed: 253 additions & 201 deletions

File tree

jadx-core/src/main/java/jadx/core/codegen/ClassGen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void addClassDeclaration(ICodeWriter clsCode) {
171171
ArgType sup = cls.getSuperClass();
172172
if (sup != null
173173
&& !sup.equals(ArgType.OBJECT)
174-
&& !cls.isEnum()) {
174+
&& !cls.contains(AFlag.REMOVE_SUPER_CLASS)) {
175175
clsCode.add("extends ");
176176
useClass(clsCode, sup);
177177
clsCode.add(' ');

jadx-core/src/main/java/jadx/core/dex/attributes/AFlag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public enum AFlag {
2121
DONT_GENERATE, // process as usual, but don't output to generated code
2222
COMMENT_OUT, // process as usual, but comment insn in generated code
2323
REMOVE, // can be completely removed
24+
REMOVE_SUPER_CLASS, // don't add super class
2425

2526
HIDDEN, // instruction used inside other instruction but not listed in args
2627

jadx-core/src/main/java/jadx/core/dex/regions/conditions/IfCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static IfCondition simplify(IfCondition cond) {
156156
return i;
157157
}
158158
if (c.getOp() == IfOp.EQ && c.getB().isFalse()) {
159-
cond = not(new IfCondition(c.invert()));
159+
cond = new IfCondition(Mode.NOT, Collections.singletonList(new IfCondition(c.invert())));
160160
} else {
161161
c.normalize();
162162
}

jadx-core/src/main/java/jadx/core/dex/visitors/ConstInlineVisitor.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,45 +62,47 @@ private static void checkInsn(MethodNode mth, InsnNode insn, List<InsnNode> toRe
6262
|| insn.getResult() == null) {
6363
return;
6464
}
65-
6665
SSAVar sVar = insn.getResult().getSVar();
6766
InsnArg constArg;
6867
Runnable onSuccess = null;
69-
70-
InsnType insnType = insn.getType();
71-
if (insnType == InsnType.CONST || insnType == InsnType.MOVE) {
72-
constArg = insn.getArg(0);
73-
if (!constArg.isLiteral()) {
74-
return;
75-
}
76-
long lit = ((LiteralArg) constArg).getLiteral();
77-
if (lit == 0 && forbidNullInlines(sVar)) {
78-
// all usages forbids inlining
79-
return;
68+
switch (insn.getType()) {
69+
case CONST:
70+
case MOVE: {
71+
constArg = insn.getArg(0);
72+
if (!constArg.isLiteral()) {
73+
return;
74+
}
75+
long lit = ((LiteralArg) constArg).getLiteral();
76+
if (lit == 0 && forbidNullInlines(sVar)) {
77+
// all usages forbids inlining
78+
return;
79+
}
80+
break;
8081
}
81-
} else if (insnType == InsnType.CONST_STR) {
82-
if (sVar.isUsedInPhi()) {
83-
return;
82+
case CONST_STR: {
83+
String s = ((ConstStringNode) insn).getString();
84+
FieldNode f = mth.getParentClass().getConstField(s);
85+
if (f == null) {
86+
InsnNode copy = insn.copyWithoutResult();
87+
constArg = InsnArg.wrapArg(copy);
88+
} else {
89+
InsnNode constGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
90+
constArg = InsnArg.wrapArg(constGet);
91+
constArg.setType(ArgType.STRING);
92+
onSuccess = () -> f.addUseIn(mth);
93+
}
94+
break;
8495
}
85-
String s = ((ConstStringNode) insn).getString();
86-
FieldNode f = mth.getParentClass().getConstField(s);
87-
if (f == null) {
88-
InsnNode copy = insn.copyWithoutResult();
89-
constArg = InsnArg.wrapArg(copy);
90-
} else {
91-
InsnNode constGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
92-
constArg = InsnArg.wrapArg(constGet);
93-
constArg.setType(ArgType.STRING);
94-
onSuccess = () -> f.addUseIn(mth);
96+
case CONST_CLASS: {
97+
if (sVar.isUsedInPhi()) {
98+
return;
99+
}
100+
constArg = InsnArg.wrapArg(insn.copyWithoutResult());
101+
constArg.setType(ArgType.CLASS);
102+
break;
95103
}
96-
} else if (insnType == InsnType.CONST_CLASS) {
97-
if (sVar.isUsedInPhi()) {
104+
default:
98105
return;
99-
}
100-
constArg = InsnArg.wrapArg(insn.copyWithoutResult());
101-
constArg.setType(ArgType.CLASS);
102-
} else {
103-
return;
104106
}
105107

106108
// all check passed, run replace

0 commit comments

Comments
 (0)