de.hpi.fgis.dude.util
Class ReflectUtil

java.lang.Object
  extended by de.hpi.fgis.dude.util.ReflectUtil

public class ReflectUtil
extends Object

Convenience methods for reflective programming.

Author:
Arvid Heise

Constructor Summary
ReflectUtil()
           
 
Method Summary
static Annotation getAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
          Returns the first annotation of the specified annotation type for the given type.
If no annotation is found for the type, the hierarchical ancestors are examined.
static Class<?> getClassForPrimtive(Class<?> primitive)
          Returns the boxing class for the given primitive type.
static Object getFieldValue(Object object, String fieldName)
          Dynamically retrieves the value of the specified field of an object.
static BoundType[] getStaticBoundTypes(Class<?> klass)
          Returns the static bounds for the given type.
static BoundType[] getStaticBoundTypes(Field field)
          Returns the static bounds for the given field.
static BoundType[] getStaticBoundTypes(ParameterizedType parameterizedType)
          Returns the static bounds for the given type.
static boolean hasFunction(Object object, String function, Object... params)
          Checks dynamically whether the object has the specified function, which takes the given parameters.
static Object invoke(Object object, String function, Object... params)
          Dynamically invokes the specified function on an object with the given parameters.
static Boolean isInstantiable(Class<?> type)
          Returns true if the given type has an accessible default constructor.
Note: this method is thread-safe
static boolean isSameTypeOrPrimitive(Class<?> type1, Class<?> type2)
          Returns true if both types are the same or represent the same primitive or boxing type.
static
<T> T
newInstance(Class<T> type)
          Creates a new instance of the given type by invoking the default constructor.
static
<T> T
newInstance(Class<T> type, Object... params)
          Creates a new instance of the given type by invoking the best public constructor for the given parameter.
If there are multiple compatible constructors, the most specific one is chosen.
static BoundType resolveParamizedSuperclass(BoundType type, ParameterizedType superclass)
          Resolves the ParameterizedType superclass for a given BoundType.
Example: class Pair<S, T> { }; class OrderedPair<X> extends Pair<X, X> {}.
If the actual type of X can be inferred for OrderedPair (hence BoundType), this method returns the actual bound for Pair.
static BoundType resolveType(BoundType type, TypeVariable<?> typeVar)
          Resolves the TypeVariable for a given BoundType.
Example: class Foo<T> { Collection<T> bar; }; class IntFoo extends Foo<Integer> {}.
The actual type of bar can be inferred for IntFoo and would result in a BoundType.of(Integer.class).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectUtil

public ReflectUtil()
Method Detail

newInstance

public static <T> T newInstance(Class<T> type)
                     throws IllegalArgumentException
Creates a new instance of the given type by invoking the default constructor. If the default constructor is not public, the method will try to gain access through AccessibleObject.setAccessible(boolean).

Note: this method is not thread-safe

Type Parameters:
T - the type to instantiate
Parameters:
type - the type to instantiate
Returns:
the created instance
Throws:
IllegalArgumentException - if the type has no accessible default constructor or an exception occurred during the invocation: possible causes are NoSuchMethodException, InstantiationException , IllegalAccessException, * InvocationTargetException

newInstance

public static <T> T newInstance(Class<T> type,
                                Object... params)
                     throws IllegalArgumentException
Creates a new instance of the given type by invoking the best public constructor for the given parameter.
If there are multiple compatible constructors, the most specific one is chosen.
If there are several constructors with the same degree of specify, an Exception is thrown.
Note: this method is thread-safe

Type Parameters:
T - the type to instantiate
Parameters:
type - the type to instantiate
params - The constructors parameters.
Returns:
the created instance
Throws:
IllegalArgumentException - if the type has 0 or more than 2 matching constructors or an exception occurred during the invocation: possible causes are NoSuchMethodException, InstantiationException , IllegalAccessException, InvocationTargetException

getClassForPrimtive

