KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > dynamic > DynamicUtil


1 /*
2  * Created on 16-Apr-2003
3  */

4 package com.mockobjects.dynamic;
5
6 import java.lang.reflect.Method JavaDoc;
7 import java.lang.reflect.Proxy JavaDoc;
8
9 import java.util.*;
10 import java.util.List JavaDoc;
11
12 public class DynamicUtil {
13     public static Object JavaDoc[] asObjectArray(Object JavaDoc primitiveArray) {
14         
15         if (primitiveArray instanceof Object JavaDoc[]) {
16             return (Object JavaDoc[])primitiveArray;
17         }
18         
19         List JavaDoc result = new ArrayList();
20         
21         if (primitiveArray instanceof boolean[]) {
22             boolean[] booleanArray = (boolean[]) primitiveArray;
23
24             for (int i = 0; i < booleanArray.length; i++) {
25                 result.add(new Boolean JavaDoc(booleanArray[i]));
26             }
27         } else if (primitiveArray instanceof char[]) {
28             char[] charArray = (char[]) primitiveArray;
29
30             for (int i = 0; i < charArray.length; i++) {
31                 result.add(new Character JavaDoc(charArray[i]));
32             }
33         } else if (primitiveArray instanceof byte[]) {
34             byte[] byteArray = (byte[]) primitiveArray;
35
36             for (int i = 0; i < byteArray.length; i++) {
37                 result.add(new Byte JavaDoc(byteArray[i]));
38             }
39
40         } else if (primitiveArray instanceof short[]) {
41             short[] shortArray = (short[]) primitiveArray;
42
43             for (int i = 0; i < shortArray.length; i++) {
44                 result.add(new Short JavaDoc(shortArray[i]));
45             }
46         } else if (primitiveArray instanceof int[]) {
47             int[] intArray = (int[]) primitiveArray;
48
49             for (int i = 0; i < intArray.length; i++) {
50                 result.add(new Integer JavaDoc(intArray[i]));
51             }
52         } else if (primitiveArray instanceof long[]) {
53             long[] longArray = (long[]) primitiveArray;
54
55             for (int i = 0; i < longArray.length; i++) {
56                 result.add(new Long JavaDoc(longArray[i]));
57             }
58         } else if (primitiveArray instanceof float[]) {
59             float[] floatArray = (float[]) primitiveArray;
60
61             for (int i = 0; i < floatArray.length; i++) {
62                 result.add(new Float JavaDoc(floatArray[i]));
63             }
64         } else if (primitiveArray instanceof double[]) {
65             double[] doulbeArray = (double[]) primitiveArray;
66
67             for (int i = 0; i < doulbeArray.length; i++) {
68                 result.add(new Float JavaDoc(doulbeArray[i]));
69             }
70         } else {
71             throw new RuntimeException JavaDoc("Unknown primitive data type for Object[] conversion " + primitiveArray.toString());
72         }
73
74         return result.toArray();
75     }
76
77     public static String JavaDoc proxyToString(Object JavaDoc element) {
78         if (Proxy.isProxyClass(element.getClass())) {
79             try {
80                 Method JavaDoc mockNameMethod = Mock.class.getDeclaredMethod("getMockName", new Class JavaDoc[0]);
81                 Object JavaDoc debugableResult = Proxy.getInvocationHandler(element).invoke(element, mockNameMethod, new Object JavaDoc[0]);
82                 return debugableResult.toString();
83             } catch (Throwable JavaDoc e) {
84                 return element.getClass().getName();
85             }
86         }
87
88         if (element.getClass().isArray()) {
89             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
90             buf.append("[");
91             join(asObjectArray(element),buf);
92             buf.append("]");
93             return buf.toString();
94         } else {
95             return element.toString();
96         }
97     }
98
99     public static String JavaDoc methodToString(String JavaDoc name, Object JavaDoc[] args) {
100         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
101
102         buf.append(name);
103         buf.append("(");
104         join(args,buf);
105         buf.append(")");
106
107         return buf.toString();
108     }
109
110     public static void join(Object JavaDoc[] elements, StringBuffer JavaDoc buf) {
111         for (int i = 0; i < elements.length; i++) {
112             if (i > 0) {
113                 buf.append(", ");
114             }
115
116             Object JavaDoc element = elements[i];
117
118             if (element.getClass().isArray()) {
119                 buf.append("[");
120                 join(asObjectArray(element), buf);
121                 buf.append("]");
122             } else {
123                 buf.append("<");
124                 buf.append(proxyToString(element));
125                 buf.append(">");
126             }
127         }
128     }
129 }
130
Popular Tags