1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.innergrid.api;
18
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22
23 import org.apache.axis.InternalException;
24
25
26
27
28
29
30
31 public final class CloneUtils {
32
33
34
35 private CloneUtils() {
36 }
37
38
39
40
41
42
43
44
45 @SuppressWarnings("unchecked")
46 public static <T> T[] cloneArray(T[] source)
47 throws CloneNotSupportedException {
48 if (source == null) {
49 return null;
50 }
51 Class theClass = source.getClass().getComponentType();
52 if (!Cloneable.class.isAssignableFrom(theClass)) {
53 return source.clone();
54 }
55 int length = Array.getLength(source);
56
57 T[] newArray = (T[])Array.newInstance(theClass, length);
58
59 try {
60 Method mclone = theClass.getMethod("clone");
61 for (int i = 0; i < length; i++) {
62 if (source[i] == null) {
63 newArray[i] = null;
64 } else {
65 newArray[i] = (T)mclone.invoke(source[i]);
66 }
67 }
68 } catch (InvocationTargetException e) {
69 throw new InternalException(e);
70 } catch (Exception e) {
71 throw new CloneNotSupportedException(e.toString());
72 }
73 return newArray;
74 }
75
76
77 }