View Javadoc

1   package org.ocltf.utils;
2   
3   import java.lang.reflect.Field;
4   
5   import org.apache.commons.lang.StringUtils;
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   /***
10   * Contains utilities for dealing with classes.
11   * 
12   * @author Chad Brandon
13   */
14  public class ClassUtils extends org.apache.commons.lang.ClassUtils {
15  	
16  	private static final Log logger = LogFactory.getLog(ClassUtils.class);
17  	
18  	/***
19  	 * Loads and returns the class having the className. Will load
20  	 * but normal classes and the classes representing primatives. 
21  	 * 
22  	 * @param className
23  	 * @return Class
24  	 * @throws ClassNotFoundException if the class can not be found
25  	 * @throws NoSuchFieldException if the className is a primative and its corresponding
26  	 *         type field can not be retrieved.
27  	 * @throws IllegalAccessException if the className is a primative and its corresponding
28  	 *         type field is illegal accessed.
29  	 */
30  	public static Class loadClass(String className) 
31  		throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
32  		String methodName = "loadClass";
33  		ExceptionUtils.checkEmpty(methodName, "className", className);
34  		className = StringUtils.trimToNull(className);
35  		//get rid of any array notation
36  		className = StringUtils.replace(className, "[]", "");
37  		
38  		ClassLoader loader = Thread.currentThread().getContextClassLoader();
39  		Class loadedClass  = null;
40  		//check and see if its a primitive and if so convert it
41  		if (ClassUtils.isPrimitiveType(className)) {
42  			loadedClass = getPrimitiveClass(className, loader);
43  		} else {
44  			loadedClass = loader.loadClass(className);
45  		}
46  		
47  		if (loader == null) {
48  			loader = ClassUtils.class.getClassLoader();
49  			Thread.currentThread().setContextClassLoader(loader);
50  		}
51  		
52  		return loadedClass;
53  	}
54  	
55  	/***
56  	 * <p>Returns the type class name for a Java primitive.</p>
57  	 *
58  	 * @param name a <code>String</code> with the name of the type
59  	 * @param loader the loader to use.
60  	 * @return a <code>String</code> with the name of the
61  	 *         corresponding java.lang wrapper class if
62  	 *         <code>name</code> is a Java primitive type;
63  	 *         <code>false</code> if not
64  	 * @throws NoSuchFieldException
65  	 * @throws IllegalAccessException
66  	 * @throws ClassNotFoundException
67  	 */
68  	protected static Class getPrimitiveClass(String name, ClassLoader loader)
69  		throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
70  		String methodName = "getPrimitiveClass";
71  		ExceptionUtils.checkEmpty(methodName, "name", name);
72  		ExceptionUtils.checkNull(methodName, "loader", loader);
73  		
74  		Class primitiveClass = null;
75  		if (isPrimitiveType(name) && !name.equals("void")) {
76  			String className = null;
77  			if ("char".equals(name)) {
78  				className = "java.lang.Character";
79  			}
80  			if ("int".equals(name)) {
81  				className = "java.lang.Integer";
82  			}
83  
84  			className = "java.lang." + StringUtils.capitalize(name);
85  			
86  			try {
87  				if (StringUtils.isNotEmpty(className)) {
88  					Field field = loader.loadClass(className).getField("TYPE");
89  					primitiveClass = (Class) field.get(null);
90  				}
91  			} catch (Exception ex) {
92  				String errMsg = "Error performing " + methodName;
93  				logger.error(errMsg, ex);
94  			}
95  		}
96  		return primitiveClass;
97  	}
98  	
99  	/***
100 	 * <p>Checks if a given type name is a Java primitive type.</p>
101 	 *
102 	 * @param name a <code>String</code> with the name of the type
103 	 * @return <code>true</code> if <code>name</code> is a Java
104 	 *         primitive type; <code>false</code> if not
105 	 */
106 	protected static boolean isPrimitiveType(String name) {
107 		return (
108 			"void".equals(name)
109 				|| "char".equals(name)
110 				|| "byte".equals(name)
111 				|| "short".equals(name)
112 				|| "int".equals(name)
113 				|| "long".equals(name)
114 				|| "float".equals(name)
115 				|| "double".equals(name)
116 				|| "boolean".equals(name));
117 	}
118 	
119 }