1919import java .util .Iterator ;
2020import java .util .List ;
2121import java .util .Map ;
22- import java .util .Map .Entry ;
2322import java .util .Set ;
2423import java .util .Stack ;
2524import java .util .regex .Pattern ;
2625
27- import javax .xml .bind .annotation .XmlElement ;
28-
2926import org .eclipse .jdt .core .IJavaProject ;
3027import org .eclipse .jdt .core .IType ;
3128import org .eclipse .jdt .core .dom .ASTNode ;
137134import org .eclipse .jdt .core .dom .WhileStatement ;
138135import org .eclipse .jdt .core .dom .WildcardType ;
139136
137+ // BH 2019.05.13 fix for Math.getExponent, ulp, nextDown, nextUp, nextAfter needing qualification
138+ // BH 2019.05.13 fix for Function reference in new Foo()::test(...)
140139// BH 2019.04.03 fix for @j2sIgnore not including {}
141140// note to self: It is an annoyance that Eclipse does not recognize an annotation edit
142141// as a need to recompile automatically
148147 * @author Bob Hanson 2017-08,09,10
149148 *
150149 *
151- *
150+ *
152151 */
153152public class Java2ScriptVisitor extends ASTVisitor {
154153
@@ -528,10 +527,7 @@ public boolean visit(ClassInstanceCreation node) {
528527 private void addConstructor (ITypeBinding javaClass ,
529528 // Type type,
530529 IMethodBinding constructorMethodBinding , List <?> arguments , int lambdaArity ) {
531- // String javaClassName = getClassJavaNameForType(type);
532530 String javaClassName = getJavaClassNameQualified (javaClass );
533- // if (!javaClassName.equals(testName))
534- // System.out.println("!!! " + javaClassName + " != " + testName);
535531 if ("java.lang.Object" .equals (javaClassName )) {
536532 buffer .append (" Clazz.new_()" );
537533 return ;
@@ -559,9 +555,17 @@ private void addConstructor(ITypeBinding javaClass,
559555 prefix = ",[" ;
560556 postfix = "]" ;
561557 }
562- if (lambdaArity >= 0 )
563- buffer .append (",[" + this .getLambdaParamList (constructorMethodBinding , lambdaArity ) + "]" );
564- else if (!isDefault ) {
558+ if (lambdaArity >= 0 ) {
559+ buffer .append (",[" );
560+ String finals = htLocalFinals .get (getNormalizedKey (javaClass ));
561+ String params = getLambdaParamList (constructorMethodBinding , lambdaArity );
562+ if (finals != null ) {
563+ buffer .append ("this," ).append (finals );
564+ if (params .length () > 0 )
565+ buffer .append ("," );
566+ }
567+ buffer .append (params ).append ("]" );
568+ } else if (!isDefault ) {
565569 IMethodBinding constructorMethodDeclaration = (constructorMethodBinding == null ? null
566570 : constructorMethodBinding .getMethodDeclaration ());
567571 addMethodParameterList (arguments , constructorMethodDeclaration , prefix , postfix , METHOD_CONSTRUCTOR );
@@ -662,6 +666,7 @@ private void processLocalInstance(ASTNode node, ASTNode anonymousClassDeclaratio
662666
663667// localName = null;
664668 String finals ;
669+
665670 if (isClassTrulyLocal ) {
666671 // predefined by class creation step
667672 finals = htLocalFinals .get (getNormalizedKey (binding ));
@@ -733,17 +738,18 @@ private String addInnerDeclaration(ASTNode node, ITypeBinding binding, int lambd
733738 buffer .append (", " );
734739 }
735740 package_methodStackForFinals .pop ();
736-
737741 // create the finals list {width: width; height: height} and add all
738742 // newly visited variables to class_visitedVars;
739743 String finals = listFinalVariables (myVisitedVars , lastVisitedVars , currentBlock );
740-
741744 // restore class/package parameters
742745 class_visitedVars = lastVisitedVars ;
743746 package_currentBlockForVisit = currentBlock ;
744747 class_isAnonymousOrLocal = wasAnonymous ;
745748 return finals ;
746749 }
750+
751+ static int test = 0 ;
752+
747753
748754 /**
749755 * Generated final variable list for anonymous class creation. Update the
@@ -1236,7 +1242,8 @@ private void addMethodInvocation(SimpleName javaQualifier, List<?> arguments, IM
12361242 // in using "t.apply" is that t must be non-null.
12371243 // if this does not work, then we can go to ($class$.prototype || t)
12381244 //
1239- boolean classIsTarget = ((isStatic || isVariableBinding (expression ))
1245+ boolean classIsTarget = ((isStatic || isVariableBinding (expression )
1246+ || expression instanceof ClassInstanceCreation ) // BH Added 2019.05.13
12401247 && lambdaArity == mBinding .getParameterTypes ().length );
12411248 buffer .append ((classIsTarget ? "$class$." : "t." ) + finalMethodNameWith$Params + ".apply("
12421249 + (isStatic ? "null" : classIsTarget ? "$class$" : "t" ) + ",[" );
@@ -2322,6 +2329,8 @@ private void addInnerTypeInstance(ASTNode node, ITypeBinding superAnonOrInnerCla
23222329 // add constructor application arguments: [object, parameters]
23232330
23242331 buffer .append (", [" );
2332+
2333+
23252334 if (outerClassExpr == null )
23262335 buffer .append ("this" );
23272336 else
@@ -5408,8 +5417,6 @@ private List<Javadoc> getJ2sJavadoc(ASTNode node, int mode) {
54085417 NativeDoc .addJ2sJavadocs (buffer , docs , false );
54095418 } else {
54105419 docs = package_mapBlockJavadoc .get (Integer .valueOf (node .getStartPosition ()));
5411- //?? buffer.append("\n/** looking for " + node + " " + Integer.valueOf(node.getStartPosition())
5412- //?? + " " + (docs != null) + " " + node.getClass().getName() + "*/\n");
54135420 }
54145421 return docs ;
54155422 }
@@ -6074,12 +6081,13 @@ static String checkClassReplacement(String className) {
60746081 private final static String defaultNonQualified
60756082 // Math and Date both are minor extensions
60766083 // of JavaScript, so they are not qualified
6077- = "java.lang.Math;"
6084+ = // "java.lang.Math;" +
60786085 // MAYBE NOT! + "java.util.Date;"
60796086 // swingjs.api.js and javajs.api.js contain
60806087 // interfaces to JavaScript methods and so
60816088 // are not parameterized.
6082- + "*.api.js;"
6089+
6090+ "*.api.js;"
60836091 // netscape.JSObject interface includes 8 methods
60846092 // that do not need to be parameterized.
60856093 // + "netscape.*;"
@@ -6130,6 +6138,18 @@ private static boolean isPackageOrClassNonqualified(String className) {
61306138 * @param methodName not used but could be
61316139 */
61326140 static boolean isMethodNonqualified (String className , String methodName ) {
6141+ if (className .equals ("java.lang.Math" )) {
6142+ switch (methodName ) {
6143+ case "ulp" :
6144+ case "nextDown" :
6145+ case "nextUp" :
6146+ case "nextAfter" :
6147+ case "getExponent" :
6148+ return false ;
6149+ default :
6150+ return true ;
6151+ }
6152+ }
61336153 return (isPackageOrClassNonqualified (className ));
61346154 }
61356155
@@ -6276,9 +6296,6 @@ static class ClassAnnotation {
62766296 private String qName ;
62776297
62786298 protected ClassAnnotation (String qName , Annotation annotation , ASTNode node ) {
6279- // System.out.println(">>>>" + qName + " "
6280- // + annotation.getClass().getName()
6281- // + " " + annotation);
62826299 this .qName = qName ;
62836300 this .annotation = annotation ;
62846301 this .node = node ;
@@ -6297,8 +6314,6 @@ public static void addClassAnnotations(int accessType, List<ClassAnnotation> cla
62976314 for (int i = 0 ; i < class_annotations .size (); i ++) {
62986315 ClassAnnotation a = class_annotations .get (i );
62996316 String str = a .annotation .toString ();
6300- // System.out.println(">>>str " + str);
6301- // System.out.println(">>>ann " + a.annotation.getClass().getName());
63026317 if (a .annotation instanceof NormalAnnotation ) {
63036318 // @XmlElement(name="test",type=Integer.class)
63046319 // remove commas, add quotes
@@ -6312,7 +6327,6 @@ public static void addClassAnnotations(int accessType, List<ClassAnnotation> cla
63126327 // add quotes
63136328 List <ASTNode > expressions = null ;
63146329 Expression e = ((SingleMemberAnnotation ) a .annotation ).getValue ();
6315- // System.out.println(">>>e " + e.getClass().getName());
63166330 if (e instanceof TypeLiteral ) {
63176331 expressions = new ArrayList <ASTNode >();
63186332 expressions .add (e );
@@ -6400,8 +6414,6 @@ public static void addClassAnnotations(int accessType, List<ClassAnnotation> cla
64006414 }
64016415
64026416 private static String annotationNameValue (String name , Object value ) {
6403- // System.out.println(">>>value " + value + " " + value.getClass().getName() + "
6404- // = " + value.toString());
64056417 String str = (name == null ? "" : name + "=" );
64066418 if (value instanceof TypeLiteral ) {
64076419 str += "\" " + ((TypeLiteral ) value ).getType ().resolveBinding ().getQualifiedName () + ".class\" " ;
@@ -6733,17 +6745,6 @@ public static void dumpStack(String msg) {
67336745 */
67346746 public boolean visit (CreationReference node ) {
67356747// // lambda_C
6736- // buffer.append("Clazz.newLambda(function(t){return ");
6737- // Type classType = node.getType();
6738- // ITypeBinding tbinding = classType.resolveBinding();
6739- // if (classType instanceof ArrayType) {
6740- // addArrayConstructor(tbinding, null);
6741- // } else {
6742- // IMethodBinding mBinding = node.resolveMethodBinding();
6743- // addConstructor(classType, mBinding, null);
6744- // }
6745- // buffer.append("},0,'S')");
6746- // was:
67476748 ITypeBinding binding = node .resolveTypeBinding ();
67486749 processLocalInstance (node , null , binding , null , null , LAMBDA_CREATION , false );
67496750 return false ;
@@ -6898,6 +6899,8 @@ private char getLambdaType(ITypeBinding binding) {
68986899 }
68996900
69006901 private void addLambdaBody (ASTNode body ) {
6902+
6903+
69016904 if (body instanceof Block ) {
69026905 body .accept (this );
69036906 } else {
@@ -6938,18 +6941,17 @@ private void addLambdaMethod(ASTNode lnode, IMethodBinding mBinding) {
69386941 return ;
69396942 }
69406943 if (lnode instanceof CreationReference ) {
6941- // int[]::new;
69426944 buffer .append ("/*lambda_C*/" );
69436945 processMethodDeclaration (mBinding , null , null , false , LAMBDA_CREATION );
69446946 CreationReference node = (CreationReference ) lnode ;
69456947 Type ctype = node .getType ();
69466948 ITypeBinding binding = ctype .resolveBinding ();
69476949 if (ctype instanceof ArrayType ) {
6950+ // int[]::new;
69486951 addArrayConstructor (binding , null );
69496952 } else {
6950- addConstructor (binding ,
6951- // ctype,
6952- mBinding , null , mBinding .getParameterTypes ().length );
6953+ // MatchSink::new;
6954+ addConstructor (binding , mBinding , null , mBinding .getParameterTypes ().length );
69536955 }
69546956 buffer .append ("});\r \n " );
69556957 return ;
0 commit comments