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 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 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, InstantiationException, IllegalAccessException, InvocationTargetException
T - clazz - 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 argumentsInstantiationException - if the class that declares the underlying constructor represents an abstract classIllegalAccessException - if the Constructor object enforces Java language access control and the underlying constructor is inaccessibleInvocationTargetException - if the underlying constructor throws 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)Copyright © 2011-2013. All Rights Reserved.