public static Class<?> getClassForPrimtive(Class<?> primitive)
Returns the boxing class for the given primitive type. A primitive type returns true for Class.isPrimitive().

Parameters:
primitive - the primitive type
Returns:
the boxing class or null if the given class is not a primitive

isSameTypeOrPrimitive

public static boolean isSameTypeOrPrimitive(Class<?> type1,
                                            Class<?> type2)
Returns true if both types are the same or represent the same primitive or boxing type.

Parameters:
type1 - the first type
type2 - the second type
Returns:
the boxing class or null if the given class is not a primitive

isInstantiable

public static Boolean isInstantiable(Class<?> type)
Returns true if the given type has an accessible default constructor.
Note: this method is thread-safe

Parameters:
type - the type to check
Returns:
true if it has an accessible default constructor.

getStaticBoundTypes

public static BoundType[] getStaticBoundTypes(ParameterizedType parameterizedType)
Returns the static bounds for the given type.
A static bound occurs if a subclass specifies the type parameter of the superclass explicitly.
Example: class IntList extends ArrayList<Integer> {} would result in a BoundType.of(Integer.class).

Parameters:
parameterizedType - the type to examine
Returns:
all static bounds

getStaticBoundTypes

public static BoundType[] getStaticBoundTypes(Class<?> klass)
Returns the static bounds for the given type.
A static bound occurs if a subclass specifies the type parameter of the superclass explicitly.
Example: class IntList extends ArrayList<Integer> {} would result in a BoundType.of(Integer.class).

Parameters:
klass - the type to examine
Returns:
all static bounds

getStaticBoundTypes

public static BoundType[] getStaticBoundTypes(Field field)
Returns the static bounds for the given field.
A static bound occurs if a subclass specifies the type parameter of the superclass explicitly.
Example: class Foo { Collection<Integer> bar; } would result in a BoundType.of(Integer.class).

Parameters:
field - the field to examine
Returns:
all static bounds

resolveType

public static BoundType resolveType(BoundType type,
                                    TypeVariable<?> typeVar)
Resolves the TypeVariable for a given BoundType.
Example: class Foo<T> { Collection<T> bar; }; class IntFoo extends Foo<Integer> {}.
The actual type of bar can be inferred for IntFoo and would result in a BoundType.of(Integer.class).

Parameters:
type - the actual bound type
typeVar - the placeholder type variable
Returns:
the bound type for the TypeVariable

resolveParamizedSuperclass

public static BoundType resolveParamizedSuperclass(BoundType type,
                                                   ParameterizedType superclass)
Resolves the ParameterizedType superclass for a given BoundType.
Example: class Pair<S, T> { }; class OrderedPair<X> extends Pair<X, X> {}.
If the actual type of X can be inferred for OrderedPair (hence BoundType), this method returns the actual bound for Pair.

Parameters:
type - the actual bound type
superclass - the superclass of the wrapped type
Returns:
the bound type for the ParameterizedType

getAnnotation

public static Annotation getAnnotation(Class<?> type,
                                       Class<? extends Annotation> annotationType)
Returns the first annotation of the specified annotation type for the given type.
If no annotation is found for the type, the hierarchical ancestors are examined.

Parameters:
type - the type which might be annotated
annotationType - the annotation type
Returns:
the annotation or null

invoke

public static Object invoke(Object object,
                            String function,
                            Object... params)
Dynamically invokes the specified function on an object with the given parameters.

Parameters:
object - the object to invoke on
function - the function to call
params - the parameters of the function
Returns:
the result of the invocation

hasFunction

public static boolean hasFunction(Object object,
                                  String function,
                                  Object... params)
Checks dynamically whether the object has the specified function, which takes the given parameters.

Parameters:
object - the object to invoke on
function - the function to call
params - the parameters of the function
Returns:
true if such a method exists

getFieldValue

public static Object getFieldValue(Object object,
                                   String fieldName)
Dynamically retrieves the value of the specified field of an object.

Parameters:
object - the object to invoke on
fieldName - the name of the field
Returns:
the value of the field


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.