Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78db1ff

Browse files
committed
Fixed bug that base class' fields get initialized more than expected.
Fixed bug polymorphic methods with different types inherited from same base class get invoked incorrectly. Fixed bug that inner class dependencies force class loader to load non-existed *.js file. Classes used in constructor, static block and field initializer will be marked as must loaded classes. Other bug fixes and improvements
1 parent 402ae4b commit 78db1ff

File tree

8 files changed

+459
-150
lines changed

8 files changed

+459
-150
lines changed

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

Lines changed: 144 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
package net.sf.j2s.core.astvisitors;
1212

1313
import java.util.ArrayList;
14+
import java.util.HashSet;
1415
import java.util.Iterator;
1516
import java.util.List;
17+
import java.util.Set;
18+
1619
import org.eclipse.jdt.core.dom.ASTNode;
1720
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
1821
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
@@ -585,7 +588,7 @@ public boolean visit(ClassInstanceCreation node) {
585588
if (constructorBinding != null) {
586589
methodDeclaration = constructorBinding.getMethodDeclaration();
587590
}
588-
visitMethodParameterList(node.arguments(), methodDeclaration, true, ", ", null);
591+
visitMethodParameterList(binding, node.arguments(), methodDeclaration, true, ", ", null);
589592
buffer.append(")");
590593
return false;
591594
}
@@ -610,7 +613,7 @@ public boolean visit(ClassInstanceCreation node) {
610613
if (constructorBinding != null) {
611614
methodDeclaration = constructorBinding.getMethodDeclaration();
612615
}
613-
visitMethodParameterList(node.arguments(), methodDeclaration, true, null, null);
616+
visitMethodParameterList(binding, node.arguments(), methodDeclaration, true, null, null);
614617
buffer.append(")");
615618
} else {
616619
ITypeBinding binding = node.resolveTypeBinding();
@@ -683,7 +686,7 @@ public boolean visit(ClassInstanceCreation node) {
683686
if (constructorBinding != null) {
684687
methodDeclaration = constructorBinding.getMethodDeclaration();
685688
}
686-
visitMethodParameterList(node.arguments(), methodDeclaration, true, ", ", null);
689+
visitMethodParameterList(binding, node.arguments(), methodDeclaration, true, ", ", null);
687690

688691
if (lastCurrentBlock != -1) {
689692
/* add the visited variables into last visited variables */
@@ -712,7 +715,45 @@ public boolean visit(ClassInstanceCreation node) {
712715
return false;
713716
}
714717

715-
protected void visitMethodParameterList(List arguments, IMethodBinding methodDeclaration, boolean isConstructor, String prefix, String suffix) {
718+
String[] getParameterSimilarities(ITypeBinding[] params1, ITypeBinding[] params2) {
719+
if (params1 == null || params2 == null || params1.length != params2.length) return null;
720+
String[] result = new String[params1.length];
721+
for (int i = 0; i < params1.length; i++) {
722+
ITypeBinding t1 = params1[i];
723+
ITypeBinding t2 = params2[i];
724+
if (t1.isPrimitive()) {
725+
String typeName1 = t1.getName();
726+
String typeName2 = t2.getName();
727+
if (!"boolean".equals(typeName1)) {
728+
typeName1 = "Number";
729+
}
730+
if (!"boolean".equals(typeName2)) {
731+
typeName2 = "Number";
732+
}
733+
if (!t2.isPrimitive() || !typeName1.equals(typeName2)) return null;
734+
} else if (t1.isArray()) {
735+
if (!t2.isArray()) return null;
736+
/*
737+
* Current Java2Script does not distinguish different array types.
738+
*
739+
ITypeBinding cType1 = t1.getComponentType();
740+
ITypeBinding cType2 = t2.getComponentType();
741+
if (cType1 != cType2 && !cType2.isCastCompatible(cType1)) return null;
742+
if (cType1 != cType2) {
743+
result[i] = cType2.getQualifiedName();
744+
}
745+
// */
746+
} else {
747+
if (t1 != t2 && !t2.isCastCompatible(t1)) return null;
748+
if (t1 != t2) {
749+
result[i] = t1.getQualifiedName();
750+
}
751+
}
752+
}
753+
return result;
754+
}
755+
756+
protected void visitMethodParameterList(ITypeBinding nodeTypeBinding, List arguments, IMethodBinding methodDeclaration, boolean isConstructor, String prefix, String suffix) {
716757
if (methodDeclaration == null) {
717758
return;
718759
}
@@ -730,13 +771,89 @@ protected void visitMethodParameterList(List arguments, IMethodBinding methodDec
730771
return;
731772
}
732773

774+
String[] ambitiousResult = null;
733775
boolean alreadyPrefixed = false;
734776
String clazzName = null;
735-
ITypeBinding[] parameterTypes = methodDeclaration.getParameterTypes();
736777
ITypeBinding declaringClass = methodDeclaration.getDeclaringClass();
737778
if (declaringClass != null) {
738779
clazzName = declaringClass.getQualifiedName();
780+
781+
String methodName = methodDeclaration.getName();
782+
IMethodBinding mBinding = methodDeclaration;
783+
ITypeBinding[] paramTypes = mBinding.getParameterTypes();
784+
List allSimilarMethods = new ArrayList();
785+
ITypeBinding dClass = nodeTypeBinding;
786+
if (dClass == null) {
787+
dClass = mBinding.getDeclaringClass();
788+
}
789+
do {
790+
IMethodBinding[] methods = dClass.getDeclaredMethods();
791+
for (int i = 0; i < methods.length; i++) {
792+
IMethodBinding m = methods[i];
793+
ITypeBinding[] mParamTypes = m.getParameterTypes();
794+
if (methodName.equals(m.getName()) && mParamTypes != null
795+
&& paramTypes.length == mParamTypes.length) {
796+
allSimilarMethods.add(m);
797+
}
798+
}
799+
dClass = dClass.getSuperclass();
800+
} while (dClass != null);
801+
IMethodBinding[] methods = (IMethodBinding[])allSimilarMethods.toArray(new IMethodBinding[allSimilarMethods.size()]);
802+
Set knownMethods = new HashSet();
803+
knownMethods.add(methodDeclaration);
804+
List ambitiousMethods = new ArrayList();
805+
int lastCastings = 0;
806+
do {
807+
ITypeBinding[] parameterTypes = mBinding.getParameterTypes();
808+
for (int i = 0; i < methods.length; i++) {
809+
IMethodBinding m = methods[i];
810+
if (knownMethods.contains(m)) continue;
811+
ITypeBinding[] mParamTypes = m.getParameterTypes();
812+
if (methodName.equals(m.getName()) && mParamTypes != null
813+
&& parameterTypes.length == mParamTypes.length) {
814+
String[] result = getParameterSimilarities(parameterTypes, mParamTypes);
815+
if (result != null) {
816+
int currentCastings = 0;
817+
for (int j = 0; j < result.length; j++) {
818+
if (result[j] != null) {
819+
currentCastings++;
820+
}
821+
}
822+
if (!knownMethods.contains(m)) {
823+
ambitiousMethods.add(m);
824+
knownMethods.add(m);
825+
}
826+
if (ambitiousResult == null) {
827+
ambitiousResult = result;
828+
lastCastings = currentCastings;
829+
} else { // other methods
830+
if (currentCastings < lastCastings) {
831+
for (int j = 0; j < result.length; j++) {
832+
if (result[j] != null) {
833+
ambitiousResult[j] = result[j];
834+
} else if (ambitiousResult[j] != null){
835+
ambitiousResult[j] = ""; // need to be casted, other similar methods exist
836+
}
837+
}
838+
lastCastings = currentCastings;
839+
} else {
840+
for (int j = 0; j < result.length; j++) {
841+
if (result[j] != null && ambitiousResult[j] == null) {
842+
ambitiousResult[j] = ""; // need to be casted, other similar methods exist
843+
}
844+
}
845+
}
846+
}
847+
}
848+
}
849+
}
850+
if (ambitiousMethods.size() <= 0) {
851+
break;
852+
}
853+
mBinding = (IMethodBinding) ambitiousMethods.remove(0);
854+
} while (true);
739855
}
856+
ITypeBinding[] parameterTypes = methodDeclaration.getParameterTypes();
740857
String methodName = methodDeclaration.getName();
741858
int argSize = arguments.size();
742859
for (int i = 0; i < parameterTypes.length; i++) {
@@ -755,6 +872,9 @@ protected void visitMethodParameterList(List arguments, IMethodBinding methodDec
755872
}
756873
}
757874
}
875+
if (!isVarArgs && ambitiousResult != null && ambitiousResult[i] != null) {
876+
buffer.append("Clazz.castObjectAs(");
877+
}
758878
String parameterTypeName = null;
759879
if (parameterTypes != null) {
760880
parameterTypeName = parameterTypes[i].getName();
@@ -776,6 +896,15 @@ protected void visitMethodParameterList(List arguments, IMethodBinding methodDec
776896
} else {
777897
ASTNode element = (ASTNode) arguments.get(i);
778898
visitArgumentItem(element, clazzName, methodName, parameterTypeName, i);
899+
if (ambitiousResult != null && ambitiousResult[i] != null) {
900+
String typeName = ambitiousResult[i];
901+
if (typeName.length() == 0) {
902+
ITypeBinding paramType = parameterTypes[i];
903+
typeName = paramType.getQualifiedName();
904+
}
905+
String typeStr = assureQualifiedName(shortenQualifiedName(typeName));
906+
buffer.append(", \"").append(typeStr).append("\")");
907+
}
779908
if (i != parameterTypes.length - 1) {
780909
buffer.append(", ");
781910
}
@@ -820,9 +949,10 @@ private void visitArgumentItem(ASTNode element,
820949
}
821950
}
822951
if (typeStr != null) {
823-
buffer.append("Clazz.castNullAs (\"");
824-
buffer.append(typeStr.replaceFirst("^\\$wt.", "org.eclipse.swt."));
825-
buffer.append("\")");
952+
//buffer.append("Clazz.castNullAs (\"");
953+
//buffer.append(typeStr.replaceFirst("^\\$wt.", "org.eclipse.swt."));
954+
//buffer.append("\")");
955+
buffer.append("null");
826956
} else {
827957
Expression exp = (Expression) element;
828958
ITypeBinding typeBinding = exp.resolveTypeBinding();
@@ -898,7 +1028,7 @@ public boolean visit(ConstructorInvocation node) {
8981028
if (constructorBinding != null) {
8991029
methodDeclaration = constructorBinding.getMethodDeclaration();
9001030
}
901-
visitMethodParameterList(node.arguments(), methodDeclaration, true, null, null);
1031+
visitMethodParameterList(methodDeclaration.getDeclaringClass(), node.arguments(), methodDeclaration, true, null, null);
9021032
buffer.append(");\r\n");
9031033
return false;
9041034
}
@@ -2138,13 +2268,15 @@ private boolean containsOnlySuperCall(Block body) {
21382268
}
21392269

21402270
public boolean visit(MethodInvocation node) {
2271+
ITypeBinding nodeTypeBinding = null;
21412272
Expression expression = node.getExpression();
21422273
if (expression != null) {
21432274
/*
21442275
* Here?
21452276
*/
21462277
expression.accept(this);
21472278
buffer.append(".");
2279+
nodeTypeBinding = node.getExpression().resolveTypeBinding();
21482280
}
21492281

21502282
String methodName = node.getName().getIdentifier();
@@ -2175,7 +2307,7 @@ public boolean visit(MethodInvocation node) {
21752307
}
21762308
buffer.append(" (");
21772309
IMethodBinding methodDeclaration = node.resolveMethodBinding();
2178-
visitMethodParameterList(node.arguments(), methodDeclaration, false, null, null);
2310+
visitMethodParameterList(nodeTypeBinding, node.arguments(), methodDeclaration, false, null, null);
21792311
buffer.append(")");
21802312
return false;
21812313
}
@@ -2629,7 +2761,7 @@ public boolean visit(SuperConstructorInvocation node) {
26292761
if (constructorBinding != null) {
26302762
methodDeclaration = constructorBinding.getMethodDeclaration();
26312763
}
2632-
visitMethodParameterList(node.arguments(), methodDeclaration, true, ", [", "]");
2764+
visitMethodParameterList(declaringClass, node.arguments(), methodDeclaration, true, ", [", "]");
26332765
buffer.append(");\r\n");
26342766
return false;
26352767
}
@@ -2735,7 +2867,7 @@ public boolean visit(SuperMethodInvocation node) {
27352867
buffer.append(name);
27362868
buffer.append("\", [");
27372869
IMethodBinding methodDeclaration = node.resolveMethodBinding();
2738-
visitMethodParameterList(node.arguments(), methodDeclaration, false, null, null);
2870+
visitMethodParameterList(methodDeclaration.getDeclaringClass(), node.arguments(), methodDeclaration, false, null, null);
27392871
buffer.append("])");
27402872
return false;
27412873
}

0 commit comments

Comments
 (0)