Package org.xwiki.component.util
Class ReflectionUtils
- java.lang.Object
-
- org.xwiki.component.util.ReflectionUtils
-
public final class ReflectionUtils extends Object
Various Reflection utilities.- Since:
- 2.1RC1
- Version:
- $Id: 0975b8ebed177070bfea1855c2e2e4cece89ff2a $
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Collection<Field>
getAllFields(Class<?> clazz)
static Collection<Method>
getAllMethods(Class<?> clazz)
Find all methods (public, private, package private, protected) in the current class and in all its superclasses.static <T extends Annotation>
TgetDirectAnnotation(Class<T> annotationClass, AnnotatedElement element)
Get the first found annotation with the provided class directly assigned to the providedAnnotatedElement
.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 passedType
.static Type[]
resolveSuperArguments(Type[] parameters, Class childClass, Type[] childParameters)
static Type[]
resolveSuperArguments(Type[] parameters, Type childType)
static Type
resolveType(Type targetType, Type rootType)
Find and replace the generic parameters with the real types.static Type
resolveType(Type type, Map<TypeVariable,Type> typeMapping)
static String
serializeType(Type type)
Serialize a type in a String using a standard definition.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 aType
object from it's serialized form.
-
-
-
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 fieldsfieldName
- 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 passedType
.
-
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 fieldfieldName
- the name of the field in the objectfieldValue
- 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 exampleprivate List<A, B> field
would return theB
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 exampleprivate List<A, B> field
would return theB
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 exampleprivate List<A, B> field
would return theB
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 examplepublic Class MyClass implements FilterClass<A, B>, SomeOtherClass<C>
will returnB
.- Parameters:
clazz
- the class to extract fromfilterClass
- 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 examplepublic Class MyClass implements FilterClass<A, B>, SomeOtherClass<C>
will returnFilterClass<A, B>, SomeOtherClass<C>
.- Parameters:
clazz
- the class to extract fromfilterClass
- 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 interfacechildType
- 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 interfacechildClass
- an extending classchildParameters
- 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 resolvetypeMapping
- 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 parametersrootType
- an extending class as Type- Returns:
- the Type with resolved parameters
-
unserializeType
public static Type unserializeType(String serializedType, ClassLoader classLoader) throws ClassNotFoundException
Retrieve aType
object from it's serialized form.- Parameters:
serializedType
- the serialized form of theType
to retrieveclassLoader
- theClassLoader
to look into to find the givenType
- 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 providedAnnotatedElement
.- Type Parameters:
T
- the type of the annotation- Parameters:
annotationClass
- the annotation classelement
- 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
-
serializeType
public static String serializeType(Type type)
Serialize a type in a String using a standard definition.- Parameters:
type
- the type to serialize.- Returns:
- a string representing this type.
- Since:
- 11.2RC1
-
getAllMethods
public static Collection<Method> getAllMethods(Class<?> clazz)
Find all methods (public, private, package private, protected) in the current class and in all its superclasses.- Parameters:
clazz
- the class for which to return all the methods- Returns:
- the collections of methods (in the defined order from current to superclasses)
- Since:
- 12.5RC1
-
-