Commit 2478fc3a authored by Anton Dyachenko's avatar Anton Dyachenko

core: fix instance initializer producing (don't generate super() call)

parent 5a68d3be
......@@ -377,10 +377,7 @@ public class ClassNode extends LineAttrNode implements ILoadable {
public MethodNode getDefaultConstructor() {
for (MethodNode mth : methods) {
if (mth.getAccessFlags().isConstructor()
&& mth.getMethodInfo().isConstructor()
&& (mth.getMethodInfo().getArgsCount() == 0
|| (mth.getArguments(false) != null && mth.getArguments(false).isEmpty()))) {
if (mth.isDefaultConstructor()) {
return mth;
}
}
......
......@@ -461,6 +461,31 @@ public class MethodNode extends LineAttrNode implements ILoadable {
}
return false;
}
public boolean isDefaultConstructor() {
boolean result = false;
if (accFlags.isConstructor() && mthInfo.isConstructor()) {
int defaultArgCount = 0;
/** workaround for non-static inner class constructor, that has
* synthetic argument */
if ((parentClass != null) && parentClass.getClassInfo().isInner()) {
if (!parentClass.getAccessFlags().isStatic()) {
ClassNode outerCls = parentClass.getParentClass();
if ((argsList != null) && (argsList.size() >= 1)) {
if (argsList.get(0).getType().equals(outerCls.getClassInfo().getType())) {
defaultArgCount = 1;
}
}
}
}
result = (argsList == null) || (argsList.size() == defaultArgCount);
}
return result;
}
public int getRegsCount() {
return regsCount;
......
......@@ -76,6 +76,12 @@ public class ModVisitor extends AbstractVisitor {
remove = true;
}
}
// remove super() call in instance initializer
if (parentClass.isAnonymous() && mth.isDefaultConstructor() && co.isSuper()) {
remove = true;
}
if (remove) {
remover.add(insn);
} else {
......
package jadx.samples;
public class TestInitializers extends AbstractTest {
private static String a;
private static int counter;
private A c_a;
public static class A {
public static String a;
static {
a = "a1";
}
public boolean z() {
return true;
}
}
public class B {
private int b;
private int bbb;
public B() {
if (c_a.z()) {
b = -1;
} else {
b = 1;
}
}
public B(int _b) {
b = _b;
}
public void setB(int _b) {
b = _b;
}
public int getB() {
return b;
}
public int getBBB() {
return bbb;
}
{
bbb = 123;
}
}
static {
a = "a0";
counter = 0;
}
{
c_a = new A();
}
@Override
public boolean testRun() throws Exception {
assertTrue(counter == 0);
assertTrue(a.equals("a0"));
assertTrue(A.a.equals("a1"));
B b1 = new B() {
{
TestInitializers.counter++;
setB(TestInitializers.counter);
}
};
assertTrue(b1.getB() == 1);
B b2 = new B() {
@SuppressWarnings("unused")
private int bb;
public int getB() {
return super.getB();
}
{
bb = 100;
}
};
assertTrue(b2.getB() == -1);
assertTrue((new B()).getB() == -1);
assertTrue(counter == 1);
B b3 = new B(3);
assertTrue((b3.getB() == 3) && (b3.getBBB() == 123));
return true;
}
public static void main(String[] args) throws Exception {
new TestInitializers().testRun();
}
}
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