Skip to content

Commit bec59ea

Browse files
martinrbejona86
authored andcommitted
Google code should not be depending on this bug in the JDK, which is
due to be fixed in jdk9: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class https://bugs.openjdk.java.net/browse/JDK-6260652 This change is not 100% compatible. If we have list = Arrays.asList("a", "b", "c"); then ((List)list).set(0, 1) will fail with ArrayStoreException, but this will not be true of its clone. Trying to patch that nano-bug via reflection or serialization will be slow and land you in different kind of hot water - not worth it. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=100143653
1 parent 4101659 commit bec59ea

File tree

2 files changed

+14
-13
lines changed
  • google-http-client/src

2 files changed

+14
-13
lines changed

google-http-client/src/main/java/com/google/api/client/util/Data.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ public static <T> T clone(T data) {
219219
copy = (T) ((ArrayMap<?, ?>) data).clone();
220220
} else if ("java.util.Arrays$ArrayList".equals(dataClass.getName())) {
221221
// Arrays$ArrayList does not have a zero-arg constructor, so it has to handled specially.
222-
// Although it appears as an Object[], arrayCopy shares the same type as the array originally
223-
// passed to Arrays.asList() because Arrays$ArrayList uses clone() to create it.
222+
// Arrays.asList(x).toArray() may or may not have the same runtime type as x.
223+
// https://bugs.openjdk.java.net/browse/JDK-6260652
224224
Object[] arrayCopy = ((List<?>) data).toArray();
225225
deepCopy(arrayCopy, arrayCopy);
226226
copy = (T) Arrays.asList(arrayCopy);

google-http-client/src/test/java/com/google/api/client/util/DataTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,19 @@ public void testClone_arrayMap() {
117117
}
118118

119119
public void testClone_ArraysAsList() {
120-
List<Object> orig = Arrays.<Object>asList("a", "b", "c", new ArrayList<Object>());
121-
List<Object> result = Data.clone(orig);
122-
assertTrue(orig != result);
123-
assertEquals(orig, result);
124-
assertTrue(orig.get(3) != result.get(3));
120+
{
121+
List<Object> orig = Arrays.<Object>asList("a", "b", "c", new ArrayList<Object>());
122+
List<Object> result = Data.clone(orig);
123+
assertTrue(orig != result);
124+
assertEquals(orig, result);
125+
assertTrue(orig.get(3) != result.get(3));
126+
}
125127

126-
List list = Data.clone(Arrays.asList(new String[] {"a", "b", "c"}));
127-
try {
128-
list.set(0, new Object());
129-
fail("Cloned list should be backed by String[], not Object[]");
130-
} catch (ArrayStoreException e) {
131-
// expected
128+
{
129+
List<String> orig = Arrays.asList(new String[] {"a", "b", "c"});
130+
List<String> result = Data.clone(orig);
131+
assertTrue(orig != result);
132+
assertEquals(orig, result);
132133
}
133134
}
134135

0 commit comments

Comments
 (0)