11
11
package net .sf .j2s .core .astvisitors ;
12
12
13
13
import java .util .ArrayList ;
14
+ import java .util .HashSet ;
14
15
import java .util .Iterator ;
15
16
import java .util .List ;
17
+ import java .util .Set ;
18
+
16
19
import org .eclipse .jdt .core .dom .ASTNode ;
17
20
import org .eclipse .jdt .core .dom .AbstractTypeDeclaration ;
18
21
import org .eclipse .jdt .core .dom .AnonymousClassDeclaration ;
@@ -585,7 +588,7 @@ public boolean visit(ClassInstanceCreation node) {
585
588
if (constructorBinding != null ) {
586
589
methodDeclaration = constructorBinding .getMethodDeclaration ();
587
590
}
588
- visitMethodParameterList (node .arguments (), methodDeclaration , true , ", " , null );
591
+ visitMethodParameterList (binding , node .arguments (), methodDeclaration , true , ", " , null );
589
592
buffer .append (")" );
590
593
return false ;
591
594
}
@@ -610,7 +613,7 @@ public boolean visit(ClassInstanceCreation node) {
610
613
if (constructorBinding != null ) {
611
614
methodDeclaration = constructorBinding .getMethodDeclaration ();
612
615
}
613
- visitMethodParameterList (node .arguments (), methodDeclaration , true , null , null );
616
+ visitMethodParameterList (binding , node .arguments (), methodDeclaration , true , null , null );
614
617
buffer .append (")" );
615
618
} else {
616
619
ITypeBinding binding = node .resolveTypeBinding ();
@@ -683,7 +686,7 @@ public boolean visit(ClassInstanceCreation node) {
683
686
if (constructorBinding != null ) {
684
687
methodDeclaration = constructorBinding .getMethodDeclaration ();
685
688
}
686
- visitMethodParameterList (node .arguments (), methodDeclaration , true , ", " , null );
689
+ visitMethodParameterList (binding , node .arguments (), methodDeclaration , true , ", " , null );
687
690
688
691
if (lastCurrentBlock != -1 ) {
689
692
/* add the visited variables into last visited variables */
@@ -712,7 +715,45 @@ public boolean visit(ClassInstanceCreation node) {
712
715
return false ;
713
716
}
714
717
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 ) {
716
757
if (methodDeclaration == null ) {
717
758
return ;
718
759
}
@@ -730,13 +771,89 @@ protected void visitMethodParameterList(List arguments, IMethodBinding methodDec
730
771
return ;
731
772
}
732
773
774
+ String [] ambitiousResult = null ;
733
775
boolean alreadyPrefixed = false ;
734
776
String clazzName = null ;
735
- ITypeBinding [] parameterTypes = methodDeclaration .getParameterTypes ();
736
777
ITypeBinding declaringClass = methodDeclaration .getDeclaringClass ();
737
778
if (declaringClass != null ) {
738
779
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 );
739
855
}
856
+ ITypeBinding [] parameterTypes = methodDeclaration .getParameterTypes ();
740
857
String methodName = methodDeclaration .getName ();
741
858
int argSize = arguments .size ();
742
859
for (int i = 0 ; i < parameterTypes .length ; i ++) {
@@ -755,6 +872,9 @@ protected void visitMethodParameterList(List arguments, IMethodBinding methodDec
755
872
}
756
873
}
757
874
}
875
+ if (!isVarArgs && ambitiousResult != null && ambitiousResult [i ] != null ) {
876
+ buffer .append ("Clazz.castObjectAs(" );
877
+ }
758
878
String parameterTypeName = null ;
759
879
if (parameterTypes != null ) {
760
880
parameterTypeName = parameterTypes [i ].getName ();
@@ -776,6 +896,15 @@ protected void visitMethodParameterList(List arguments, IMethodBinding methodDec
776
896
} else {
777
897
ASTNode element = (ASTNode ) arguments .get (i );
778
898
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
+ }
779
908
if (i != parameterTypes .length - 1 ) {
780
909
buffer .append (", " );
781
910
}
@@ -820,9 +949,10 @@ private void visitArgumentItem(ASTNode element,
820
949
}
821
950
}
822
951
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" );
826
956
} else {
827
957
Expression exp = (Expression ) element ;
828
958
ITypeBinding typeBinding = exp .resolveTypeBinding ();
@@ -898,7 +1028,7 @@ public boolean visit(ConstructorInvocation node) {
898
1028
if (constructorBinding != null ) {
899
1029
methodDeclaration = constructorBinding .getMethodDeclaration ();
900
1030
}
901
- visitMethodParameterList (node .arguments (), methodDeclaration , true , null , null );
1031
+ visitMethodParameterList (methodDeclaration . getDeclaringClass (), node .arguments (), methodDeclaration , true , null , null );
902
1032
buffer .append (");\r \n " );
903
1033
return false ;
904
1034
}
@@ -2138,13 +2268,15 @@ private boolean containsOnlySuperCall(Block body) {
2138
2268
}
2139
2269
2140
2270
public boolean visit (MethodInvocation node ) {
2271
+ ITypeBinding nodeTypeBinding = null ;
2141
2272
Expression expression = node .getExpression ();
2142
2273
if (expression != null ) {
2143
2274
/*
2144
2275
* Here?
2145
2276
*/
2146
2277
expression .accept (this );
2147
2278
buffer .append ("." );
2279
+ nodeTypeBinding = node .getExpression ().resolveTypeBinding ();
2148
2280
}
2149
2281
2150
2282
String methodName = node .getName ().getIdentifier ();
@@ -2175,7 +2307,7 @@ public boolean visit(MethodInvocation node) {
2175
2307
}
2176
2308
buffer .append (" (" );
2177
2309
IMethodBinding methodDeclaration = node .resolveMethodBinding ();
2178
- visitMethodParameterList (node .arguments (), methodDeclaration , false , null , null );
2310
+ visitMethodParameterList (nodeTypeBinding , node .arguments (), methodDeclaration , false , null , null );
2179
2311
buffer .append (")" );
2180
2312
return false ;
2181
2313
}
@@ -2629,7 +2761,7 @@ public boolean visit(SuperConstructorInvocation node) {
2629
2761
if (constructorBinding != null ) {
2630
2762
methodDeclaration = constructorBinding .getMethodDeclaration ();
2631
2763
}
2632
- visitMethodParameterList (node .arguments (), methodDeclaration , true , ", [" , "]" );
2764
+ visitMethodParameterList (declaringClass , node .arguments (), methodDeclaration , true , ", [" , "]" );
2633
2765
buffer .append (");\r \n " );
2634
2766
return false ;
2635
2767
}
@@ -2735,7 +2867,7 @@ public boolean visit(SuperMethodInvocation node) {
2735
2867
buffer .append (name );
2736
2868
buffer .append ("\" , [" );
2737
2869
IMethodBinding methodDeclaration = node .resolveMethodBinding ();
2738
- visitMethodParameterList (node .arguments (), methodDeclaration , false , null , null );
2870
+ visitMethodParameterList (methodDeclaration . getDeclaringClass (), node .arguments (), methodDeclaration , false , null , null );
2739
2871
buffer .append ("])" );
2740
2872
return false ;
2741
2873
}
0 commit comments