Class
utilities.
*
* @version $Revision: 2787 $
* @author Jason Dillon
* @author Scott Stark
* @author Dimitris Andreadis
*/
@SuppressWarnings("unchecked")
public final class Classes {
/** The string used to separator packages */
public static final String PACKAGE_SEPARATOR = ".";
/** The characther used to separator packages */
public static final char PACKAGE_SEPARATOR_CHAR = '.';
/** The default package name. */
public static final String DEFAULT_PACKAGE_NAME = "
* For example, "boolean" returns Boolean.TYPE and so on...
*
* @param name
* Primitive type name (boolean, int, byte, ...)
* @return Primitive type or null.
*
* @exception IllegalArgumentException
* Type is not a primitive class
*/
public static Class getPrimitiveTypeForName(final String name) {
return (Class) PRIMITIVE_NAME_TYPE_MAP.get(name);
}
/** Map of primitive types to their wrapper classes */
private static final Class[] PRIMITIVE_WRAPPER_MAP = { Boolean.TYPE, Boolean.class, Byte.TYPE,
Byte.class, Character.TYPE, Character.class, Double.TYPE, Double.class, Float.TYPE,
Float.class, Integer.TYPE, Integer.class, Long.TYPE, Long.class, Short.TYPE, Short.class, };
/**
* Get the wrapper class for the given primitive type.
*
* @param type
* Primitive class.
* @return Wrapper class for primitive.
*
* @exception IllegalArgumentException
* Type is not a primitive class
*/
public static Class getPrimitiveWrapper(final Class type) {
if (!type.isPrimitive()) {
throw new IllegalArgumentException("type is not a primitive class");
}
for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) {
if (type.equals(PRIMITIVE_WRAPPER_MAP[i]))
return PRIMITIVE_WRAPPER_MAP[i + 1];
}
// should never get here, if we do then PRIMITIVE_WRAPPER_MAP
// needs to be updated to include the missing mapping
System.out.println("Unreachable Statement Exception");
return null;
}
/**
* Check if the given class is a primitive wrapper class.
*
* @param type
* Class to check.
* @return True if the class is a primitive wrapper.
*/
public static boolean isPrimitiveWrapper(final Class type) {
for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) {
if (type.equals(PRIMITIVE_WRAPPER_MAP[i + 1])) {
return true;
}
}
return false;
}
/**
* Check if the given class is a primitive class or a primitive wrapper class.
*
* @param type
* Class to check.
* @return True if the class is a primitive or primitive wrapper.
*/
public static boolean isPrimitive(final Class type) {
if (type.isPrimitive() || isPrimitiveWrapper(type)) {
return true;
}
return false;
}
/**
* Check type against boolean, byte, char, short, int, long, float, double.
*
* @param type
* The java type name
* @return true if this is a primative type name.
*/
public static boolean isPrimitive(final String type) {
return PRIMITIVE_NAME_TYPE_MAP.containsKey(type);
}
/**
* Instantiate a java class object
*
* @param expected
* the expected class type
* @param property
* the system property defining the class
* @param defaultClassName
* the default class name
* @return the instantiated object
*/
public static Object instantiate(Class expected, String property, String defaultClassName) {
String className = getProperty(property, defaultClassName);
Class clazz = null;
try {
clazz = loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot load class " + className, e);
}
Object result = null;
try {
result = clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Error instantiating " + className, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Error instantiating " + className, e);
}
if (expected.isAssignableFrom(clazz) == false)
throw new RuntimeException("Class " + className + " from classloader "
+ clazz.getClassLoader() + " is not of the expected class " + expected + " loaded from "
+ expected.getClassLoader());
return result;
}
// ///////////////////////////////////////////////////////////////////////
// Class Loading //
// ///////////////////////////////////////////////////////////////////////
/**
* This method acts equivalently to invoking
* Thread.currentThread().getContextClassLoader().loadClass(className);
* but it also supports primitive types and array classes of object types or
* primitive types.
*
* @param className
* the qualified name of the class or the name of primitive type or
* array in the same format as returned by the
* java.lang.Class.getName()
method.
* @return the Class object for the requested className
*
* @throws ClassNotFoundException
* when the classLoader
can not find the requested
* class
*/
public static Class loadClass(String className) throws ClassNotFoundException {
return loadClass(className, Thread.currentThread().getContextClassLoader());
}
/**
* This method acts equivalently to invoking classLoader.loadClass(className)
* but it also supports primitive types and array classes of object types or
* primitive types.
*
* @param className
* the qualified name of the class or the name of primitive type or
* array in the same format as returned by the
* java.lang.Class.getName() method.
* @param classLoader
* the ClassLoader used to load classes
* @return the Class object for the requested className
*
* @throws ClassNotFoundException
* when the classLoader
can not find the requested
* class
*/
public static Class loadClass(String className, ClassLoader classLoader)
throws ClassNotFoundException {
// ClassLoader.loadClass() does not handle primitive types:
//
// B byte
// C char
// D double
// F float
// I int
// J long
// S short
// Z boolean
// V void
//
if (className.length() == 1) {
char type = className.charAt(0);
if (type == 'B')
return Byte.TYPE;
if (type == 'C')
return Character.TYPE;
if (type == 'D')
return Double.TYPE;
if (type == 'F')
return Float.TYPE;
if (type == 'I')
return Integer.TYPE;
if (type == 'J')
return Long.TYPE;
if (type == 'S')
return Short.TYPE;
if (type == 'Z')
return Boolean.TYPE;
if (type == 'V')
return Void.TYPE;
// else throw...
throw new ClassNotFoundException(className);
}
// Check for a primative type
if (isPrimitive(className) == true)
return (Class) Classes.PRIMITIVE_NAME_TYPE_MAP.get(className);
// Check for the internal vm format: Lclassname;
if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';')
return classLoader.loadClass(className.substring(1, className.length() - 1));
// first try - be optimistic
// this will succeed for all non-array classes and array classes that have
// already been resolved
//
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
// if it was non-array class then throw it
if (className.charAt(0) != '[')
throw e;
}
// we are now resolving array class for the first time
// count opening braces
int arrayDimension = 0;
while (className.charAt(arrayDimension) == '[')
arrayDimension++;
// resolve component type - use recursion so that we can resolve primitive
// types also
Class componentType = loadClass(className.substring(arrayDimension), classLoader);
// construct array class
return Array.newInstance(componentType, new int[arrayDimension]).getClass();
}
/**
* Get a system property
*
* @param name
* the property name
* @param defaultValue
* the default value
*/
private static String getProperty(final String name, final String defaultValue) {
return (String) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(name, defaultValue);
}
});
}
}