org.mule.util
Class ClassUtils

java.lang.Object
  extended by org.apache.commons.lang.ClassUtils
      extended by org.mule.util.ClassUtils

public class ClassUtils
extends ClassUtils

Extend the Apache Commons ClassUtils to provide additional functionality.

This class is useful for loading resources and classes in a fault tolerant manner that works across different applications servers. The resource and classloading methods are SecurityManager friendly.


Field Summary
static java.lang.Object[] NO_ARGS
           
static java.lang.Class<?>[] NO_ARGS_TYPE
           
 
Fields inherited from class org.apache.commons.lang.ClassUtils
INNER_CLASS_SEPARATOR, INNER_CLASS_SEPARATOR_CHAR, PACKAGE_SEPARATOR, PACKAGE_SEPARATOR_CHAR
 
Constructor Summary
ClassUtils()
           
 
Method Summary
static void addLibrariesToClasspath(java.util.List urls)
           
static boolean compare(java.lang.Class[] c1, java.lang.Class[] c2, boolean matchOnObject)
           
static boolean equal(java.lang.Object a, java.lang.Object b)
          Simple helper for writing object equalities.
static java.lang.String getClassName(java.lang.Class clazz)
           
static java.net.URL getClassPathRoot(java.lang.Class clazz)
           
static java.lang.Class<?>[] getClassTypes(java.lang.Object object)
          Used for creating an array of class types for an array or single object
static java.lang.reflect.Constructor getConstructor(java.lang.Class clazz, java.lang.Class[] paramTypes)
           
static java.lang.reflect.Constructor getConstructor(java.lang.Class clazz, java.lang.Class[] paramTypes, boolean exactMatch)
          Returns available constructor in the target class that as the parameters specified.
static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>[] parameterTypes)
          Returns a matching method for the given name and parameters on the given class If the parameterTypes arguments is null it will return the first matching method on the class.
static java.lang.Class<?>[] getParameterTypes(java.lang.Object bean, java.lang.String methodName)
           
static java.net.URL getResource(java.lang.String resourceName, java.lang.Class<?> callingClass)
          Load a given resource.
static java.util.Enumeration<java.net.URL> getResources(java.lang.String resourceName, java.lang.Class<?> callingClass)
           
static java.util.List<java.lang.reflect.Method> getSatisfiableMethods(java.lang.Class<?> implementation, java.lang.Class<?>[] parameterTypes, boolean voidOk, boolean matchOnObject, java.util.Collection<java.lang.String> ignoredMethodNames, WildcardFilter filter)
          A helper method that will find all matching methods on a class with the given parameter type
static java.util.List<java.lang.reflect.Method> getSatisfiableMethods(java.lang.Class<?> implementation, java.lang.Class<?>[] parameterTypes, boolean voidOk, boolean matchOnObject, java.util.Set<java.lang.String> ignoredMethodNames)
          A helper method that will find all matching methods on a class with the given parameter type
static java.util.List<java.lang.reflect.Method> getSatisfiableMethodsWithReturnType(java.lang.Class implementation, java.lang.Class returnType, boolean matchOnObject, java.util.Set<java.lang.String> ignoredMethodNames)
          Match all method son a class with a defined return type
static java.lang.String getSimpleName(java.lang.Class clazz)
          Provide a simple-to-understand class name (with access to only Java 1.4 API).
static int hash(java.lang.Object[] state)
           
static java.lang.Class<?> initializeClass(java.lang.Class<?> clazz)
          Ensure that the given class is properly initialized when the argument is passed in as .class literal.
static
<T> T
instanciateClass(java.lang.Class<? extends T> clazz, java.lang.Object... constructorArgs)
           
static java.lang.Object instanciateClass(java.lang.String name, java.lang.Object... constructorArgs)
           
static java.lang.Object instanciateClass(java.lang.String name, java.lang.Object[] constructorArgs, java.lang.Class<?> callingClass)
           
