Skip to content

Commit

Permalink
fix: don't remove empty default constructor if other constructors exi…
Browse files Browse the repository at this point in the history
…sts (#460)
  • Loading branch information
skylot committed Mar 2, 2019
1 parent 653bb2a commit dd13edf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
27 changes: 23 additions & 4 deletions jadx-core/src/main/java/jadx/core/dex/visitors/ClassModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,34 @@ private static boolean isMethodUnique(ClassNode cls, MethodNode mth) {

private static void removeEmptyMethods(MethodNode mth) {
AccessInfo af = mth.getAccessFlags();
// remove public empty constructors
// remove public empty constructors (static or default)
if (af.isConstructor()
&& (af.isPublic() || af.isStatic())
&& mth.getArguments(false).isEmpty()
&& !mth.contains(AType.JADX_ERROR)) {
&& mth.getArguments(false).isEmpty()) {
List<BlockNode> bb = mth.getBasicBlocks();
if (bb == null || bb.isEmpty() || BlockUtils.isAllBlocksEmpty(bb)) {
mth.add(AFlag.DONT_GENERATE);
if (af.isStatic() && mth.getMethodInfo().isClassInit()) {
mth.add(AFlag.DONT_GENERATE);
} else {
// don't remove default constructor if other constructors exists
if (mth.isDefaultConstructor() && !isNonDefaultConstructorExists(mth)) {
mth.add(AFlag.DONT_GENERATE);
}
}
}
}
}

private static boolean isNonDefaultConstructorExists(MethodNode defCtor) {
ClassNode parentClass = defCtor.getParentClass();
for (MethodNode mth : parentClass.getMethods()) {
if (mth != defCtor
&& mth.getAccessFlags().isConstructor()
&& mth.getMethodInfo().isConstructor()
&& !mth.isDefaultConstructor()) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package jadx.tests.integration.others;

import org.junit.Test;

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

import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

public class TestDefConstructorNotRemoved extends IntegrationTest {

public static class TestCls {

static {
// empty
}

public static class A {
private final String s;

public A() {
s = "a";
}

public A(String str) {
s = str;
}
}

public static class B extends A {
public B() {
super();
}

public B(String s) {
super(s);
}
}

public void check() {
new A();
new A("a");
new B();
new B("b");
}
}

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

assertThat(code, not(containsString("super();")));
assertThat(code, not(containsString("static {")));
assertThat(code, containsOne("public B() {"));
}
}

0 comments on commit dd13edf

Please sign in to comment.