org.spicefactory.cinnamon.reflect.util
Class ClassUtil

java.lang.Object
  extended by org.spicefactory.cinnamon.reflect.util.ClassUtil

public class ClassUtil
extends Object

Static utility methods for loading classes and reflectively creating new instances.

Author:
Jens Halm

Constructor Summary
ClassUtil()
           
 
Method Summary
static Class<?> loadClass(String className)
          Loads the given Class using the context ClassLoader of the current thread.
static Class<?> loadClass(String className, boolean allowPrimitive, boolean allowArray)
          Loads the given Class using the context ClassLoader of the current thread.
static
<T> Class<? extends T>
loadClass(String className, Class<T> requiredType)
          Loads the given Class if it is of the specified required type.
static
<T> T
newInstance(Class<T> type)
          Creates a new instance of the given Class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtil

public ClassUtil()
Method Detail

loadClass

public static Class<?> loadClass(String className,
                                 boolean allowPrimitive,
                                 boolean allowArray)
                          throws ClassLoadingException
Loads the given Class using the context ClassLoader of the current thread. If the allowPrimitive parameter is true this method also considers Class instances representing primitive types which is otherwise not supported by the underlying ClassLoader.loadClass method. If the allowArray parameter is true this method allows to load Class instances that represent arrays if the className parameter ends with brackets like in java.lang.String[].

Parameters:
className - the name of the class to load
allowPrimitive - whether primitive types should also be considered when resolving the given name
allowArray - whether Class instances representing array should be loaded when the className is appended with []
Returns:
the loaded class
Throws:
ClassLoadingException - if the Class cannot be loaded

loadClass

public static Class<?> loadClass(String className)
                          throws ClassLoadingException
Loads the given Class using the context ClassLoader of the current thread.

Parameters:
className - the name of the class to load
Returns:
the loaded class
Throws:
ClassLoadingException - if the Class cannot be loaded

loadClass

public static <T> Class<? extends T> loadClass(String className,
                                               Class<T> requiredType)
                                    throws ClassLoadingException
Loads the given Class if it is of the specified required type. Uses the context ClassLoader of the current thread.

Parameters:
className - the name of the class to load
requiredType - the Class that the loaded Class must implement or extend
Returns:
the loaded class
Throws:
ClassLoadingException - if the Class cannot be loaded or does not implement or extend the required type

newInstance

public static <T> T newInstance(Class<T> type)
                     throws ObjectInstantiationException
Creates a new instance of the given Class.

Parameters:
type - the Class an instance should be created of
Returns:
a new instance of the given class
Throws:
ObjectInstantiationException - if the instance cannot be created or the Constructor threw an Exception