static java.lang.Object instanciateClass(java.lang.String name, java.lang.Object[] constructorArgs, java.lang.ClassLoader classLoader)
           
static boolean isClassOnPath(java.lang.String className, java.lang.Class currentClass)
          Can be used by serice endpoints to select which service to use based on what's loaded on the classpath
static boolean isConcrete(java.lang.Class<?> clazz)
           
static java.lang.Class loadClass(java.lang.String className, java.lang.Class<?> callingClass)
          Load a class with a given name.
static
<T extends java.lang.Class>
T
loadClass(java.lang.String className, java.lang.Class<?> callingClass, T type)
          Load a class with a given name.
static java.lang.Class loadClass(java.lang.String className, java.lang.ClassLoader classLoader)
          Load a class with a given name from the given classloader.
static java.lang.Class[] wrappersToPrimitives(java.lang.Class[] wrappers)
           
static java.lang.Class wrapperToPrimitive(java.lang.Class wrapper)
           
 
Methods inherited from class org.apache.commons.lang.ClassUtils
convertClassesToClassNames, convertClassNamesToClasses, getAllInterfaces, getAllSuperclasses, getClass, getClass, getClass, getClass, getPackageCanonicalName, getPackageCanonicalName, getPackageCanonicalName, getPackageName, getPackageName, getPackageName, getPublicMethod, getShortCanonicalName, getShortCanonicalName, getShortCanonicalName, getShortClassName, getShortClassName, getShortClassName, isAssignable, isAssignable, isInnerClass, primitivesToWrappers, primitiveToWrapper, toClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_ARGS

public static final java.lang.Object[] NO_ARGS

NO_ARGS_TYPE

public static final java.lang.Class<?>[] NO_ARGS_TYPE
Constructor Detail

ClassUtils

public ClassUtils()
Method Detail

isConcrete

public static boolean isConcrete(java.lang.Class<?> clazz)

getResource

public static java.net.URL getResource(java.lang.String resourceName,
                                       java.lang.Class<?> callingClass)
Load a given resource.

This method will try to load the resource using the following methods (in order):

Parameters:
resourceName - The name of the resource to load
callingClass - The Class object of the calling object
Returns:
A URL pointing to the resource to load or null if the resource is not found

getResources

public static java.util.Enumeration<java.net.URL> getResources(java.lang.String resourceName,
                                                               java.lang.Class<?> callingClass)

loadClass

public static java.lang.Class loadClass(java.lang.String className,
                                        java.lang.Class<?> callingClass)
                                 throws java.lang.ClassNotFoundException
Load a class with a given name.

It will try to load the class in the following order:

Parameters:
className - The name of the class to load
callingClass - The Class object of the calling object
Returns:
The Class instance
Throws:
java.lang.ClassNotFoundException - If the class cannot be found anywhere.

loadClass

public static <T extends java.lang.Class> T loadClass(java.lang.String className,
                                                      java.lang.Class<?> callingClass,
                                                      T type)
                                           throws java.lang.ClassNotFoundException
Load a class with a given name.

It will try to load the class in the following order:

Parameters:
className - The name of the class to load
callingClass - The Class object of the calling object
type - the class type to expect to load
Returns:
The Class instance
Throws:
java.lang.ClassNotFoundException - If the class cannot be found anywhere.

loadClass

public static java.lang.Class loadClass(java.lang.String className,
                                        java.lang.ClassLoader classLoader)
                                 throws java.lang.ClassNotFoundException
Load a class with a given name from the given classloader.

Parameters:
className - the name of the class to load
classLoader - the loader to load it from
Returns:
the instance of the class
Throws:
java.lang.ClassNotFoundException - if the class is not available in the class loader

initializeClass

public static java.lang.Class<?> initializeClass(java.lang.Class<?> clazz)
Ensure that the given class is properly initialized when the argument is passed in as .class literal. This method can never fail unless the bytecode is corrupted or the VM is otherwise seriously confused.

