Skip to content

Commit

Permalink
fix: add assign for inlined getter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jul 29, 2019
1 parent 8d68d40 commit ccb8ed1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
21 changes: 21 additions & 0 deletions jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.instructions.args.Named;
import jadx.core.dex.instructions.args.NamedArg;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.instructions.args.SSAVar;
import jadx.core.dex.instructions.mods.ConstructorInsn;
Expand Down Expand Up @@ -874,6 +875,13 @@ private boolean inlineMethod(MethodNode callMthNode, InvokeNode insn, CodeWriter
if (Consts.DEBUG) {
code.add("/* inline method: ").add(callMthNode.toString()).add("*/").startLine();
}
if (forceAssign(inl, insn, callMthNode)) {
ArgType varType = callMthNode.getReturnType();
useType(code, varType);
code.add(' ');
code.add(mgen.getNameGen().assignNamedArg(new NamedArg("unused", varType)));
code.add(" = ");
}
if (callMthNode.getMethodInfo().getArgumentsTypes().isEmpty()) {
makeInsn(inl, code, Flags.BODY_ONLY);
} else {
Expand Down Expand Up @@ -907,6 +915,19 @@ private boolean inlineMethod(MethodNode callMthNode, InvokeNode insn, CodeWriter
return true;
}

private boolean forceAssign(InsnNode inlineInsn, InvokeNode parentInsn, MethodNode callMthNode) {
if (parentInsn.getResult() != null) {
return false;
}
if (parentInsn.contains(AFlag.WRAPPED)) {
return false;
}
if (callMthNode.getReturnType().equals(ArgType.VOID)) {
return false;
}
return true;
}

private void makeTernary(TernaryInsn insn, CodeWriter code, Set<Flags> state) throws CodegenException {
boolean wrap = state.contains(Flags.BODY_ONLY);
if (wrap) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package jadx.tests.integration.inline;

import org.junit.jupiter.api.Test;

import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.SmaliTest;

import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

public class TestGetterInlineNegative extends SmaliTest {

// @formatter:off
/*
public class TestGetterInlineNegative {
public static final String field = "some string";
public static synthetic String getter() {
return field;
}
public void test() {
getter(); // inline will produce 'field;' and fail to compile with 'not a statement' error
}
public String test2() {
return getter();
}
}
*/
// @formatter:on

@Test
public void test() {
ClassNode cls = getClassNodeFromSmali();
String code = cls.getCode().toString();

assertThat(code, not(containsString(indent() + "field;")));
assertThat(code, containsOne("return field;"));
}
}
29 changes: 29 additions & 0 deletions jadx-core/src/test/smali/inline/TestGetterInlineNegative.smali
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.class public Linline/TestGetterInlineNegative;
.super Ljava/lang/Object;

.field public static final field:Ljava/lang/String; = "some string"

.method public static synthetic getter()Ljava/lang/String;
.locals 1

sget-object v0, Linline/TestGetterInlineNegative;->field:Ljava/lang/String;

return-object v0
.end method

.method public test()V
.locals 1

invoke-static {}, Linline/TestGetterInlineNegative;->getter()Ljava/lang/String;

return-void
.end method

.method public test2()Ljava/lang/String;
.locals 2

invoke-static {}, Linline/TestGetterInlineNegative;->getter()Ljava/lang/String;
move-result-object v1

return-object v1
.end method

0 comments on commit ccb8ed1

Please sign in to comment.