Package org.nuxeo.common.utils
Class ReflectUtils
java.lang.Object
org.nuxeo.common.utils.ReflectUtils
- Since:
- 2025.14
-
Method Summary
Modifier and TypeMethodDescriptionstatic <O extends B,B>
List<B>downgradeCast(List<O> originalList) Returns the given list typed with a parent type.static <T,D, O extends D>
Class<T>retrieveFirstParameterType(Class<O> clazz, Class<D> typedClass) Retrieves the first generic parameter typeTfrom a classOthat extends a typed classD<T, ...>.static <D,O extends D>
Class<?>[]retrieveParameterTypes(Class<O> clazz, Class<D> typedClass, String parameterName, String... parameterNames) Retrieves a generic parameter typeTfrom a classOthat extends a typed classD<..., T, ...>, by its parameter name.
-
Method Details
-
downgradeCast
Returns the given list typed with a parent type. The benefit of this method is that it checks that theReflectUtilstype is assignable toReflectUtilstype at compile time.For example when you want a List<Number> from a List<Long>.
List<Long> longList = List.of(1L, 2L, 3L); // casting List<Long> to List<Number> without the helper List<number> previousNumberList = (List<Number>) ((List<?>) longList); // casting List<Long> to List<Number> with the helper List<Number> numberList = ReflectUtils.downgradeCast(longList);- Type Parameters:
O- the original list typeB- the target parent list type- Parameters:
originalList- the list to downgrade cast- Returns:
- the given list typed with a parent type
-
retrieveFirstParameterType
public static <T,D, Class<T> retrieveFirstParameterTypeO extends D> (Class<O> clazz, Class<D> typedClass) Retrieves the first generic parameter typeTfrom a classOthat extends a typed classD<T, ...>.- Type Parameters:
T- the target type to retrieveD- the type that declares the generic parameter, such asD<T, ...>O- the type that infers the generic parameter, such asO extends D<T, ...>- Parameters:
clazz- the instance to extract the target type fromtypedClass- the type that declares the generic parameter- Returns:
- the type from the first generic parameter
-
retrieveParameterTypes
public static <D,O extends D> Class<?>[] retrieveParameterTypes(Class<O> clazz, Class<D> typedClass, String parameterName, String... parameterNames) Retrieves a generic parameter typeTfrom a classOthat extends a typed classD<..., T, ...>, by its parameter name.- Type Parameters:
D- the type that declares the generic parameter, such asD<..., T, ..>O- the type that infers the generic parameter, such asO extends D<..., T, ...>- Parameters:
clazz- the instance to extract the target type fromtypedClass- the type that declares the generic parameter- Returns:
- the parameterName types from the generic parameters
-