View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.composite.util;
17  
18  import java.lang.reflect.Array;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /***
25   * @author Matt Sgarlata
26   * @since Mar 11, 2005
27   */
28  public abstract class ClassUtils {
29  
30  	private static final Map classPresentCache = Collections.synchronizedMap(new HashMap());
31  
32  	/***
33  	 * Returns the type of object that makes up the given array class (for
34  	 * example, converts Long[] to Long)
35  	 */
36  	public static Class getContainedClass(Class arrayClass) {
37  		return arrayClass.getComponentType();
38  	}
39  
40  	/***
41  	 * Tests whether the given className can be found by the classloader that
42  	 * loaded this class.
43  	 * 
44  	 * @param className
45  	 *            the class for which we are searching
46  	 * @return <code>true</code> if the class indicated by
47  	 *         <code>className</code> is present or <br>
48  	 *         <code>false</code>, otherwise
49  	 */
50  	public static boolean isClassPresent(String className) {
51  		Boolean classPresent = (Boolean) classPresentCache.get(className);
52  		if (classPresent == null) {
53  			try {
54  				Class.forName(className);
55  				classPresent = Boolean.TRUE;
56  			}
57  			catch (ClassNotFoundException e) {
58  				classPresent = Boolean.FALSE;
59  			}
60  			classPresentCache.put(className, classPresent);
61  		}
62  		return classPresent.booleanValue();
63  	}
64  
65  	/***
66  	 * Indicates whether we are running under JDK 1.4 or higher.
67  	 * 
68  	 * @return <code>true</code> if we are running in JDK 1.4 or higher or <br>
69  	 *         <code>false</code> otherwise
70  	 */
71  	public static boolean isJdk14OrHigherPresent() {
72  		return isClassPresent("java.lang.CharSequence");
73  	}
74  
75  	/***
76  	 * Creates a new array with the specified component type and length.
77  	 * Equivalent to calling {@link Array#newInstance(java.lang.Class, int)}.
78  	 * 
79  	 * @param componentType
80  	 *            the <code>Class</code> object representing the most general
81  	 *            type of object which may be contained in the new array
82  	 * @param length
83  	 *            the length of the new array
84  	 * @return the new array
85  	 * @exception NullPointerException
86  	 *                if <code>componentType</code> is <code>null</code>
87  	 * @exception IllegalArgumentException
88  	 *                if <code>componentType</code> is Void.TYPE
89  	 * @exception NegativeArraySizeException
90  	 *                if <code>length</code> is negative
91  	 */
92  	public static Object createArray(Class componentType, int length) {
93  		return Array.newInstance(componentType, length);
94  	}
95  
96  	/***
97  	 * Returns a class' name without a package prefix. For example,
98  	 * <code>ClassUtils.getUnqualifiedClassName(ClassUtils.class)</code> would
99  	 * return <code>ClassUtils</code>.
100 	 * 
101 	 * @param clazz
102 	 *            the class whose name we are to return
103 	 * @return the class' name
104 	 * @throws NullPointerException
105 	 *             if <code>clazz</code> is <code>null</code>.
106 	 */
107 	public static String getUnqualifiedClassName(Class clazz) {
108 		return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
109 	}
110 
111 	/***
112 	 * Return all interfaces implemented by the specified class.
113 	 * @param clazz a Class.
114 	 * @return Class[]
115 	 */
116 	public static Class[] getInterfaces(Class clazz) {
117 		//preserving order so we manually check contains() instead of using a Set:
118 		ArrayList result = new ArrayList();
119 		Class currentClass = clazz;
120 		while (currentClass != null) {
121 			//try to add in order declared from earliest ancestor down:
122 			Class[] currentInterfaces = currentClass.getInterfaces();
123 			for (int i = 0; i < currentInterfaces.length; i++) {
124 				int idx = result.indexOf(currentInterfaces[i]);
125 				if (idx >= 0) {
126 					if (idx == i) {
127 						continue;
128 					}
129 					result.remove(idx);
130 				}
131 				result.add(i, currentInterfaces[i]);
132 			}
133 			currentClass = currentClass.getSuperclass();
134 		}
135 		return (Class[]) result.toArray(new Class[result.size()]);
136 	}
137 }