Skip to content

Commit 5fc0bc8

Browse files
committed
removing unnecessary addition of Foo() if Foo(int...) present
1 parent 4c1ebc1 commit 5fc0bc8

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private void setInnerGLobals(ASTScriptVisitor parent, TypeDeclaration node) {
104104
/**
105105
* default constructor found by visit(MethodDeclaration)
106106
*/
107-
IMethodBinding defaultConstructor;
107+
boolean haveDefaultConstructor;
108108

109109
/**
110110
* holds all static field definitions for insertion at the end of the class def
@@ -1137,27 +1137,17 @@ protected String[] getFilterMethods() {
11371137

11381138
public boolean visit(MethodDeclaration node) {
11391139

1140-
if (checkj2sIgnore(node)) {
1141-
return false;
1142-
}
1143-
11441140
IMethodBinding mBinding = node.resolveBinding();
11451141

1146-
boolean isConstructor = node.isConstructor();
1147-
if (isConstructor) {
1148-
// TODO: This is not sufficient for variable args. We are not
1149-
// finding the correct constructor, only the first-stated
1150-
// For example, this(int...) and this(float...) will be wrong here.
1151-
// fortunately this only affects anonymous class definitions - BH
1152-
if (mBinding.getParameterTypes().length == 0 || mBinding.isVarargs() && (defaultConstructor == null)) {
1153-
defaultConstructor = mBinding;
1154-
}
1142+
if (mBinding == null || checkj2sIgnore(node)) {
1143+
return false;
11551144
}
1145+
11561146
boolean isStatic = isStatic(node.getModifiers());
11571147

11581148
if (!checkKeepSpecialClassMethod(node, mBinding, false))
11591149
return false;
1160-
String key = (mBinding == null ? null : mBinding.getKey());
1150+
String key = mBinding.getKey();
11611151
if (key != null)
11621152
methodDeclareNameStack.push(key);
11631153

@@ -1166,15 +1156,17 @@ public boolean visit(MethodDeclaration node) {
11661156
// Abstract method
11671157
return false;
11681158
}
1159+
1160+
boolean isConstructor = node.isConstructor();
11691161
String name = (isConstructor ? "construct" : getJ2SName(node.getName())) + getJ2SParamQualifier(null, mBinding);
1170-
if (isConstructor && name.equals("construct"))
1171-
defaultConstructor = mBinding; // in case we are not qualifying names here
1162+
if (isConstructor && name.equals("construct") || mBinding.isVarargs() && mBinding.getParameterTypes().length == 1)
1163+
haveDefaultConstructor = true; // in case we are not qualifying names here
11721164
buffer.append("\r\nClazz.newMethod$(C$, '").append(name).append("', function (");
11731165
@SuppressWarnings("unchecked")
11741166
List<ASTNode> parameters = node.parameters();
11751167
visitList(parameters, ", ");
11761168
buffer.append(") ");
1177-
if (node.isConstructor()) {
1169+
if (isConstructor) {
11781170
// BH @j2sIgnoreSuperConstructor removed from options
11791171
// as it is too risky to do this -- lose all initialization.
11801172
@SuppressWarnings("unchecked")
@@ -2475,22 +2467,15 @@ static String j2sGetParamCode(ITypeBinding binding, boolean addAAA) {
24752467
}
24762468

24772469
private void addDefaultConstructor() {
2478-
if (defaultConstructor == null || defaultConstructor.isVarargs()) {
2479-
buffer.append("\r\nClazz.newMethod$(C$, 'construct', function () {");
2480-
if (defaultConstructor == null) {
2470+
// if there is no Foo() or Foo(xxx... array)
2471+
// then we need to provide our own constructor
2472+
if (haveDefaultConstructor) {
2473+
haveDefaultConstructor = false;
2474+
} else {
2475+
buffer.append("\r\nClazz.newMethod$(C$, 'construct', function(){");
24812476
addSuperConstructor(null, null);
2482-
} else {
2483-
// TODO BH: This is still not right. It's specifically for
2484-
// anonymous
2485-
// constructors
2486-
// But I can't seem to see how to get the right vararg
2487-
// constructor
2488-
// (float...) vs (int...)
2489-
addThisConstructorCall(defaultConstructor, new ArrayList<Object>());
2490-
}
24912477
buffer.append("}, 1);\r\n");
24922478
}
2493-
defaultConstructor = null;
24942479
}
24952480

24962481
private void addSuperConstructor(SuperConstructorInvocation node, IMethodBinding methodDeclaration) {

0 commit comments

Comments
 (0)