core: fix anonymous class codegen
This commit is contained in:
@@ -524,10 +524,10 @@ public class InsnGen {
|
||||
if (cls != null && cls.isAnonymous()) {
|
||||
// anonymous class construction
|
||||
ClassInfo parent;
|
||||
if (cls.getSuperClass() != null && !cls.getSuperClass().isObject()) {
|
||||
parent = cls.getSuperClass();
|
||||
} else {
|
||||
if (cls.getInterfaces().size() == 1) {
|
||||
parent = cls.getInterfaces().get(0);
|
||||
} else {
|
||||
parent = cls.getSuperClass();
|
||||
}
|
||||
MethodNode defCtr = cls.getDefaultConstructor();
|
||||
if (RegionUtils.notEmpty(defCtr.getRegion())) {
|
||||
@@ -535,7 +535,7 @@ public class InsnGen {
|
||||
} else {
|
||||
defCtr.getAttributes().add(AttributeFlag.DONT_GENERATE);
|
||||
}
|
||||
code.add("new ").add(useClass(parent)).add("() ");
|
||||
code.add("new ").add(parent == null ? "Object" : useClass(parent)).add("() ");
|
||||
code.incIndent(2);
|
||||
new ClassGen(cls, mgen.getClassGen().getParentGen(), fallback).makeClassBody(code);
|
||||
code.decIndent(2);
|
||||
|
||||
@@ -338,15 +338,17 @@ public class ClassNode extends LineAttrNode implements ILoadable {
|
||||
}
|
||||
|
||||
public boolean isAnonymous() {
|
||||
MethodNode defConstrExists = getDefaultConstructor();
|
||||
return defConstrExists != null && getShortName().startsWith(Consts.ANONYMOUS_CLASS_PREFIX);
|
||||
return clsInfo.isInner()
|
||||
&& getShortName().startsWith(Consts.ANONYMOUS_CLASS_PREFIX)
|
||||
&& getDefaultConstructor() != null;
|
||||
}
|
||||
|
||||
public MethodNode getDefaultConstructor() {
|
||||
for (MethodNode mth : methods) {
|
||||
if (mth.getAccessFlags().isConstructor()
|
||||
&& mth.getMethodInfo().isConstructor()
|
||||
&& mth.getArguments(false).isEmpty()) {
|
||||
&& (mth.getMethodInfo().getArgsCount() == 0
|
||||
|| (mth.getArguments(false) != null && mth.getArguments(false).isEmpty()))) {
|
||||
return mth;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,19 @@ public class TestInner extends AbstractTest {
|
||||
}.run();
|
||||
}
|
||||
|
||||
public String func3() {
|
||||
return new Object() {
|
||||
{
|
||||
count += 7;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
count += 8;
|
||||
return Integer.toString(count);
|
||||
}
|
||||
}.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class MyException extends Exception {
|
||||
public MyException(String str, Exception e) {
|
||||
@@ -94,6 +107,8 @@ public class TestInner extends AbstractTest {
|
||||
thread.join();
|
||||
thread2.join();
|
||||
|
||||
return TestInner.count == 26;
|
||||
assertEquals(func3(), "41");
|
||||
|
||||
return TestInner.count == 41;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user