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$

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 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[] resolveSuperParameters(Type[] parameters, Class childClass, Type[] childParameters)
           
static void setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)
          Sets a value to a field using reflection even if the field is private.
 
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&lt;A, B&gt; 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&lt;A, B&gt; 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&lt;A, B&gt; 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&lt;A, B&gt;, SomeOtherClass&lt;C&gt; 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&lt;A, B&gt;, SomeOtherClass&lt;C&gt; will return FilterClass&lt;A, B&gt;, SomeOtherClass&lt;C&gt;.

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

resolveSuperParameters

public static Type[] resolveSuperParameters(Type[] parameters,
                                            Class childClass,
                                            Type[] childParameters)
Parameters:
parameters - the parameters of a direct superclass or interface
childClass - a 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

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


Copyright © 2004-2012 XWiki. All Rights Reserved.