Parameters:
clazz - the Class to be initialized
Returns:
the same class but initialized

instanciateClass

public static <T> T instanciateClass(java.lang.Class<? extends T> clazz,
                                     java.lang.Object... constructorArgs)
                          throws java.lang.SecurityException,
                                 java.lang.NoSuchMethodException,
                                 java.lang.IllegalArgumentException,
                                 java.lang.InstantiationException,
                                 java.lang.IllegalAccessException,
                                 java.lang.reflect.InvocationTargetException
Throws:
java.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.IllegalArgumentException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

instanciateClass

public static java.lang.Object instanciateClass(java.lang.String name,
                                                java.lang.Object... constructorArgs)
                                         throws java.lang.ClassNotFoundException,
                                                java.lang.SecurityException,
                                                java.lang.NoSuchMethodException,
                                                java.lang.IllegalArgumentException,
                                                java.lang.InstantiationException,
                                                java.lang.IllegalAccessException,
                                                java.lang.reflect.InvocationTargetException
Throws:
java.lang.ClassNotFoundException
java.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.IllegalArgumentException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

instanciateClass

public static java.lang.Object instanciateClass(java.lang.String name,
                                                java.lang.Object[] constructorArgs,
                                                java.lang.Class<?> callingClass)
                                         throws java.lang.ClassNotFoundException,
                                                java.lang.SecurityException,
                                                java.lang.NoSuchMethodException,
                                                java.lang.IllegalArgumentException,
                                                java.lang.InstantiationException,
                                                java.lang.IllegalAccessException,
                                                java.lang.reflect.InvocationTargetException
Throws:
java.lang.ClassNotFoundException
java.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.IllegalArgumentException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

instanciateClass

public static java.lang.Object instanciateClass(java.lang.String name,
                                                java.lang.Object[] constructorArgs,
                                                java.lang.ClassLoader classLoader)
                                         throws java.lang.ClassNotFoundException,
                                                java.lang.SecurityException,
                                                java.lang.NoSuchMethodException,
                                                java.lang.IllegalArgumentException,
                                                java.lang.InstantiationException,
                                                java.lang.IllegalAccessException,
                                                java.lang.reflect.InvocationTargetException
Throws:
java.lang.ClassNotFoundException
java.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.IllegalArgumentException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

getParameterTypes

public static java.lang.Class<?>[] getParameterTypes(java.lang.Object bean,
                                                     java.lang.String methodName)

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz,
                                                 java.lang.String name,
                                                 java.lang.Class<?>[] parameterTypes)
Returns a matching method for the given name and parameters on the given class If the parameterTypes arguments is null it will return the first matching method on the class.

Parameters:
clazz - the class to find the method on
name - the method name to find
parameterTypes - an array of argument types or null
Returns:
the Method object or null if none was found

getConstructor

public static java.lang.reflect.Constructor getConstructor(java.lang.Class clazz,
                                                           java.lang.Class[] paramTypes)

getConstructor

public static java.lang.reflect.Constructor getConstructor(java.lang.Class clazz,
                                                           java.lang.Class[] paramTypes,
                                                           boolean exactMatch)
Returns available constructor in the target class that as the parameters specified.

Parameters:
clazz - the class to search
paramTypes - the param types to match against
exactMatch - should exact types be used (i.e. equals rather than isAssignableFrom.)
Returns:
The matching constructor or null if no matching constructor is found

getSatisfiableMethods

public static java.util.List<java.lang.reflect.Method> getSatisfiableMethods(java.lang.Class<?> implementation,
                                                                             java.lang.Class<?>[] parameterTypes,
                                                                             boolean voidOk,
                                                                             boolean matchOnObject,
                                                                             java.util.Set<java.lang.String> ignoredMethodNames)
A helper method that will find all matching methods on a class with the given parameter type

