1 4 package com.mockobjects.dynamic; 5 6 import java.lang.reflect.Method ; 7 import java.lang.reflect.Proxy ; 8 9 import java.util.*; 10 import java.util.List ; 11 12 public class DynamicUtil { 13 public static Object [] asObjectArray(Object primitiveArray) { 14 15 if (primitiveArray instanceof Object []) { 16 return (Object [])primitiveArray; 17 } 18 19 List 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 (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 (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 (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 (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 (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 (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 (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 (doulbeArray[i])); 69 } 70 } else { 71 throw new RuntimeException ("Unknown primitive data type for Object[] conversion " + primitiveArray.toString()); 72 } 73 74 return result.toArray(); 75 } 76 77 public static String proxyToString(Object element) { 78 if (Proxy.isProxyClass(element.getClass())) { 79 try { 80 Method mockNameMethod = Mock.class.getDeclaredMethod("getMockName", new Class [0]); 81 Object debugableResult = Proxy.getInvocationHandler(element).invoke(element, mockNameMethod, new Object [0]); 82 return debugableResult.toString(); 83 } catch (Throwable e) { 84 return element.getClass().getName(); 85 } 86 } 87 88 if (element.getClass().isArray()) { 89 StringBuffer buf = new StringBuffer (); 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 methodToString(String name, Object [] args) { 100 StringBuffer buf = new StringBuffer (); 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 [] elements, StringBuffer buf) { 111 for (int i = 0; i < elements.length; i++) { 112 if (i > 0) { 113 buf.append(", "); 114 } 115 116 Object 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 |