|
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(String value,
Class<T> targetType)
Convert a String to an Object of the provided type. |
|
static HashMap<String,Version> |
getClassFileVersions(File jarFile)
This method extracts the class file version from each class file in the provided jar. |
|
static FormalParameter[] |
getFormalParameters(Method method)
Obtain array of formal parameters for a method. |
|
static List<Class<?>> |
getSuperClasses(Class<?> clazz)
Get super class list of a class. |
|
static Object |
invoke(Method method,
Object object,
Map<FormalParameter,Object> parameterMap)
Invoke a method with parameters. |
|
static boolean |
isAssignable(Class<?> source,
Class<?> target)
Check whether an Object of the source type can be used instead of an Object of the target type. |
|
static boolean |
isConvertibleFromString(Class<?> targetType)
This method checks if the provided type can be converted from a string. |
|
static boolean |
isInstanceOfAll(Object o,
Class<?>... classes)
Returns whether the given object is an instance of all of the given classes. |
|
static boolean |
isInstanceOfAny(Object o,
Class<?>... classes)
Returns whether the given object is an instance of at least one of the given classes. |
|
static
|
listInstances(List<?> objects,
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(InputStream inputStream)
Obtains the version of a Java class file. |
|
static Class<?> |
obtainGenericMethodReturnType(Class<?> clazz,
String methodName)
Obtain the generic return type of method. |
|
static Class<?> |
obtainMethodReturnType(Class<?> clazz,
String methodName)
Obtain the return type of method. |
|
static
|
performNearestClassLookup(Class<?> clazz,
Map<Class<?>,T> classMap)
Returns the value from the map, whose key is the best match for the given class. |
|
static
|
pickInstanceOf(Class<T> clazz,
Collection<?> objects)
Returns the first object in the given collection which is an instance of the given class (or null otherwise). |
|
static Class<?> |
resolvePrimitiveClass(Class<?> clazz)
Returns the wrapper class type for a primitive type (e.g. |
|
static Class<?> |
resolveType(String typeName)
Resolves the class object for a type name. |
|
static Class<?> |
resolveType(String typeName,
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(String value,
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(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(Method method)
FormalParameterpublic static List<Class<?>> getSuperClasses(Class<?> clazz)
clazz - the class to start traversal from
Object, primitives and interfaces this returns an
empty list. Object. Enum and
Object
public static Object invoke(Method method,
Object object,
Map<FormalParameter,Object> parameterMap)
throws IllegalArgumentException,
IllegalAccessException,
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
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.
IllegalAccessException - if this Method object enforces Java language access control
and the underlying method is inaccessible.
InvocationTargetException - if the underlying method throws an exception.
NullPointerException - if the specified object is null and the method is an instance
method.
ExceptionInInitializerError - if the initialization provoked by this method fails.
public static boolean isAssignable(Class<?> source,
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 Class<?> resolvePrimitiveClass(Class<?> clazz)
Integer for an int). If the given class is not
a primitive, the class itself is returned.
clazz - the class.
public static Class<?> resolveType(String typeName)
throws 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.
ClassNotFoundException - if the typeName neither resolves to a primitive, nor to a
known class.
public static Class<?> resolveType(String typeName,
ClassLoader classLoader)
throws 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.
ClassNotFoundException - if the typeName neither resolves to a primitive, nor to a
known class.
public static Class<?> obtainMethodReturnType(Class<?> clazz,
String methodName)
throws NoSuchMethodException
clazz - the classmethodName - the name of the method.
NoSuchMethodException - if the class doesn't contain the desired method
public static Class<?> obtainGenericMethodReturnType(Class<?> clazz,
String methodName)
throws NoSuchMethodException
clazz - the classmethodName - the name of the method.
NoSuchMethodException - if the class doesn't contain the desired method
public static <T> T performNearestClassLookup(Class<?> clazz,
Map<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(Object o,
Class<?>... classes)
public static boolean isInstanceOfAll(Object o,
Class<?>... classes)
public static <T> T pickInstanceOf(Class<T> clazz,
Collection<?> objects)
public static Version obtainClassFileVersion(InputStream inputStream)
throws 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.
IOException - if an IO problem occurs.
public static HashMap<String,Version> getClassFileVersions(File jarFile)
throws IOException
IOException
public static <T> List<T> listInstances(List<?> objects,
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 | |||||||||