Parameters:
implementation - the class to build methods on
parameterTypes - the argument param types to look for
voidOk - whether void methods shouldbe included in the found list
matchOnObject - determines whether parameters of Object type are matched when they are of Object.class type
ignoredMethodNames - a Set of method names to ignore. Often 'equals' is not a desired match. This argument can be null.
Returns:
a List of methods on the class that match the criteria. If there are none, an empty list is returned

getSatisfiableMethods

public static java.util.List<java.lang.reflect.Method> getSatisfiableMethods(java.lang.Class<?> implementation,
                                                                             java.lang.Class<?>[] parameterTypes,
                                                                             boolean voidOk,
                                                                             boolean matchOnObject,
                                                                             java.util.Collection<java.lang.String> ignoredMethodNames,
                                                                             WildcardFilter filter)
A helper method that will find all matching methods on a class with the given parameter type

Parameters:
implementation - the class to build methods on
parameterTypes - the argument param types to look for
voidOk - whether void methods shouldbe included in the found list
matchOnObject - determines whether parameters of Object type are matched when they are of Object.class type
ignoredMethodNames - a Set of method names to ignore. Often 'equals' is not a desired match. This argument can be null.
filter - Wildcard expression filter that allows methods to be matched using wildcards i.e. 'get*'
Returns:
a List of methods on the class that match the criteria. If there are none, an empty list is returned

getSatisfiableMethodsWithReturnType

public static java.util.List<java.lang.reflect.Method> getSatisfiableMethodsWithReturnType(java.lang.Class implementation,
                                                                                           java.lang.Class returnType,
                                                                                           boolean matchOnObject,
                                                                                           java.util.Set<java.lang.String> ignoredMethodNames)
Match all method son a class with a defined return type

Parameters:
implementation - the class to search
returnType - the return type to match
matchOnObject - whether Object methods should be matched
ignoredMethodNames - a set of method names to ignore
Returns:
the list of methods that matched the return type and criteria. If none are found an empty result is returned

isClassOnPath

public static boolean isClassOnPath(java.lang.String className,
                                    java.lang.Class currentClass)
Can be used by serice endpoints to select which service to use based on what's loaded on the classpath

Parameters:
className - The class name to look for
currentClass - the calling class
Returns:
true if the class is on the path

getClassTypes

public static java.lang.Class<?>[] getClassTypes(java.lang.Object object)
Used for creating an array of class types for an array or single object

Parameters:
object - single object or array. If this parameter is null or a zero length array then NO_ARGS_TYPE is returned
Returns:
an array of class types for the object

getClassName

public static java.lang.String getClassName(java.lang.Class clazz)

compare

public static boolean compare(java.lang.Class[] c1,
                              java.lang.Class[] c2,
                              boolean matchOnObject)

wrapperToPrimitive

public static java.lang.Class wrapperToPrimitive(java.lang.Class wrapper)

wrappersToPrimitives

public static java.lang.Class[] wrappersToPrimitives(java.lang.Class[] wrappers)

getSimpleName

public static java.lang.String getSimpleName(java.lang.Class clazz)
Provide a simple-to-understand class name (with access to only Java 1.4 API).

Parameters:
clazz - The class whose name we will generate
Returns:
A readable name for the class

equal

public static boolean equal(java.lang.Object a,
                            java.lang.Object b)
Simple helper for writing object equalities. TODO Is there a better place for this?

Parameters:
a - object to compare
b - object to be compared to
Returns:
true if the objects are equal (value or reference), false otherwise

hash

public static int hash(java.lang.Object[] state)

addLibrariesToClasspath

public static void addLibrariesToClasspath(java.util.List urls)
                                    throws java.lang.NoSuchMethodException,
                                           java.lang.IllegalAccessException,
                                           java.lang.reflect.InvocationTargetException
Throws:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException

getClassPathRoot

public static java.net.URL getClassPathRoot(java.lang.Class clazz)


Copyright © 2003-2010 MuleSoft, Inc.. All Rights Reserved.