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 Field getField(Class<?> clazz, String fieldName)
           
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 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

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 of null if the field doesn't have a generic type

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


Copyright © 2004-2012 XWiki. All Rights Reserved.