public final class ReflectionUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
ARRAY_SUFFIX
Suffix for array class names: "[]"
|
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
createObject(Class<T> clazz,
Object... args)
Create an object of the given type using a constructor that matches the
supplied arguments.
|
static <T> T |
createObject(Class<T> clazz,
Object[] args,
Map<String,Object> vars)
Create an object of the given type using a constructor that matches the
supplied arguments and invoke the setters with the supplied variables.
|
static Class<?> |
forName(String name)
Replacement for
Class.forName() that also returns Class
instances for primitives (like "int") and array class names (like
"String[]"). |
static Class<?> |
forName(String name,
ClassLoader classLoader)
Replacement for
Class.forName() that also returns Class
instances for primitives (e.g."int") and array class names (e.g. |
static ClassLoader |
getDefaultClassLoader()
Return the default ClassLoader to use: typically the thread context
ClassLoader, if available; the ClassLoader that loaded the
ReflectionUtils class will be used as fallback.
|
static <T> T |
invokeSetters(T instance,
Map<String,Object> vars)
Invoke the setters for the given variables on the given instance.
|
static boolean |
isAssignable(Class<?> lhsType,
Class<?> rhsType)
Check if the right-hand side type may be assigned to the left-hand side
type, assuming setting by reflection.
|
static boolean |
isAssignableValue(Class<?> type,
Object value)
Determine if the given type is assignable from the given value, assuming
setting by reflection.
|
public static final String ARRAY_SUFFIX
public static <T> T createObject(Class<T> clazz, Object... args) throws NoSuchConstructorException, AmbiguousConstructorException, ReflectiveOperationException
T - the object typeclazz - the type to createargs - the arguments to the constructorNoSuchConstructorException - if there is not a constructor that matches the given
argumentsAmbiguousConstructorException - if there is more than one constructor that matches the given
argumentsReflectiveOperationException - if any of the reflective operations throw an exceptionpublic static <T> T createObject(Class<T> clazz, Object[] args, Map<String,Object> vars) throws NoSuchConstructorException, AmbiguousConstructorException, ReflectiveOperationException
T - the object typeclazz - the type to createargs - the arguments to the constructorvars - the named arguments for settersNoSuchConstructorException - if there is not a constructor that matches the given
argumentsAmbiguousConstructorException - if there is more than one constructor that matches the given
argumentsReflectiveOperationException - if any of the reflective operations throw an exceptionpublic static boolean isAssignable(Class<?> lhsType, Class<?> rhsType)
lhsType - the target typerhsType - the value type that should be assigned to the target typepublic static boolean isAssignableValue(Class<?> type, Object value)
type - the target typevalue - the value that should be assigned to the typepublic static ClassLoader getDefaultClassLoader()
Call this method if you intend to use the thread context ClassLoader in a
scenario where you absolutely need a non-null ClassLoader reference: for
example, for class path resource loading (but not necessarily for
Class.forName, which accepts a null ClassLoader
reference as well).
null)Thread.getContextClassLoader()public static Class<?> forName(String name) throws ClassNotFoundException
Class.forName() that also returns Class
instances for primitives (like "int") and array class names (like
"String[]").
Always uses the default class loader: that is, preferably the thread context class loader, or the ClassLoader that loaded the ReflectionUtils class as fallback.
name - the name of the ClassClassNotFoundException - if the class was not foundClass.forName(String, boolean, ClassLoader),
getDefaultClassLoader()public static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException
Class.forName() that also returns Class
instances for primitives (e.g."int") and array class names (e.g.
"String[]"). Furthermore, it is also capable of resolving inner class
names in Java source style (e.g. "java.lang.Thread.State" instead of
"java.lang.Thread$State").name - the name of the ClassclassLoader - the class loader to use (may be null, which
indicates the default class loader)ClassNotFoundException - if the class was not foundClass.forName(String, boolean, ClassLoader)public static <T> T invokeSetters(T instance,
Map<String,Object> vars)
throws ReflectiveOperationException
T - the instance typeinstance - the instance to inject with the variablesvars - the variables to injectReflectiveOperationException - if there was a problem finding or invoking a setter methodCopyright © 2011-2015. All Rights Reserved.