Changed behavior of Arrays.asList().toArray() to match new JDK sematics.

Now Arrays.asList(T[]).toArray() returns Object[] instead of T[] to
match toArray() semantics.

The RPC code that serialized lists produced by Arrays.asList() does
not depend anymore on the result type of toArray().

Change-Id: Ic8b2b0326c3f961df6bd0945c12cd4dca48d46cb
diff --git a/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java b/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java
index 3ffb204..e33757d 100644
--- a/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java
+++ b/user/src/com/google/gwt/user/client/rpc/core/java/util/Arrays.java
@@ -15,7 +15,6 @@
  */
 package com.google.gwt.user.client.rpc.core.java.util;
 
-import com.google.gwt.core.shared.GWT;
 import com.google.gwt.user.client.rpc.CustomFieldSerializer;
 import com.google.gwt.user.client.rpc.SerializationException;
 import com.google.gwt.user.client.rpc.SerializationStreamReader;
@@ -32,20 +31,12 @@
    * Custom field serializer for {@link java.util.Arrays.ArrayList}.
    */
   @SuppressWarnings("rawtypes")
-  public static final class ArrayList_CustomFieldSerializer extends
-      CustomFieldSerializer<List> {
+  public static final class ArrayList_CustomFieldSerializer extends CustomFieldSerializer<List> {
 
     public static String concreteType() {
       return java.util.Arrays.asList().getClass().getName();
     }
 
-    /*
-     * Note: the reason this implementation differs from that of a standard List
-     * (which serializes a number and then each element) is the requirement that
-     * the underlying array retain its correct type across the wire. This gives
-     * toArray() results the correct type, and can generate internal
-     * ArrayStoreExceptions.
-     */
     @SuppressWarnings("unused")
     public static void deserialize(SerializationStreamReader streamReader,
         List<?> instance) throws SerializationException {
@@ -54,21 +45,21 @@
 
     public static List<?> instantiate(SerializationStreamReader streamReader)
         throws SerializationException {
-      Object[] array = (Object[]) streamReader.readObject();
+      int size = streamReader.readInt();
+      Object[] array = new Object[size];
+      for (int i = 0; i < size; ++i) {
+        array[i] = streamReader.readObject();
+      }
       return java.util.Arrays.asList(array);
     }
 
     public static void serialize(SerializationStreamWriter streamWriter,
         List<?> instance) throws SerializationException {
-      Object[] array;
-      if (GWT.isScript()) {
-        // Violator pattern.
-        array = ArraysViolator.getArray0(instance);
-      } else {
-        // Clone the underlying array.
-        array = instance.toArray();
+      int size = instance.size();
+      streamWriter.writeInt(size);
+      for (Object obj : instance) {
+        streamWriter.writeObject(obj);
       }
-      streamWriter.writeObject(array);
     }
 
     @Override
diff --git a/user/src/com/google/gwt/user/server/rpc/core/java/util/Arrays.java b/user/src/com/google/gwt/user/server/rpc/core/java/util/Arrays.java
index ff647f7..0159d25 100644
--- a/user/src/com/google/gwt/user/server/rpc/core/java/util/Arrays.java
+++ b/user/src/com/google/gwt/user/server/rpc/core/java/util/Arrays.java
@@ -20,10 +20,8 @@
 import com.google.gwt.user.client.rpc.SerializationStreamWriter;
 import com.google.gwt.user.server.rpc.ServerCustomFieldSerializer;
 import com.google.gwt.user.server.rpc.impl.DequeMap;
-import com.google.gwt.user.server.rpc.impl.SerializabilityUtil;
 import com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader;
 
-import java.lang.reflect.Array;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 import java.util.List;
@@ -44,39 +42,17 @@
       return java.util.Arrays.asList().getClass().getName();
     }
 
-    /*
-     * Note: the reason this implementation differs from that of a standard List
-     * (which serializes a number and then each element) is the requirement that
-     * the underlying array retain its correct type across the wire. This gives
-     * toArray() results the correct type, and can generate internal
-     * ArrayStoreExceptions.
-     * 
-     * The type checking is messy because we need some way of converting the
-     * List<X> or related type that we are expecting into the array type that we
-     * are about to try to read. You can't create objects of class Type
-     * directly, so we need to create a dummy array and then use it's class as a
-     * type.
-     */
     public static List<?> instantiate(ServerSerializationStreamReader streamReader,
         Type[] expectedParameterTypes, DequeMap<TypeVariable<?>, Type> resolvedTypes)
         throws SerializationException {
-      Class<?> componentClass = SerializabilityUtil.getClassFromType(expectedParameterTypes[0],
-          resolvedTypes);
-      if (componentClass == null) {
-        return com.google.gwt.user.client.rpc.core.java.util.Arrays.ArrayList_CustomFieldSerializer
+      return com.google.gwt.user.client.rpc.core.java.util.Arrays.ArrayList_CustomFieldSerializer
             .instantiate(streamReader);
-      }
-      
-      Object expectedArray = Array.newInstance(componentClass, 0);
-      Object[] array = (Object[]) streamReader.readObject(expectedArray.getClass(), resolvedTypes);
-      return java.util.Arrays.asList(array);
     }
 
     @Override
     public void deserializeInstance(SerializationStreamReader streamReader, List instance)
         throws SerializationException {
-      com.google.gwt.user.client.rpc.core.java.util.Arrays.ArrayList_CustomFieldSerializer
-          .deserialize(streamReader, instance);
+      // Handled in instantiateInstance.
     }
 
     @SuppressWarnings("unused")
@@ -114,3 +90,4 @@
     }
   }
 }
+
diff --git a/user/super/com/google/gwt/emul/java/util/Arrays.java b/user/super/com/google/gwt/emul/java/util/Arrays.java
index c60eda7..cd9f352 100644
--- a/user/super/com/google/gwt/emul/java/util/Arrays.java
+++ b/user/super/com/google/gwt/emul/java/util/Arrays.java
@@ -73,12 +73,9 @@
       return array.length;
     }
 
-    /*
-     * Semantics are to return an array of identical type.
-     */
     @Override
     public Object[] toArray() {
-      return ArrayHelper.clone(array, 0, array.length);
+      return toArray(new Object[array.length]);
     }
 
     /*