File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
main/java/com/google/api/client/util
test/java/com/google/api/client/util Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change 23
23
import java .math .BigDecimal ;
24
24
import java .math .BigInteger ;
25
25
import java .util .ArrayList ;
26
+ import java .util .Arrays ;
26
27
import java .util .Collection ;
27
28
import java .util .Collections ;
28
29
import java .util .HashSet ;
@@ -216,6 +217,14 @@ public static <T> T clone(T data) {
216
217
copy = (T ) Array .newInstance (dataClass .getComponentType (), Array .getLength (data ));
217
218
} else if (data instanceof ArrayMap <?, ?>) {
218
219
copy = (T ) ((ArrayMap <?, ?>) data ).clone ();
220
+ } else if ("java.util.Arrays$ArrayList" .equals (dataClass .getName ())) {
221
+ // 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.
224
+ Object [] arrayCopy = ((List <?>) data ).toArray ();
225
+ deepCopy (arrayCopy , arrayCopy );
226
+ copy = (T ) Arrays .asList (arrayCopy );
227
+ return copy ;
219
228
} else {
220
229
copy = (T ) Types .newInstance (dataClass );
221
230
}
Original file line number Diff line number Diff line change @@ -116,6 +116,22 @@ public void testClone_arrayMap() {
116
116
assertEquals (map , Data .clone (map ));
117
117
}
118
118
119
+ 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 ));
125
+
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
132
+ }
133
+ }
134
+
119
135
public void testNewCollectionInstance () throws Exception {
120
136
assertEquals (ArrayList .class , Data .newCollectionInstance (null ).getClass ());
121
137
assertEquals (ArrayList .class , Data .newCollectionInstance (String [].class ).getClass ());
You can’t perform that action at this time.
0 commit comments