Commit f9e7a29c authored by Skylot's avatar Skylot

core: fix sythetic constructor replacement (#334)

parent 6cb14a1c
...@@ -396,14 +396,14 @@ public class ModVisitor extends AbstractVisitor { ...@@ -396,14 +396,14 @@ public class ModVisitor extends AbstractVisitor {
return filledArr; return filledArr;
} }
private static boolean allArgsNull(InsnNode insn) { private static boolean allArgsNull(ConstructorInsn insn) {
for (InsnArg insnArg : insn.getArguments()) { for (InsnArg insnArg : insn.getArguments()) {
if (insnArg.isLiteral()) { if (insnArg.isLiteral()) {
LiteralArg lit = (LiteralArg) insnArg; LiteralArg lit = (LiteralArg) insnArg;
if (lit.getLiteral() != 0) { if (lit.getLiteral() != 0) {
return false; return false;
} }
} else if (!insnArg.isThis()) { } else {
return false; return false;
} }
} }
......
...@@ -25,6 +25,7 @@ import jadx.core.dex.visitors.IDexTreeVisitor; ...@@ -25,6 +25,7 @@ import jadx.core.dex.visitors.IDexTreeVisitor;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.tests.api.IntegrationTest; import jadx.tests.api.IntegrationTest;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -58,7 +59,7 @@ public abstract class BaseExternalTest extends IntegrationTest { ...@@ -58,7 +59,7 @@ public abstract class BaseExternalTest extends IntegrationTest {
} else { } else {
Pattern clsPtrn = Pattern.compile(clsPatternStr); Pattern clsPtrn = Pattern.compile(clsPatternStr);
Pattern mthPtrn = mthPatternStr == null ? null : Pattern.compile(mthPatternStr); Pattern mthPtrn = mthPatternStr == null ? null : Pattern.compile(mthPatternStr);
processMthByPatterns(jadx, clsPtrn, mthPtrn); processByPatterns(jadx, clsPtrn, mthPtrn);
} }
printErrorReport(jadx); printErrorReport(jadx);
} }
...@@ -69,12 +70,22 @@ public abstract class BaseExternalTest extends IntegrationTest { ...@@ -69,12 +70,22 @@ public abstract class BaseExternalTest extends IntegrationTest {
} }
} }
private void processMthByPatterns(JadxDecompiler jadx, Pattern clsPattern, @Nullable Pattern mthPattern) { private void processByPatterns(JadxDecompiler jadx, Pattern clsPattern, @Nullable Pattern mthPattern) {
List<IDexTreeVisitor> passes = Jadx.getPassesList(jadx.getArgs()); List<IDexTreeVisitor> passes = Jadx.getPassesList(jadx.getArgs());
RootNode root = JadxInternalAccess.getRoot(jadx); RootNode root = JadxInternalAccess.getRoot(jadx);
int processed = 0;
for (ClassNode classNode : root.getClasses(true)) { for (ClassNode classNode : root.getClasses(true)) {
String clsFullName = classNode.getClassInfo().getFullName(); String clsFullName = classNode.getClassInfo().getFullName();
if (clsPattern.matcher(clsFullName).matches()) { if (clsPattern.matcher(clsFullName).matches()) {
if (processCls(mthPattern, passes, classNode)) {
processed++;
}
}
}
assertThat("No classes processed", processed, greaterThan(0));
}
private boolean processCls(@Nullable Pattern mthPattern, List<IDexTreeVisitor> passes, ClassNode classNode) {
classNode.load(); classNode.load();
boolean decompile = false; boolean decompile = false;
if (mthPattern == null) { if (mthPattern == null) {
...@@ -87,7 +98,9 @@ public abstract class BaseExternalTest extends IntegrationTest { ...@@ -87,7 +98,9 @@ public abstract class BaseExternalTest extends IntegrationTest {
} }
} }
} }
if (decompile) { if (!decompile) {
return false;
}
for (IDexTreeVisitor visitor : passes) { for (IDexTreeVisitor visitor : passes) {
DepthTraversal.visit(visitor, classNode); DepthTraversal.visit(visitor, classNode);
} }
...@@ -103,10 +116,7 @@ public abstract class BaseExternalTest extends IntegrationTest { ...@@ -103,10 +116,7 @@ public abstract class BaseExternalTest extends IntegrationTest {
LOG.info("Code: \n{}", classNode.getCode()); LOG.info("Code: \n{}", classNode.getCode());
} }
checkCode(classNode); checkCode(classNode);
// SaveCode.save(jadx.getArgs().getOutDirSrc(), jadx.getArgs(), classNode); return true;
}
}
}
} }
private void printMethods(ClassNode classNode, @NotNull Pattern mthPattern) { private void printMethods(ClassNode classNode, @NotNull Pattern mthPattern) {
......
package jadx.tests.integration.inner;
import org.junit.Test;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
public class TestOuterConstructorCall extends IntegrationTest {
public static class TestCls {
private TestCls(Inner inner) {
System.out.println(inner);
}
private class Inner {
private TestCls test() {
return new TestCls(this);
}
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("private class Inner {"));
assertThat(code, containsString("return new TestOuterConstructorCall$TestCls(this);"));
assertThat(code, not(containsString("synthetic")));
}
}
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