public class ReflectionUtils extends java.lang.Object
FormalParameter
.Constructor and Description |
---|
ReflectionUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Iterable<?> |
asIterable(java.lang.Object arrayOrIterable)
Unified interface to iterable types (i.e.
|
static <T> T |
convertString(java.lang.String value,
java.lang.Class<T> targetType)
Convert a String to an Object of the provided type.
|
static java.lang.Object |
convertTo(java.lang.String valueString,
java.lang.String typeName)
Convert String to object of specified type
|
static java.lang.Class<?> |
determineCommonBase(java.lang.Class<?> class1,
java.lang.Class<?> class2)
Determines the most specific common base class.
|
static java.util.List<java.lang.reflect.Field> |
getAllFields(java.lang.Class<?> type)
Returns all fields declared for a class (all visibilities and also inherited
from super classes).
|
static java.util.Set<java.lang.Class<?>> |
getAllInterfaces(java.lang.Class<?> baseClass)
Returns the set of all interfaces implemented by the given class.
|
static java.util.List<java.lang.reflect.Method> |
getAllMethods(java.lang.Class<?> type)
Returns all methods declared for a class (all visibilities and also inherited
from super classes).
|
static <T extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.Class<?> element,
java.lang.Class<T> annotationClass)
Returns the annotation the given element is annotated with.
|
static java.util.List<java.lang.Class<?>> |
getBaseHierarchy(java.lang.Class<?> baseClass)
Returns the hierarchy of base classes starting with
Object up to the
class itself. |
static java.util.HashMap<java.lang.String,Version> |
getClassFileVersions(java.io.File jarFile)
This method extracts the class file version from each class file in the
provided jar.
|
static java.lang.reflect.Field |
getField(java.lang.Class<?> type,
java.lang.String name)
Returns the field with the given name from the given type or null if none
exists.
|
static FormalParameter[] |
getFormalParameters(java.lang.reflect.Method method)
Obtain array of formal parameters for a method.
|
static java.util.List<java.lang.Class<?>> |
getSuperClasses(java.lang.Class<?> clazz)
Get super class list of a class.
|
static java.lang.Object |
invoke(java.lang.reflect.Method method,
java.lang.Object object,
java.util.Map<FormalParameter,java.lang.Object> parameterMap)
Invoke a method with parameters.
|
static boolean |
isAssignable(java.lang.Class<?> source,
java.lang.Class<?> target)
Check whether an Object of the source type can be used instead of an Object
of the target type.
|
static boolean |
isConvertibleFromString(java.lang.Class<?> targetType)
This method checks if the provided type can be converted from a string.
|
static boolean |
isInstanceOfAll(java.lang.Object o,
java.lang.Class<?>... classes)
Returns whether the given object is an instance of all of the given classes.
|
static boolean |
isInstanceOfAny(java.lang.Object o,
java.lang.Class<?>... classes)
Returns whether the given object is an instance of at least one of the given
classes.
|
static <T> boolean |
isIterable(T parsedObject)
Returns true when the given object is an array or implements
Iterable , false otherwise. |
static <T> java.util.List<T> |
listInstances(java.util.List<?> objects,
java.lang.Class<T> type)
Creates a list that contains only the types that are instances of a specified
type from the objects of an input list.
|
static Version |
obtainClassFileVersion(java.io.InputStream inputStream)
Obtains the version of a Java class file.
|
static java.lang.Class<?> |
obtainGenericMethodReturnType(java.lang.Class<?> clazz,
java.lang.String methodName)
Obtain the generic return type of method.
|
static java.lang.Class<?> |
obtainMethodReturnType(java.lang.Class<?> clazz,
java.lang.String methodName)
Obtain the return type of method.
|
static <T> T |
performNearestClassLookup(java.lang.Class<?> clazz,
java.util.Map<java.lang.Class<?>,T> classMap)
Returns the value from the map, whose key is the best match for the given
class.
|
static <T> T |
pickInstanceOf(java.lang.Class<T> clazz,
java.util.Collection<?> objects)
Returns the first object in the given collection which is an instance of the
given class (or null otherwise).
|
static java.lang.Class<?> |
resolvePrimitiveClass(java.lang.Class<?> clazz)
Returns the wrapper class type for a primitive type (e.g.
|
static java.lang.Class<?> |
resolveType(java.lang.String typeName)
Resolves the class object for a type name.
|
static java.lang.Class<?> |
resolveType(java.lang.String typeName,
java.lang.ClassLoader classLoader)
Resolves the class object for a type name.
|
static void |
setFieldValue(java.lang.Object object,
java.lang.String fieldName,
java.lang.Object newValue)
Sets the value of a field on the given object.
|
public ReflectionUtils()
public static <T> T convertString(java.lang.String value, java.lang.Class<T> targetType) throws TypeConversionException
EnumUtils.valueOfIgnoreCase(Class, String)
method. Otherwise it is
checked if the target type has a constructor that takes a single string and
it is invoked. For all other cases an exception is thrown, as no conversion
is possible.
Maintainer note: Make sure this method is changed in accordance with
method isConvertibleFromString(Class)
value
- the string to be converted.targetType
- the type of the resulting object.TypeConversionException
- in the case that no conversion could be performed.convertString(String, Class)
,
isConvertibleFromString(Class)
public static java.lang.Object convertTo(java.lang.String valueString, java.lang.String typeName) throws TypeConversionException, java.lang.ClassNotFoundException
TypeConversionException
java.lang.ClassNotFoundException
public static boolean isConvertibleFromString(java.lang.Class<?> targetType)
convertString(String, Class)
the semantics are the
following: If this method returns true
a particular string
may be convertible to the target type. If this method returns
false
, a call to convertString(String, Class)
is
guaranteed to result in a TypeConversionException
. If a call to
convertString(String, Class)
does not result in an exception, a call
to this method is guaranteed to return true
.
Maintainer note: Make sure this method is change in accordance with
method convertString(String, Class)
convertString(String, Class)
public static FormalParameter[] getFormalParameters(java.lang.reflect.Method method)
FormalParameter
public static java.util.List<java.lang.Class<?>> getSuperClasses(java.lang.Class<?> clazz)
clazz
- the class to start traversal fromObject
, primitives and interfaces this returns an empty
list. Object
. Enum
and
Object
public static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object object, java.util.Map<FormalParameter,java.lang.Object> parameterMap) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
method
- the method to invokeobject
- the object the underlying method is invoked fromparameterMap
- this maps from the formal parameter of the method to the parameter
valuejava.lang.IllegalArgumentException
- if the method is an instance method and the specified object
argument is not an instance of the class or interface declaring
the underlying method (or of a subclass or implementor thereof);
if the number of actual and formal parameters differ; if an
unwrapping conversion for primitive arguments fails; or if, after
possible unwrapping, a parameter value cannot be converted to the
corresponding formal parameter type by a method invocation
conversion; if formal parameters belong to different methods.java.lang.IllegalAccessException
- if this Method object enforces Java language access control and
the underlying method is inaccessible.java.lang.reflect.InvocationTargetException
- if the underlying method throws an exception.java.lang.NullPointerException
- if the specified object is null and the method is an instance
method.java.lang.ExceptionInInitializerError
- if the initialization provoked by this method fails.public static boolean isAssignable(java.lang.Class<?> source, java.lang.Class<?> target)
Class.isAssignableFrom(java.lang.Class)
does not handle primitive
types.source
- type of the source objecttarget
- type of the target objectpublic static java.lang.Class<?> resolvePrimitiveClass(java.lang.Class<?> clazz)
Integer
for an int
). If the given class is not a
primitive, the class itself is returned.clazz
- the class.public static java.lang.Class<?> resolveType(java.lang.String typeName) throws java.lang.ClassNotFoundException
Class.forName(String)
is used, that uses the caller's
class loader.
While method Class.forName(...)
resolves fully qualified names,
it does not resolve primitives, e.g. "java.lang.Boolean" can be resolved but
"boolean" cannot.
typeName
- name of the type. For primitives case is ignored.java.lang.ClassNotFoundException
- if the typeName neither resolves to a primitive, nor to a known
class.public static java.lang.Class<?> resolveType(java.lang.String typeName, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundException
While method Class.forName(...)
resolves fully qualified names,
it does not resolve primitives, e.g. "java.lang.Boolean" can be resolved but
"boolean" cannot.
typeName
- name of the type. For primitives case is ignored.classLoader
- the class loader used for loading the class. If this is null, the
caller class loader is used.java.lang.ClassNotFoundException
- if the typeName neither resolves to a primitive, nor to a known
class.public static java.lang.Class<?> obtainMethodReturnType(java.lang.Class<?> clazz, java.lang.String methodName) throws java.lang.NoSuchMethodException
clazz
- the classmethodName
- the name of the method.java.lang.NoSuchMethodException
- if the class doesn't contain the desired methodpublic static java.lang.Class<?> obtainGenericMethodReturnType(java.lang.Class<?> clazz, java.lang.String methodName) throws java.lang.NoSuchMethodException
clazz
- the classmethodName
- the name of the method.java.lang.NoSuchMethodException
- if the class doesn't contain the desired methodpublic static <T> T performNearestClassLookup(java.lang.Class<?> clazz, java.util.Map<java.lang.Class<?>,T> classMap)
Object
, which
is considered only as the very last option.
As this lookup can be expensive (reflective iteration over the entire inheritance tree) the results should be cached if multiple lookups for the same class are expected.
clazz
- the class being looked up.classMap
- the map to perform the lookup in.null
if no matching entry was
found. Note that null
will also be returned if the entry
for the best matching class was null
.public static boolean isInstanceOfAny(java.lang.Object o, java.lang.Class<?>... classes)
public static boolean isInstanceOfAll(java.lang.Object o, java.lang.Class<?>... classes)
public static <T> T pickInstanceOf(java.lang.Class<T> clazz, java.util.Collection<?> objects)
public static Version obtainClassFileVersion(java.io.InputStream inputStream) throws java.io.IOException
J2SE 7 = 51 J2SE 6.0 = 50 J2SE 5.0 = 49 JDK 1.4 = 48 JDK 1.3 = 47 JDK 1.2 = 46 JDK 1.1 = 45
inputStream
- stream to read class file from.null
if stream does not
contain a class file.java.io.IOException
- if an IO problem occurs.public static java.util.HashMap<java.lang.String,Version> getClassFileVersions(java.io.File jarFile) throws java.io.IOException
java.io.IOException
public static <T> java.util.List<T> listInstances(java.util.List<?> objects, java.lang.Class<T> type)
objects
- List of objects that gets filteredtype
- target type whose instances are returnedpublic static java.lang.Class<?> determineCommonBase(java.lang.Class<?> class1, java.lang.Class<?> class2)
public static java.util.List<java.lang.Class<?>> getBaseHierarchy(java.lang.Class<?> baseClass)
Object
up to the
class itself.public static java.util.Set<java.lang.Class<?>> getAllInterfaces(java.lang.Class<?> baseClass)
public static java.util.List<java.lang.reflect.Field> getAllFields(java.lang.Class<?> type)
public static java.util.List<java.lang.reflect.Method> getAllMethods(java.lang.Class<?> type)
public static java.lang.reflect.Field getField(java.lang.Class<?> type, java.lang.String name) throws java.lang.SecurityException
Class.getField(String)
, this will
also work for non-public fields.java.lang.SecurityException
public static void setFieldValue(java.lang.Object object, java.lang.String fieldName, java.lang.Object newValue) throws java.io.IOException
java.io.IOException
public static <T> boolean isIterable(T parsedObject)
Iterable
, false otherwise.public static java.lang.Iterable<?> asIterable(java.lang.Object arrayOrIterable)
Iterable
).java.lang.IllegalArgumentException
- if given object does not conform to one of the expected typespublic static <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.Class<?> element, java.lang.Class<T> annotationClass) throws java.lang.AssertionError
AssertionError
is thrown.java.lang.AssertionError