Revision: 9574
Author: [email protected]
Date: Wed Jan 19 14:08:56 2011
Log: Fix handling of null passed into varargs.

Fixes a bug where passing null into a varargs method in web mode would create a 1-length array containing null. The correct behavior is to pass null in for the array.

Found by: rchandia

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=9574

Modified:
 /trunk/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
 /trunk/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java

=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java Tue Oct 5 11:03:13 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java Wed Jan 19 14:08:56 2011
@@ -2051,10 +2051,11 @@
       }
     }

-    private void addCallArgs(Expression[] args, JMethodCall call,
+    private void addCallArgs(Expression[] jdtArgs, JMethodCall call,
         MethodBinding binding) {
-      if (args == null) {
-        args = new Expression[0];
+ JExpression[] args = new JExpression[jdtArgs == null ? 0 : jdtArgs.length];
+      for (int i = 0; i < args.length; ++i) {
+        args[i] = dispProcessExpression(jdtArgs[i]);
       }

       TypeBinding[] params = binding.parameters;
@@ -2072,32 +2073,29 @@
       }

       for (int i = 0; i < n; ++i) {
-        call.addArg(dispProcessExpression(args[i]));
+        call.addArg(args[i]);
       }

       if (binding.isVarargs()) {
         // Handle the last arg.
-        JArrayType type = (JArrayType) typeMap.get(params[n]);
+        JArrayType lastParamType = (JArrayType) typeMap.get(params[n]);

// See if there is only one arg and it's an array of the correct dims.
         if (args.length == n + 1) {
-          JType lastArgType = (JType) typeMap.get(args[n].resolvedType);
-          if (lastArgType instanceof JArrayType) {
-            JArrayType lastArgArrayType = (JArrayType) lastArgType;
-            if (lastArgArrayType.getDims() == type.getDims()) {
-              // Looks like it's already an array.
-              call.addArg(dispProcessExpression(args[n]));
-              return;
-            }
+          if (program.typeOracle.canTriviallyCast(args[n].getType(),
+              lastParamType)) {
+            // Looks like it's already an array.
+            call.addArg(args[n]);
+            return;
           }
         }

         List<JExpression> initializers = new ArrayList<JExpression>();
         for (int i = n; i < args.length; ++i) {
-          initializers.add(dispProcessExpression(args[i]));
+          initializers.add(args[i]);
         }
         JNewArray newArray = JNewArray.createInitializers(program,
-            call.getSourceInfo(), type, initializers);
+            call.getSourceInfo(), lastParamType, initializers);
         call.addArg(newArray);
       }
     }
=======================================
--- /trunk/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java Fri Apr 4 09:22:37 2008 +++ /trunk/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java Wed Jan 19 14:08:56 2011
@@ -27,6 +27,13 @@
   public String getModuleName() {
     return "com.google.gwt.dev.jjs.CompilerSuite";
   }
+
+  public void testNullEmpty() {
+    assertNotNull(vararg());
+    assertNull(vararg(null));
+    assertNotNull(vararg((String) null));
+    assertNull(vararg((String[]) null));
+  }

   public void testVararg() {
     String[] expected = new String[] {"1", "2", "3"};

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to