org.xwiki.component.util
Class ReflectionUtils

java.lang.Object
  extended by org.xwiki.component.util.ReflectionUtils

public final class ReflectionUtils
extends Object

Various Reflection utilities.

Since:
2.1RC1
Version:
$Id: 903d1d86eec58a505935b5445313a1910c7b6dae $

Method Summary
static Collection<Field> getAllFields(Class<?> clazz)
           
static
<T extends Annotation>
T
getDirectAnnotation(Class<T> annotationClass, AnnotatedElement element)
          Get the first found annotation with the provided class directly assigned to the provided AnnotatedElement .
static List<Type> getDirectTypes(Type type)
           
static Field getField(Class<?> clazz, String fieldName)
           
static Type getGenericClassType(Class clazz, Class filterClass)
          Extract the real Type from the passed class.
static Type getLastFieldGenericArgument(Field field)
          Extract the last generic type from the passed field.
static Class<?> getLastGenericClassType(Class clazz, Class filterClass)
          Extract the last generic type from the passed class.
static Class<?> getLastGenericFieldType(Field field)
          Extract the last generic type from the passed field.
static Type getLastTypeGenericArgument(Type type)
          Extract the last generic type from the passed Type.
static Class getTypeClass(Type type)
          Extract the main class from the passed Type.
static Type[] resolveSuperArguments(Type[] parameters, Class childClass, Type[] childParameters)
           
static Type[] resolveSuperArguments(Type[] parameters, Type childType)
           
static Type resolveType(Type type, Map<TypeVariable,Type> typeMapping)
           
static Type resolveType(Type targetType, Type rootType)
          Find and replace the generic parameters with the real types.
static void setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)
          Sets a value to a field using reflection even if the field is private.
static Type unserializeType(String serializedType, ClassLoader classLoader)
          Retrieve a Type object from it's serialized form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAllFields

public static Collection<Field> getAllFields(Class<?> clazz)
Parameters:
clazz - the class for which to return all fields
Returns:
all fields declared by the passed class and its superclasses

getField

public static Field getField(Class<?> clazz,
                             String fieldName)
                      throws NoSuchFieldException
Parameters:
clazz - the class for which to return all fields
fieldName - the name of the field to get
Returns:
the field specified from either the passed class or its superclasses
Throws:
NoSuchFieldException - if the field doesn't exist in the class or superclasses

getTypeClass

public static Class getTypeClass(Type type)
Extract the main class from the passed Type.

Parameters:
type - the generic Type
Returns:
the main Class of the generic Type
Since:
4.0M1

setFieldValue

public static void setFieldValue(Object instanceContainingField,
                                 String fieldName,
                                 Object fieldValue)
Sets a value to a field using reflection even if the field is private.

Parameters:
instanceContainingField - the object containing the field
fieldName - the name of the field in the object
fieldValue - the value to set for the provided field

getLastGenericFieldType

public static Class<?> getLastGenericFieldType(Field field)
Extract the last generic type from the passed field. For example private List<A, B> field would return the B class.

Parameters:
field - the field from which to extract the generic type
Returns:
the class of the last generic type or null if the field doesn't have a generic type

getLastFieldGenericArgument

public static Type getLastFieldGenericArgument(Field field)
Extract the last generic type from the passed field. For example private List<A, B> field would return the B class.

Parameters:
field - the field from which to extract the generic type
Returns:
the type of the last generic type or null if the field doesn't have a generic type
Since:
4.0M1

getLastTypeGenericArgument

public static Type getLastTypeGenericArgument(Type type)
Extract the last generic type from the passed Type. For example private List<A, B> field would return the B class.

Parameters:
type - the type from which to extract the generic type
Returns:
the type of the last generic type or null if the field doesn't have a generic type
Since:
4.0M1

getLastGenericClassType

public static Class<?> getLastGenericClassType(Class clazz,
                                               Class filterClass)
Extract the last generic type from the passed class. For example public Class MyClass implements FilterClass<A, B>, SomeOtherClass<C> will return B.

Parameters:
clazz - the class to extract from
filterClass - the class of the generic type we're looking for
Returns:
the last generic type from the interfaces of the passed class, filtered by the passed filter class

getGenericClassType

public static Type getGenericClassType(Class clazz,
                                       Class filterClass)
Extract the real Type from the passed class. For example public Class MyClass implements FilterClass<A, B>, SomeOtherClass<C> will return FilterClass<A, B>, SomeOtherClass<C>.

Parameters:
clazz - the class to extract from
filterClass - the class of the generic type we're looking for
Returns:
the real Type from the interfaces of the passed class, filtered by the passed filter class
Since:
4.0M1

resolveSuperArguments

public static Type[] resolveSuperArguments(Type[] parameters,
                                           Type childType)
Parameters:
parameters - the parameters of a direct superclass or interface
childType - a extending class as Type
Returns:
the actual parameters of the direct superclass or interface, return null if it's impossible to resolve

resolveSuperArguments

public static Type[] resolveSuperArguments(Type[] parameters,
                                           Class childClass,
                                           Type[] childParameters)
Parameters:
parameters - the parameters of a direct superclass or interface
childClass - an extending class
childParameters - the actual parameters of the extending class
Returns:
the actual parameters of the direct superclass or interface, return null if it's impossible to resolve

resolveType

public static Type resolveType(Type type,
                               Map<TypeVariable,Type> typeMapping)
Parameters:
type - the type to resolve
typeMapping - the mapping between TypeVariable and real type
Returns:
the resolved type, the passed type it does not need to be resolved or null if it can't be resolved

resolveType

public static Type resolveType(Type targetType,
                               Type rootType)
Find and replace the generic parameters with the real types.

Parameters:
targetType - the type for which to resolve the parameters
rootType - an extending class as Type
Returns:
the Type with resolved parameters

unserializeType

public static Type unserializeType(String serializedType,
                                   ClassLoader classLoader)
                            throws ClassNotFoundException
Retrieve a Type object from it's serialized form.

Parameters:
serializedType - the serialized form of the Type to retrieve
classLoader - the ClassLoader to look into to find the given Type
Returns:
the type built from the given String
Throws:
ClassNotFoundException - if no class corresponding to the passed serialized type can be found

getDirectAnnotation

public static <T extends Annotation> T getDirectAnnotation(Class<T> annotationClass,
                                                           AnnotatedElement element)
Get the first found annotation with the provided class directly assigned to the provided AnnotatedElement .

Type Parameters:
T - the type of the annotation
Parameters:
annotationClass - the annotation class
element - the class on which annotation are assigned
Returns:
the found annotation or null if there is none

getDirectTypes

public static List<Type> getDirectTypes(Type type)
Parameters:
type - the type from which to extract super type and interfaces
Returns:
the direct super type and interfaces for the provided type


Copyright © 2004–2014 XWiki. All rights reserved.