|
TUM CCSM Commons | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.tum.cs.commons.reflect.ReflectionUtils
public class ReflectionUtils
This class provides utility methods for reflection purposes. In particular it
provides access to FormalParameter
.
Constructor Summary | |
---|---|
ReflectionUtils()
|
Method Summary | ||
---|---|---|
static
|
convertString(java.lang.String value,
java.lang.Class<T> targetType)
Convert a String to an Object of the provided type. |
|
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 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
|
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
|
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
|
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ReflectionUtils()
Method Detail |
---|
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 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 from
Object
, 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 value
java.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 object
public 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
major minor Java Version 45 3 1.0 45 3 1.1 46 0 1.2 47 0 1.3 48 0 1.4 49 0 1.5 50 0 1.6
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 returned
|
TUM CCSM Commons | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |