public class ClassUtils extends Object
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.
Modifier and Type | Field and Description |
---|---|
static Object[] |
NO_ARGS |
static Class<?>[] |
NO_ARGS_TYPE |
Modifier and Type | Method and Description |
---|---|
static boolean |
compare(Class[] c1,
Class[] c2,
boolean matchOnObject,
boolean acceptNulls)
Returns true if the types from array c2 are assignable to the types from c1 and the arrays are the same size.
|
static boolean |
equal(Object a,
Object b)
Simple helper for writing object equalities.
|
static Class<?>[] |
findImplementedInterfaces(Class<?> aClass)
Returns the list of interfaces implemented in a given class.
|
static String |
getClassName(Class clazz) |
static URL |
getClassPathRoot(Class clazz) |
static Class<?>[] |
getClassTypes(Object object)
Used for creating an array of class types for an array or single object
|
static Constructor |
getConstructor(Class clazz,
Class[] paramTypes) |
static Constructor |
getConstructor(Class clazz,
Class[] paramTypes,
boolean exactMatch)
Returns available constructor in the target class that as the parameters specified.
|
static <T> T |
getFieldValue(Object target,
String fieldName,
boolean recursive) |
static Method |
getMethod(Class<?> clazz,
String name,
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 Method |
getMethod(Class clazz,
String name,
Class[] parameterTypes,
boolean acceptNulls) |
static Class<?>[] |
getParameterTypes(Object bean,
String methodName) |
static URL |
getResource(String resourceName,
Class<?> callingClass)
Load a given resource.
|
static Enumeration<URL> |
getResources(String resourceName,
Class<?> callingClass)
Deprecated.
|
static Enumeration<URL> |
getResources(String resourceName,
ClassLoader fallbackClassLoader)
Find resources with a given name.
|
static String |
getSimpleName(Class clazz)
Provide a simple-to-understand class name (with access to only Java 1.4 API).
|
static int |
hash(Object[] state) |
static Class<?> |
initializeClass(Class<?> clazz)
Ensure that the given class is properly initialized when the argument is passed in as .class literal.
|
static <T> T |
instantiateClass(Class<? extends T> clazz,
Object... constructorArgs) |
static Object |
instantiateClass(String name,
Object... constructorArgs) |
static Object |
instantiateClass(String name,
Object[] constructorArgs,
Class<?> callingClass) |
static Object |
instantiateClass(String name,
Object[] constructorArgs,
ClassLoader classLoader) |
static boolean |
isClassOnPath(String className,
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(Class<?> clazz) |
static <T> boolean |
isInstance(Class<T> type,
Object value)
Checks that
value is an instance of type . |
static Class |
loadClass(String className,
Class<?> callingClass)
Load a class with a given name.
|
static <T extends Class> |
loadClass(String className,
Class<?> callingClass,
T type)
Load a class with a given name.
|
static Class |
loadClass(String className,
ClassLoader classLoader)
Load a class with a given name from the given classloader.
|
static <I,O> Function<I,O> |
memoize(Function<I,O> f,
Map<I,O> cache)
Wraps the given function
f so that results for a given input are cached in the given Map. |
static Class<? extends Annotation> |
resolveAnnotationClass(Annotation annotation) |
static void |
setFieldValue(Object target,
String fieldName,
Object value,
boolean recursive) |
static <T> T |
withContextClassLoader(ClassLoader classLoader,
Callable<T> callable)
Executes the given
callable using the given classLoader as the current Thread 's context classloader. |
static <T,E extends Exception> |
withContextClassLoader(ClassLoader classLoader,
Callable<T> callable,
Class<E> expectedExceptionType,
ExceptionHandler<T,E> exceptionHandler)
Executes the given
callable using the given classLoader as the current Thread 's context classloader. |
static void |
withContextClassLoader(ClassLoader classLoader,
Runnable runnable)
Executes the given
runnable using the given classLoader as the current Thread 's context classloader. |
static Class[] |
wrappersToPrimitives(Class[] wrappers) |
public static final Object[] NO_ARGS
public static final Class<?>[] NO_ARGS_TYPE
public static boolean isConcrete(Class<?> clazz)
public static URL getResource(String resourceName, Class<?> callingClass)
resourceName
- The name of the resource to loadcallingClass
- The Class object of the calling object@Deprecated public static Enumeration<URL> getResources(String resourceName, Class<?> callingClass)
public static Enumeration<URL> getResources(String resourceName, ClassLoader fallbackClassLoader)
ClassUtils
s class classLoaderresourceName
- resource being searched. Non empty.fallbackClassLoader
- classloader used to fallback the search. Non null.Enumeration
containing the found resources. Can be empty.public static Class loadClass(String className, Class<?> callingClass) throws ClassNotFoundException
Thread.currentThread().getContextClassLoader()
Class.forName(java.lang.String)
ClassLoaderUtil.class.getClassLoader()
callingClass.getClassLoader()
className
- The name of the class to loadcallingClass
- The Class object of the calling objectClassNotFoundException
- If the class cannot be found anywhere.public static <T extends Class> T loadClass(String className, Class<?> callingClass, T type) throws ClassNotFoundException
Thread.currentThread().getContextClassLoader()
Class.forName(java.lang.String)
ClassLoaderUtil.class.getClassLoader()
callingClass.getClassLoader()
className
- The name of the class to loadcallingClass
- The Class object of the calling objecttype
- the class type to expect to loadClassNotFoundException
- If the class cannot be found anywhere.public static Class loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException
className
- the name of the class to loadclassLoader
- the loader to load it fromClassNotFoundException
- if the class is not available in the class loaderpublic static <T> T getFieldValue(Object target, String fieldName, boolean recursive) throws IllegalAccessException, NoSuchFieldException
public static void setFieldValue(Object target, String fieldName, Object value, boolean recursive) throws IllegalAccessException, NoSuchFieldException
public static Class<?> initializeClass(Class<?> clazz)
clazz
- the Class to be initializedpublic static <T> T instantiateClass(Class<? extends T> clazz, Object... constructorArgs) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
public static Object instantiateClass(String name, Object... constructorArgs) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
public static Object instantiateClass(String name, Object[] constructorArgs, Class<?> callingClass) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
public static Object instantiateClass(String name, Object[] constructorArgs, ClassLoader classLoader) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
public static Method getMethod(Class<?> clazz, String name, Class<?>[] parameterTypes)
clazz
- the class to find the method onname
- the method name to findparameterTypes
- an array of argument types or nullpublic static Method getMethod(Class clazz, String name, Class[] parameterTypes, boolean acceptNulls)
public static Constructor getConstructor(Class clazz, Class[] paramTypes)
public static Constructor getConstructor(Class clazz, Class[] paramTypes, boolean exactMatch)
clazz
- the class to searchparamTypes
- the param types to match againstexactMatch
- should exact types be used (i.e. equals rather than isAssignableFrom.)public static boolean isClassOnPath(String className, Class currentClass)
className
- The class name to look forcurrentClass
- the calling classpublic static Class<?>[] getClassTypes(Object object)
object
- single object or array. If this parameter is null or a zero length array then NO_ARGS_TYPE
is returnedpublic static boolean compare(Class[] c1, Class[] c2, boolean matchOnObject, boolean acceptNulls)
c1
- parameter types arrayc2
- parameter types arraymatchOnObject
- return false if there is a parameter of type Object in c1acceptNulls
- allows null parameter types in c2public static String getSimpleName(Class clazz)
clazz
- The class whose name we will generatepublic static boolean equal(Object a, Object b)
TODO Is there a better place for this?
a
- object to compareb
- object to be compared topublic static int hash(Object[] state)
public static Class<? extends Annotation> resolveAnnotationClass(Annotation annotation)
public static <T> boolean isInstance(Class<T> type, Object value)
value
is an instance of type
.
The value that this method adds over something like Class.isInstance(Object)
is that it also considers the case in
which type
and value
are evaluate by isWrapperAndPrimitivePair(Class, Class)
as true
T
- the generic type of type
type
- the Class
you want to check the value against
value
- an instance you want to verify is instance of type
true
if value
is an instance of type
or if they are a wrapper-primitive pair. false
otherwisepublic static void withContextClassLoader(ClassLoader classLoader, Runnable runnable)
runnable
using the given classLoader
as the current Thread
's context classloader.
This method guarantees that whatever the outcome, the thread's context classloader is set back to the value that it had before executing this method
classLoader
- the context ClassLoader
on which the runnable
should be executedrunnable
- a closurepublic static <T> T withContextClassLoader(ClassLoader classLoader, Callable<T> callable)
callable
using the given classLoader
as the current Thread
's context classloader.
This method guarantees that whatever the outcome, the thread's context classloader is set back to the value that it had before executing this method
classLoader
- the context ClassLoader
on which the runnable
should be executedcallable
- a Callable
callable
producedpublic static <T,E extends Exception> T withContextClassLoader(ClassLoader classLoader, Callable<T> callable, Class<E> expectedExceptionType, ExceptionHandler<T,E> exceptionHandler) throws E extends Exception
callable
using the given classLoader
as the current Thread
's context classloader.
This method guarantees that whatever the outcome, the thread's context classloader is set back to the value that it had before executing this method.
This method also accounts for the possibility of the callable
to throw and exception of type
expectedExceptionType
. If that happens, then the exception is re-thrown. If the callable
throws a
RuntimeException
of a different type, it is also re-thrown. Finally, if an exception of any other type is found, then
it is handled delegating into the exceptionHandler
which might in turn throw another exception of
expectedExceptionType
or return a value
T
- the generic type of the return valueE
- the generic type of the expected exceptionclassLoader
- the context ClassLoader
on which the runnable
should be executedcallable
- a Callable
expectedExceptionType
- the type of exception which is expected to be thrownexceptionHandler
- a ExceptionHandler
in case an unexpected exception is found insteadcallable
or the exceptionHandler
E
- if the expected exception is actually thrownE extends Exception
public static <I,O> Function<I,O> memoize(Function<I,O> f, Map<I,O> cache)
f
so that results for a given input are cached in the given Map.f
- the function to memoizecache
- the map where cached values are storedCopyright © 2018. All rights reserved.