1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared_orchestra.util;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.FacesException;
25 import javax.servlet.jsp.el.ELException;
26 import java.io.InputStream;
27 import java.io.IOException;
28 import java.lang.reflect.Array;
29 import java.util.*;
30
31
32
33
34
35
36
37 public final class ClassUtils
38 {
39
40
41 private static final Log log = LogFactory.getLog(ClassUtils.class);
42
43
44 public static final Class BOOLEAN_ARRAY_CLASS = boolean[].class;
45 public static final Class BYTE_ARRAY_CLASS = byte[].class;
46 public static final Class CHAR_ARRAY_CLASS = char[].class;
47 public static final Class SHORT_ARRAY_CLASS = short[].class;
48 public static final Class INT_ARRAY_CLASS = int[].class;
49 public static final Class LONG_ARRAY_CLASS = long[].class;
50 public static final Class FLOAT_ARRAY_CLASS = float[].class;
51 public static final Class DOUBLE_ARRAY_CLASS = double[].class;
52 public static final Class OBJECT_ARRAY_CLASS = Object[].class;
53 public static final Class BOOLEAN_OBJECT_ARRAY_CLASS = Boolean[].class;
54 public static final Class BYTE_OBJECT_ARRAY_CLASS = Byte[].class;
55 public static final Class CHARACTER_OBJECT_ARRAY_CLASS = Character[].class;
56 public static final Class SHORT_OBJECT_ARRAY_CLASS = Short[].class;
57 public static final Class INTEGER_OBJECT_ARRAY_CLASS = Integer[].class;
58 public static final Class LONG_OBJECT_ARRAY_CLASS = Long[].class;
59 public static final Class FLOAT_OBJECT_ARRAY_CLASS = Float[].class;
60 public static final Class DOUBLE_OBJECT_ARRAY_CLASS = Double[].class;
61 public static final Class STRING_OBJECT_ARRAY_CLASS = String[].class;
62
63 public static final Map COMMON_TYPES = new HashMap(64);
64 static
65 {
66 COMMON_TYPES.put("byte", Byte.TYPE);
67 COMMON_TYPES.put("char", Character.TYPE);
68 COMMON_TYPES.put("double", Double.TYPE);
69 COMMON_TYPES.put("float", Float.TYPE);
70 COMMON_TYPES.put("int", Integer.TYPE);
71 COMMON_TYPES.put("long", Long.TYPE);
72 COMMON_TYPES.put("short", Short.TYPE);
73 COMMON_TYPES.put("boolean", Boolean.TYPE);
74 COMMON_TYPES.put("void", Void.TYPE);
75 COMMON_TYPES.put("java.lang.Object", Object.class);
76 COMMON_TYPES.put("java.lang.Boolean", Boolean.class);
77 COMMON_TYPES.put("java.lang.Byte", Byte.class);
78 COMMON_TYPES.put("java.lang.Character", Character.class);
79 COMMON_TYPES.put("java.lang.Short", Short.class);
80 COMMON_TYPES.put("java.lang.Integer", Integer.class);
81 COMMON_TYPES.put("java.lang.Long", Long.class);
82 COMMON_TYPES.put("java.lang.Float", Float.class);
83 COMMON_TYPES.put("java.lang.Double", Double.class);
84 COMMON_TYPES.put("java.lang.String", String.class);
85
86 COMMON_TYPES.put("byte[]", BYTE_ARRAY_CLASS);
87 COMMON_TYPES.put("char[]", CHAR_ARRAY_CLASS);
88 COMMON_TYPES.put("double[]", DOUBLE_ARRAY_CLASS);
89 COMMON_TYPES.put("float[]", FLOAT_ARRAY_CLASS);
90 COMMON_TYPES.put("int[]", INT_ARRAY_CLASS);
91 COMMON_TYPES.put("long[]", LONG_ARRAY_CLASS);
92 COMMON_TYPES.put("short[]", SHORT_ARRAY_CLASS);
93 COMMON_TYPES.put("boolean[]", BOOLEAN_ARRAY_CLASS);
94 COMMON_TYPES.put("java.lang.Object[]", OBJECT_ARRAY_CLASS);
95 COMMON_TYPES.put("java.lang.Boolean[]", BOOLEAN_OBJECT_ARRAY_CLASS);
96 COMMON_TYPES.put("java.lang.Byte[]", BYTE_OBJECT_ARRAY_CLASS);
97 COMMON_TYPES.put("java.lang.Character[]", CHARACTER_OBJECT_ARRAY_CLASS);
98 COMMON_TYPES.put("java.lang.Short[]", SHORT_OBJECT_ARRAY_CLASS);
99 COMMON_TYPES.put("java.lang.Integer[]", INTEGER_OBJECT_ARRAY_CLASS);
100 COMMON_TYPES.put("java.lang.Long[]", LONG_OBJECT_ARRAY_CLASS);
101 COMMON_TYPES.put("java.lang.Float[]", FLOAT_OBJECT_ARRAY_CLASS);
102 COMMON_TYPES.put("java.lang.Double[]", DOUBLE_OBJECT_ARRAY_CLASS);
103 COMMON_TYPES.put("java.lang.String[]", STRING_OBJECT_ARRAY_CLASS);
104
105 }
106
107
108 private ClassUtils()
109 {
110
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public static Class classForName(String type)
126 throws ClassNotFoundException
127 {
128 if (type == null) throw new NullPointerException("type");
129 try
130 {
131
132 return Class.forName(type,
133 false,
134 Thread.currentThread().getContextClassLoader());
135 }
136 catch (ClassNotFoundException ignore)
137 {
138
139 return Class.forName(type,
140 false,
141 ClassUtils.class.getClassLoader());
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154 public static Class simpleClassForName(String type)
155 {
156 try
157 {
158 return classForName(type);
159 }
160 catch (ClassNotFoundException e)
161 {
162 log.error("Class " + type + " not found", e);
163 throw new FacesException(e);
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178 public static Class javaTypeToClass(String type)
179 throws ClassNotFoundException
180 {
181 if (type == null) throw new NullPointerException("type");
182
183
184 Class clazz = (Class) COMMON_TYPES.get(type);
185 if (clazz != null)
186 {
187 return clazz;
188 }
189
190 int len = type.length();
191 if (len > 2 && type.charAt(len - 1) == ']' && type.charAt(len - 2) == '[')
192 {
193 String componentType = type.substring(0, len - 2);
194 Class componentTypeClass = classForName(componentType);
195 return Array.newInstance(componentTypeClass, 0).getClass();
196 }
197 else
198 {
199 return classForName(type);
200 }
201 }
202
203
204
205
206
207
208
209
210
211
212 public static Class simpleJavaTypeToClass(String type)
213 {
214 try
215 {
216 return javaTypeToClass(type);
217 }
218 catch (ClassNotFoundException e)
219 {
220 log.error("Class " + type + " not found", e);
221 throw new FacesException(e);
222 }
223 }
224
225 public static InputStream getResourceAsStream(String resource)
226 {
227 InputStream stream = Thread.currentThread().getContextClassLoader()
228 .getResourceAsStream(resource);
229 if (stream == null)
230 {
231
232 stream = ClassUtils.class.getClassLoader().getResourceAsStream(resource);
233 }
234 return stream;
235 }
236
237
238
239
240
241
242 public static Iterator getResources(String resource, Object defaultObject)
243 {
244 try
245 {
246 Enumeration resources = getCurrentLoader(defaultObject).getResources(resource);
247 List lst = new ArrayList();
248 while (resources.hasMoreElements())
249 {
250 lst.add(resources.nextElement());
251 }
252 return lst.iterator();
253 }
254 catch (IOException e)
255 {
256 log.error(e.getMessage(), e);
257 throw new FacesException(e);
258 }
259 }
260
261
262 public static Object newInstance(String type)
263 throws FacesException
264 {
265 if (type == null) return null;
266 return newInstance(simpleClassForName(type));
267 }
268
269 public static Object newInstance(String type, Class expectedType) throws FacesException
270 {
271 return newInstance(type, expectedType == null ? null : new Class[] {expectedType});
272 }
273
274 public static Object newInstance(String type, Class[] expectedTypes)
275 {
276 if (type == null)
277 return null;
278
279 Class clazzForName = simpleClassForName(type);
280
281 if(expectedTypes != null)
282 {
283 for (int i = 0, size = expectedTypes.length; i < size; i++)
284 {
285 if (!expectedTypes[i].isAssignableFrom(clazzForName))
286 {
287 throw new FacesException("'" + type + "' does not implement expected type '" + expectedTypes[i]
288 + "'");
289 }
290 }
291 }
292
293 return newInstance(clazzForName);
294 }
295
296 public static Object newInstance(Class clazz)
297 throws FacesException
298 {
299 try
300 {
301 return clazz.newInstance();
302 }
303 catch(NoClassDefFoundError e)
304 {
305 log.error("Class : "+clazz.getName()+" not found.",e);
306 throw new FacesException(e);
307 }
308 catch (InstantiationException e)
309 {
310 log.error(e.getMessage(), e);
311 throw new FacesException(e);
312 }
313 catch (IllegalAccessException e)
314 {
315 log.error(e.getMessage(), e);
316 throw new FacesException(e);
317 }
318 }
319
320 public static Object convertToType(Object value, Class desiredClass)
321 {
322 if (value == null) return null;
323
324 try
325 {
326
327
328
329
330 return _Coercions.coerce(value, desiredClass);
331 }
332 catch (ELException e)
333 {
334 String message = "Cannot coerce " + value.getClass().getName()
335 + " to " + desiredClass.getName();
336 log.error(message, e);
337 throw new FacesException(message, e);
338 }
339 }
340
341
342
343
344
345
346
347
348 protected static ClassLoader getCurrentLoader(Object defaultObject)
349 {
350 ClassLoader loader = Thread.currentThread().getContextClassLoader();
351 if(loader == null)
352 {
353 loader = defaultObject.getClass().getClassLoader();
354 }
355 return loader;
356 }
357 }