Class ReflectionUtils
- java.lang.Object
-
- com.hybris.cockpitng.core.util.impl.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.ObjectConvenience methods dealing with java reflection api.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K> java.lang.Class<K>extractGenericParameterType(java.lang.Class<?> clazz, int position)Simplified version of methodextractGenericParameterType(Class, Class, int).static <P,C extends P,K>
java.lang.Class<K>extractGenericParameterType(java.lang.Class<C> clazz, java.lang.Class<P> baseClass)Simplified version of methodextractGenericParameterType(Class, Class, int).static <P,C extends P,K>
java.lang.Class<K>extractGenericParameterType(java.lang.Class<C> clazz, java.lang.Class<P> baseClass, int position)Helper method to fetch generic argument's type of a class.static java.lang.reflect.Field[]getAllDeclaredFields(java.lang.Class<?> clazz)Getting all declared fields from the specified class and all its superclasses.static java.lang.reflect.FieldgetDeclaredField(java.lang.Class<?> clazz, java.lang.String fieldName)Looks for declared field in specified class and all its superclasses.static <V> VgetField(java.lang.reflect.Field field, java.lang.Object object)Getting a fields value regardless its accessibility.static voidsetField(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value)Setting a fields value regardless its accessibility.static voidsetField(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value, java.lang.Class<? extends java.lang.annotation.Annotation> annotationConstraint)Setting a fields value regardless its accessibility.
-
-
-
Method Detail
-
getAllDeclaredFields
public static java.lang.reflect.Field[] getAllDeclaredFields(java.lang.Class<?> clazz)
Getting all declared fields from the specified class and all its superclasses.- See Also:
Class.getDeclaredFields()
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> clazz, java.lang.String fieldName) throws java.lang.NoSuchFieldExceptionLooks for declared field in specified class and all its superclasses.- Throws:
java.lang.NoSuchFieldException- See Also:
Class.getDeclaredFields()
-
getField
public static <V> V getField(java.lang.reflect.Field field, java.lang.Object object)Getting a fields value regardless its accessibility.- Parameters:
field- The field which value should be read.object- The object at which the field value should be read- Throws:
java.lang.IllegalArgumentException- See Also:
Field.get(Object)
-
setField
public static void setField(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value) throws java.lang.IllegalArgumentExceptionSetting a fields value regardless its accessibility.- Parameters:
field- The field which value should be set.object- The object at which the field value should be setvalue- The value- Throws:
java.lang.IllegalArgumentExceptionjava.lang.IllegalAccessException- See Also:
Field.set(Object, Object)
-
setField
public static void setField(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value, java.lang.Class<? extends java.lang.annotation.Annotation> annotationConstraint)Setting a fields value regardless its accessibility.- Parameters:
field- The field which value should be set.object- The object at which the field value should be setvalue- The valueannotationConstraint- A annotation class serving as constraint, i.e. the field value will only be set, if this annotation is present.- See Also:
Field.set(Object, Object)
-
extractGenericParameterType
public static <K> java.lang.Class<K> extractGenericParameterType(java.lang.Class<?> clazz, int position)Simplified version of methodextractGenericParameterType(Class, Class, int). A superclass of provided class is taken as a baseClass.
-
extractGenericParameterType
public static <P,C extends P,K> java.lang.Class<K> extractGenericParameterType(java.lang.Class<C> clazz, java.lang.Class<P> baseClass)Simplified version of methodextractGenericParameterType(Class, Class, int). First generic parameter is assumed (position 0).
-
extractGenericParameterType
public static <P,C extends P,K> java.lang.Class<K> extractGenericParameterType(java.lang.Class<C> clazz, java.lang.Class<P> baseClass, int position)Helper method to fetch generic argument's type of a class. For example when invoked on a class that extends from ArrayList<String>:
returns class java.lang.String. Method may resolve only those parameters that are already set in class definition. If a class defines a generic parameter by itself, then i may not be resolved, i.e.ReflectionUtils.extractGenericParameterType(new ArrayList<String>(){}.getClass(), 0)
returns null.ReflectionUtils.extractGenericParameterType(new ArrayList<String>().getClass(), 0)- Parameters:
clazz- type to be scannedbaseClass- type which parameter is to be found. Ff a class implements multiple generic interfaces, or deeper class hesitancy is involved, then parameter is used to implicitly specify whose generic parameter is to be extractedposition- generic argument's position- Returns:
- generic argument's type or
nullif argument cannot be resolved
-
-