/Users/lyon/j4p/src/classUtils/pack/util/TypeWrapper.java

1    package classUtils.pack.util; 
2     
3    import java.lang.reflect.Array; 
4     
5    import classUtils.pack.util.util.SignatureAnalyzer; 
6     
7    /** 
8     * An helper class to automatically wrap primitive types 
9     * into their corresponding object types. 
10    *  
11    * @author Cristiano Sadun 
12    */ 
13   public class TypeWrapper { 
14    
15       public static Object wrap(byte v) { 
16           return new Byte(v); 
17       } 
18       public static Object wrap(short v) { 
19           return new Short(v); 
20       } 
21       public static Object wrap(int v) { 
22           return new Integer(v); 
23       } 
24       public static Object wrap(long v) { 
25           return new Long(v); 
26       } 
27       public static Object wrap(float v) { 
28           return new Float(v); 
29       } 
30       public static Object wrap(double v) { 
31           return new Double(v); 
32       } 
33       public static Object wrap(char v) { 
34           return new Character(v); 
35       } 
36       public static Object wrap(boolean v) { 
37           return new Boolean(v); 
38       } 
39    
40       public static Object wrap(Object obj) { 
41    
42           if (!obj.getClass().isArray()) 
43               throw new IllegalArgumentException("wrap(Object) can be invoked only on arrays of primitive types"); 
44    
45           Object[] array = (Object[]) obj; 
46           Class componentType = obj.getClass().getComponentType(); 
47           Class baseComponentType = componentType; 
48    
49           int dim = 1; 
50    
51           while (baseComponentType.isArray()) { 
52               baseComponentType = baseComponentType.getComponentType(); 
53               dim++; 
54           } 
55    
56           if (!baseComponentType.isPrimitive()) 
57               throw new IllegalArgumentException("wrap(Object) can be invoked only on arrays of primitive types"); 
58    
59           return wrap0(array, componentType, baseComponentType, dim); 
60       } 
61    
62       private static Object wrap0( 
63           Object[] array, 
64           Class componentType, 
65           Class baseComponentType, 
66           int dim) { 
67           Object[] result = null; 
68           for (int i = 0; i < array.length; i++) { 
69               if (componentType.isArray()) { 
70                   // Component type is an array. If the value of the ith element is 
71                   // not null, go creating the subarray and assign it 
72                   if (array[i] == null) { 
73                       result[i] = null; 
74                       continue; 
75                   } else { 
76                       Object[] elementArray = (Object[]) array[i]; 
77                       result[i] = 
78                           wrap0( 
79                               elementArray, 
80                               componentType.getComponentType(), 
81                               baseComponentType, 
82                               dim - 1); 
83                   } 
84               } else { 
85                   // Component type is not an array. Since we're dealing with primitive 
86                   // types, array elements are never null. 
87    
88                   // Let's create a corresponding array of wrappers        
89                   result = 
90                       (Object[]) Array.newInstance( 
91                           TypeWrapper.getWrapperClass(baseComponentType), 
92                           1); 
93                   // Now, let's fill the values with wrapper objects 
94                   for (int j = 0; j < result.length; j++) { 
95    
96                   } 
97               } 
98           } 
99           return result; 
100      } 
101   
102      /* 
103      private static Object [] wrap0(Object obj, int level, Class [] baseType) {  
104          if (!obj.getClass().isArray())  
105              throw new IllegalArgumentException("wrap(Object) can be invoked only on arrays of primitive types"); 
106               
107          Class componentType = obj.getClass().getComponentType(); 
108          if (componentType.isArray()) { 
109              // For each element, we need to obtain the wrapped array 
110              Object [] result=null; 
111              for(int i=0;i<((Object[])obj).length;i++) { 
112                  Object elem = ((Object[])obj)[i]; 
113                  Object [] wrapped = wrap0(elem, level+1, baseType); 
114                  // In "basetype", we've recorded the primitive type, 
115                  // so we create an appropriate wrapper type array  
116                  if (result==null) { 
117                      try { 
118                          result = Array.n.newInstance() 
119                      } catch (Exception e) { 
120                          throw new RuntimeException(e); 
121                      } 
122                      System.exit(0); 
123                  } 
124              /* 
125              if (componentType==byte[].class) wrappedArray=wrap0((byte) 
126              else if (componentType==Short.TYPE) wrappedValue=wrap(((short[])obj)[i]); 
127              else if (componentType==Integer.TYPE) wrappedValue=wrap(((int[])obj)[i]); 
128              else if (componentType==Long.TYPE) wrappedValue=wrap(((long[])obj)[i]); 
129              else if (componentType==Float.TYPE) wrappedValue=wrap(((float[])obj)[i]); 
130              else if (componentType==Double.TYPE) wrappedValue=wrap(((double[])obj)[i]); 
131              else if (componentType==Character.TYPE) wrappedValue=wrap(((char[])obj)[i]); 
132              else if (componentType==Boolean.TYPE) wrappedValue=wrap(((boolean[])obj)[i]); 
133              * 
134              } 
135              return result; 
136          } 
137           
138          if (!componentType.isPrimitive()) 
139              throw new IllegalArgumentException("wrap(Object) can be invoked only on arrays of primitive types"); 
140           
141          // Create an array of the wrapper type, and assign it 
142          // with wraps of the original value 
143          int length = getArrayLength(obj); 
144          baseType[0]=componentType; 
145          Object [] wrapperArray = createWrapperArray(componentType, length); 
146          for(int i=0;i<length;i++) { 
147              Object wrappedValue=null; 
148              if (componentType==Byte.TYPE) wrappedValue=wrap(((byte[])obj)[i]); 
149              else if (componentType==Short.TYPE) wrappedValue=wrap(((short[])obj)[i]); 
150              else if (componentType==Integer.TYPE) wrappedValue=wrap(((int[])obj)[i]); 
151              else if (componentType==Long.TYPE) wrappedValue=wrap(((long[])obj)[i]); 
152              else if (componentType==Float.TYPE) wrappedValue=wrap(((float[])obj)[i]); 
153              else if (componentType==Double.TYPE) wrappedValue=wrap(((double[])obj)[i]); 
154              else if (componentType==Character.TYPE) wrappedValue=wrap(((char[])obj)[i]); 
155              else if (componentType==Boolean.TYPE) wrappedValue=wrap(((boolean[])obj)[i]); 
156              wrapperArray[i]=wrappedValue; 
157          } 
158          return wrapperArray; 
159      } 
160      */ 
161   
162      private static int getArrayLength(Object array) { 
163          Class componentType = array.getClass().getComponentType(); 
164          if (componentType == Byte.TYPE) 
165              return ((byte[]) array).length; 
166          if (componentType == Short.TYPE) 
167              return ((short[]) array).length; 
168          if (componentType == Integer.TYPE) 
169              return ((int[]) array).length; 
170          if (componentType == Long.TYPE) 
171              return ((long[]) array).length; 
172          if (componentType == Float.TYPE) 
173              return ((float[]) array).length; 
174          if (componentType == Double.TYPE) 
175              return ((double[]) array).length; 
176          if (componentType == Character.TYPE) 
177              return ((char[]) array).length; 
178          if (componentType == Boolean.TYPE) 
179              return ((boolean[]) array).length; 
180          throw new IllegalArgumentException("getArrayLength() called over an array of nonprimitive types"); 
181   
182      } 
183   
184      private static Object[] createWrapperArray( 
185          Class componentType, 
186          int length) { 
187          if (componentType == Byte.TYPE) 
188              return new Byte[length]; 
189          if (componentType == Short.TYPE) 
190              return new Short[length]; 
191          if (componentType == Integer.TYPE) 
192              return new Integer[length]; 
193          if (componentType == Long.TYPE) 
194              return new Long[length]; 
195          if (componentType == Float.TYPE) 
196              return new Float[length]; 
197          if (componentType == Double.TYPE) 
198              return new Double[length]; 
199          if (componentType == Character.TYPE) 
200              return new Character[length]; 
201          if (componentType == Boolean.TYPE) 
202              return new Boolean[length]; 
203          throw new IllegalArgumentException("createWrapperArray() called over a nonprimitive type"); 
204   
205      } 
206   
207      /** 
208       * Return the class of the wrapper object for the given 
209       * primitive type. 
210       *  
211       * @param primitiveCls the Class of the primitive type 
212       * @return Class the wrapper class 
213       */ 
214      public static Class getWrapperClass(Class primitiveCls) { 
215          if (primitiveCls == Byte.TYPE) 
216              return Byte.class; 
217          if (primitiveCls == Short.TYPE) 
218              return Short.class; 
219          if (primitiveCls == Integer.TYPE) 
220              return Integer.class; 
221          if (primitiveCls == Long.TYPE) 
222              return Long.class; 
223          if (primitiveCls == Float.TYPE) 
224              return Float.class; 
225          if (primitiveCls == Double.TYPE) 
226              return Double.class; 
227          if (primitiveCls == Character.TYPE) 
228              return Character.class; 
229          if (primitiveCls == Boolean.TYPE) 
230              return Boolean.class; 
231          throw new IllegalArgumentException("getWrapper() called over a nonprimitive type"); 
232      } 
233   
234      /** 
235       * Return a wrapper object corresponding to passed value, interpreted 
236       * according to the given primitive class. 
237       *  
238       * @param primitiveCls the Class of the primitive type 
239       * @param value the value to intepret 
240       */ 
241      public static Object wrapFromString(Class primitiveCls, String value) { 
242          try { 
243              if (primitiveCls == Byte.TYPE) 
244                  return new Byte(value); 
245              if (primitiveCls == Short.TYPE) 
246                  return new Short(value); 
247              if (primitiveCls == Integer.TYPE) 
248                  return new Integer(value); 
249              if (primitiveCls == Long.TYPE) 
250                  return new Long(value); 
251              if (primitiveCls == Float.TYPE) 
252                  return new Float(value); 
253              if (primitiveCls == Double.TYPE) 
254                  return new Double(value); 
255              if (primitiveCls == Character.TYPE) { 
256                  if (value.length() != 1) 
257                      throw new IllegalArgumentException("Invalid value for character type"); 
258                  return new Character(value.charAt(0)); 
259              } 
260              if (primitiveCls == Boolean.TYPE) 
261                  return new Boolean(value); 
262          } catch (NumberFormatException e) { 
263              throw new IllegalArgumentException( 
264                  "'" 
265                      + value 
266                      + "' is not a correct representation for the type " 
267                      + SignatureAnalyzer.getJavaTypeName(primitiveCls)); 
268          } 
269          throw new IllegalArgumentException("getWrapper() called over a nonprimitive type"); 
270      } 
271   
272      /*public Object interpretPrimitiveString(String value, Class cls) { 
273          Class primitiveCls=getWrapperClass(cls); 
274          return primitiveCls. 
275      }*/ 
276   
277      public static void main(String args[]) { 
278   
279          Integer[][] wrapped = 
280              ( 
281                  Integer[][]) TypeWrapper 
282                      .wrap(new int[][] { new int[] { 2, 5 }, new int[] { 3, 6 } 
283          }); 
284      } 
285   
286  } 
287