|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object de.hpi.fgis.dude.util.ReflectUtil
public class ReflectUtil
Convenience methods for reflective programming.
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
|
newInstance(Class<T> type)
Creates a new instance of the given type by invoking the default constructor. |
|
static
|
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 |
---|
public ReflectUtil()
Method Detail |
---|
public static <T> T newInstance(Class<T> type) throws IllegalArgumentException
AccessibleObject.setAccessible(boolean)
.
T
- the type to instantiatetype
- the type to instantiate
IllegalArgumentException
- if the type has no accessible default constructor or an exception occurred during the invocation: possible causes are
NoSuchMethodException
, InstantiationException
, IllegalAccessException
, * InvocationTargetException
public static <T> T newInstance(Class<T> type, Object... params) throws IllegalArgumentException
T
- the type to instantiatetype
- the type to instantiateparams
- The constructors parameters.
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
public static Class<?> getClassForPrimtive(Class<?> primitive)
Class.isPrimitive()
.
primitive
- the primitive type
public static boolean isSameTypeOrPrimitive(Class<?> type1, Class<?> type2)
type1
- the first typetype2
- the second type
public static Boolean isInstantiable(Class<?> type)
type
- the type to check
public static BoundType[] getStaticBoundTypes(ParameterizedType parameterizedType)
class IntList extends ArrayList<Integer> {}
would result in a BoundType.of(Integer.class)
.
parameterizedType
- the type to examine
public static BoundType[] getStaticBoundTypes(Class<?> klass)
class IntList extends ArrayList<Integer> {}
would result in a BoundType.of(Integer.class)
.
klass
- the type to examine
public static BoundType[] getStaticBoundTypes(Field field)
class Foo { Collection<Integer> bar; }
would result in a BoundType.of(Integer.class)
.
field
- the field to examine
public static BoundType resolveType(BoundType type, TypeVariable<?> typeVar)
TypeVariable
for a given BoundType
.class Foo<T> { Collection<T> bar; }; class IntFoo extends Foo<Integer> {}
.BoundType.of(Integer.class)
.
type
- the actual bound typetypeVar
- the placeholder type variable
TypeVariable
public static BoundType resolveParamizedSuperclass(BoundType type, ParameterizedType superclass)
ParameterizedType
superclass for a given BoundType
.class Pair<S, T> { }; class OrderedPair<X> extends Pair<X, X> {}
.
type
- the actual bound typesuperclass
- the superclass of the wrapped type
ParameterizedType
public static Annotation getAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
type
- the type which might be annotatedannotationType
- the annotation type
public static Object invoke(Object object, String function, Object... params)
object
- the object to invoke onfunction
- the function to callparams
- the parameters of the function
public static boolean hasFunction(Object object, String function, Object... params)
object
- the object to invoke onfunction
- the function to callparams
- the parameters of the function
public static Object getFieldValue(Object object, String fieldName)
object
- the object to invoke onfieldName
- the name of the field
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |