KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > ClassUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.util;
17
18 import org.apache.commons.el.Coercions;
19 import org.apache.commons.el.Logger;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.faces.FacesException;
24 import javax.servlet.jsp.el.ELException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.lang.reflect.Array JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30
31 /**
32  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
33  * @author Anton Koinov
34  * @version $Revision: 1.11 $ $Date: 2005/01/19 13:18:04 $
35  * $Log: ClassUtils.java,v $
36  * Revision 1.11 2005/01/19 13:18:04 mmarinschek
37  * better logging of component information
38  *
39  * Revision 1.10 2004/10/13 11:51:01 matze
40  * renamed packages to org.apache
41  *
42  * Revision 1.9 2004/10/05 22:34:21 dave0000
43  * bug 1021656 with related improvements
44  *
45  * Revision 1.8 2004/08/10 10:57:38 manolito
46  * fixed StackOverflow in ClassUtils and cleaned up ClassUtils methods
47  *
48  * Revision 1.7 2004/08/05 22:55:51 o_rossmueller
49  * fix: resolve primitive classes
50  *
51  * Revision 1.6 2004/07/13 04:59:25 tinytoony
52  * primitive types where not retrieved (call to javaTypeToClass not used)
53  *
54  * Revision 1.5 2004/07/13 04:56:55 tinytoony
55  * primitive types where not retrieved (call to javaTypeToClass not used)
56  *
57  * Revision 1.4 2004/07/01 22:01:13 mwessendorf
58  * ASF switch
59  *
60  * Revision 1.3 2004/05/17 14:28:29 manolito
61  * new configuration concept
62  *
63  * Revision 1.2 2004/05/11 04:24:10 dave0000
64  * Bug 943166: add value coercion to ManagedBeanConfigurator
65  *
66  * Revision 1.1 2004/03/31 11:58:45 manolito
67  * custom component refactoring
68  *
69  * Revision 1.12 2004/03/30 13:27:50 manolito
70  * new getResourceAsStream method
71  *
72  */

73 public class ClassUtils
74 {
75     //~ Static fields/initializers -----------------------------------------------------------------
76

77     private static final Log log = LogFactory.getLog(ClassUtils.class);
78     private static final Logger COERCION_LOGGER = new Logger(System.out);
79     
80     public static final Class JavaDoc BOOLEAN_ARRAY_CLASS = boolean[].class;
81     public static final Class JavaDoc BYTE_ARRAY_CLASS = byte[].class;
82     public static final Class JavaDoc CHAR_ARRAY_CLASS = char[].class;
83     public static final Class JavaDoc SHORT_ARRAY_CLASS = short[].class;
84     public static final Class JavaDoc INT_ARRAY_CLASS = int[].class;
85     public static final Class JavaDoc LONG_ARRAY_CLASS = long[].class;
86     public static final Class JavaDoc FLOAT_ARRAY_CLASS = float[].class;
87     public static final Class JavaDoc DOUBLE_ARRAY_CLASS = double[].class;
88     public static final Class JavaDoc OBJECT_ARRAY_CLASS = Object JavaDoc[].class;
89     public static final Class JavaDoc BOOLEAN_OBJECT_ARRAY_CLASS = Boolean JavaDoc[].class;
90     public static final Class JavaDoc BYTE_OBJECT_ARRAY_CLASS = Byte JavaDoc[].class;
91     public static final Class JavaDoc CHARACTER_OBJECT_ARRAY_CLASS = Character JavaDoc[].class;
92     public static final Class JavaDoc SHORT_OBJECT_ARRAY_CLASS = Short JavaDoc[].class;
93     public static final Class JavaDoc INTEGER_OBJECT_ARRAY_CLASS = Integer JavaDoc[].class;
94     public static final Class JavaDoc LONG_OBJECT_ARRAY_CLASS = Long JavaDoc[].class;
95     public static final Class JavaDoc FLOAT_OBJECT_ARRAY_CLASS = Float JavaDoc[].class;
96     public static final Class JavaDoc DOUBLE_OBJECT_ARRAY_CLASS = Double JavaDoc[].class;
97     public static final Class JavaDoc STRING_OBJECT_ARRAY_CLASS = String JavaDoc[].class;
98
99     public static final Map JavaDoc COMMON_TYPES = new HashMap JavaDoc(64);
100     static
101     {
102         COMMON_TYPES.put("byte", Byte.TYPE);
103         COMMON_TYPES.put("char", Character.TYPE);
104         COMMON_TYPES.put("double", Double.TYPE);
105         COMMON_TYPES.put("float", Float.TYPE);
106         COMMON_TYPES.put("int", Integer.TYPE);
107         COMMON_TYPES.put("long", Long.TYPE);
108         COMMON_TYPES.put("short", Short.TYPE);
109         COMMON_TYPES.put("boolean", Boolean.TYPE);
110         COMMON_TYPES.put("void", Void.TYPE);
111         COMMON_TYPES.put("java.lang.Object", Object JavaDoc.class);
112         COMMON_TYPES.put("java.lang.Boolean", Boolean JavaDoc.class);
113         COMMON_TYPES.put("java.lang.Byte", Byte JavaDoc.class);
114         COMMON_TYPES.put("java.lang.Character", Character JavaDoc.class);
115         COMMON_TYPES.put("java.lang.Short", Short JavaDoc.class);
116         COMMON_TYPES.put("java.lang.Integer", Integer JavaDoc.class);
117         COMMON_TYPES.put("java.lang.Long", Long JavaDoc.class);
118         COMMON_TYPES.put("java.lang.Float", Float JavaDoc.class);
119         COMMON_TYPES.put("java.lang.Double", Double JavaDoc.class);
120         COMMON_TYPES.put("java.lang.String", String JavaDoc.class);
121         
122         COMMON_TYPES.put("byte[]", BYTE_ARRAY_CLASS);
123         COMMON_TYPES.put("char[]", CHAR_ARRAY_CLASS);
124         COMMON_TYPES.put("double[]", DOUBLE_ARRAY_CLASS);
125         COMMON_TYPES.put("float[]", FLOAT_ARRAY_CLASS);
126         COMMON_TYPES.put("int[]", INT_ARRAY_CLASS);
127         COMMON_TYPES.put("long[]", LONG_ARRAY_CLASS);
128         COMMON_TYPES.put("short[]", SHORT_ARRAY_CLASS);
129         COMMON_TYPES.put("boolean[]", BOOLEAN_ARRAY_CLASS);
130         COMMON_TYPES.put("java.lang.Object[]", OBJECT_ARRAY_CLASS);
131         COMMON_TYPES.put("java.lang.Boolean[]", BOOLEAN_OBJECT_ARRAY_CLASS);
132         COMMON_TYPES.put("java.lang.Byte[]", BYTE_OBJECT_ARRAY_CLASS);
133         COMMON_TYPES.put("java.lang.Character[]", CHARACTER_OBJECT_ARRAY_CLASS);
134         COMMON_TYPES.put("java.lang.Short[]", SHORT_OBJECT_ARRAY_CLASS);
135         COMMON_TYPES.put("java.lang.Integer[]", INTEGER_OBJECT_ARRAY_CLASS);
136         COMMON_TYPES.put("java.lang.Long[]", LONG_OBJECT_ARRAY_CLASS);
137         COMMON_TYPES.put("java.lang.Float[]", FLOAT_OBJECT_ARRAY_CLASS);
138         COMMON_TYPES.put("java.lang.Double[]", DOUBLE_OBJECT_ARRAY_CLASS);
139         COMMON_TYPES.put("java.lang.String[]", STRING_OBJECT_ARRAY_CLASS);
140         // array of void is not a valid type
141
}
142     
143     /** utility class, do not instantiate */
144     private ClassUtils()
145     {
146         // utility class, disable instantiation
147
}
148
149     //~ Methods ------------------------------------------------------------------------------------
150

151     /**
152      * Tries a Class.forName with the context class loader of the current thread first and
153      * automatically falls back to the ClassUtils class loader (i.e. the loader of the
154      * myfaces.jar lib) if necessary.
155      *
156      * @param type fully qualified name of a non-primitive non-array class
157      * @return the corresponding Class
158      * @throws NullPointerException if type is null
159      * @throws ClassNotFoundException
160      */

161     public static Class JavaDoc classForName(String JavaDoc type)
162         throws ClassNotFoundException JavaDoc
163     {
164         if (type == null) throw new NullPointerException JavaDoc("type");
165         try
166         {
167             // Try WebApp ClassLoader first
168
return Class.forName(type,
169                                  false, // do not initialize for faster startup
170
Thread.currentThread().getContextClassLoader());
171         }
172         catch (ClassNotFoundException JavaDoc ignore)
173         {
174             // fallback: Try ClassLoader for ClassUtils (i.e. the myfaces.jar lib)
175
return Class.forName(type,
176                                  false, // do not initialize for faster startup
177
ClassUtils.class.getClassLoader());
178         }
179     }
180
181
182     /**
183      * Same as {@link #classForName(String)}, but throws a RuntimeException
184      * (FacesException) instead of a ClassNotFoundException.
185      *
186      * @return the corresponding Class
187      * @throws NullPointerException if type is null
188      * @throws FacesException if class not found
189      */

190     public static Class JavaDoc simpleClassForName(String JavaDoc type)
191     {
192         try
193         {
194             return classForName(type);
195         }
196         catch (ClassNotFoundException JavaDoc e)
197         {
198             log.error("Class " + type + " not found", e);
199             throw new FacesException(e);
200         }
201     }
202
203
204     /**
205      * Similar as {@link #classForName(String)}, but also supports primitive types
206      * and arrays as specified for the JavaType element in the JavaServer Faces Config DTD.
207      *
208      * @param type fully qualified class name or name of a primitive type, both optionally
209      * followed by "[]" to indicate an array type
210      * @return the corresponding Class
211      * @throws NullPointerException if type is null
212      * @throws ClassNotFoundException
213      */

214     public static Class JavaDoc javaTypeToClass(String JavaDoc type)
215         throws ClassNotFoundException JavaDoc
216     {
217         if (type == null) throw new NullPointerException JavaDoc("type");
218
219         // try common types and arrays of common types first
220
Class JavaDoc clazz = (Class JavaDoc) COMMON_TYPES.get(type);
221         if (clazz != null)
222         {
223             return clazz;
224         }
225
226         int len = type.length();
227         if (len > 2 && type.charAt(len - 1) == ']' && type.charAt(len - 2) == '[')
228         {
229             String JavaDoc componentType = type.substring(0, len - 2);
230             Class JavaDoc componentTypeClass = classForName(componentType);
231             return Array.newInstance(componentTypeClass, 0).getClass();
232         }
233         else
234         {
235             return classForName(type);
236         }
237     }
238
239
240     /**
241      * Same as {@link #javaTypeToClass(String)}, but throws a RuntimeException
242      * (FacesException) instead of a ClassNotFoundException.
243      *
244      * @return the corresponding Class
245      * @throws NullPointerException if type is null
246      * @throws FacesException if class not found
247      */

248     public static Class JavaDoc simpleJavaTypeToClass(String JavaDoc type)
249     {
250         try
251         {
252             return javaTypeToClass(type);
253         }
254         catch (ClassNotFoundException JavaDoc e)
255         {
256             log.error("Class " + type + " not found", e);
257             throw new FacesException(e);
258         }
259     }
260
261     public static InputStream JavaDoc getResourceAsStream(String JavaDoc resource)
262     {
263         InputStream JavaDoc stream = Thread.currentThread().getContextClassLoader()
264                                 .getResourceAsStream(resource);
265         if (stream == null)
266         {
267             // fallback
268
stream = ClassUtils.class.getClassLoader().getResourceAsStream(resource);
269         }
270         return stream;
271     }
272
273
274     public static Object JavaDoc newInstance(String JavaDoc type)
275         throws FacesException
276     {
277         if (type == null) return null;
278         return newInstance(simpleClassForName(type));
279     }
280
281
282     public static Object JavaDoc newInstance(Class JavaDoc clazz)
283         throws FacesException
284     {
285         try
286         {
287             return clazz.newInstance();
288         }
289         catch(NoClassDefFoundError JavaDoc e)
290         {
291             log.error("Class : "+clazz.getName()+" not found.",e);
292             throw new FacesException(e);
293         }
294         catch (InstantiationException JavaDoc e)
295         {
296             log.error(e.getMessage(), e);
297             throw new FacesException(e);
298         }
299         catch (IllegalAccessException JavaDoc e)
300         {
301             log.error(e.getMessage(), e);
302             throw new FacesException(e);
303         }
304     }
305
306     public static Object JavaDoc convertToType(Object JavaDoc value, Class JavaDoc desiredClass)
307     {
308         if (value == null) return null;
309
310         try
311         {
312             // Use coersion implemented by JSP EL for consistency with EL
313
// expressions. Additionally, it caches some of the coersions.
314
return Coercions.coerce(value, desiredClass, COERCION_LOGGER);
315         }
316         catch (ELException JavaDoc e)
317         {
318             String JavaDoc message = "Cannot coerce " + value.getClass().getName()
319                 + " to " + desiredClass.getName();
320             log.error(message, e);
321             throw new FacesException(message, e);
322         }
323     }
324 }
325
Popular Tags