Class ReflectUtils

java.lang.Object
org.nuxeo.common.utils.ReflectUtils

public final class ReflectUtils extends Object
Since:
2025.14
  • Method Details

    • downgradeCast

      public static <O extends B, B> List<B> downgradeCast(List<O> originalList)
      Returns the given list typed with a parent type. The benefit of this method is that it checks that the ReflectUtils type is assignable to ReflectUtils type 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 type
      B - 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, O extends D> Class<T> retrieveFirstParameterType(Class<O> clazz, Class<D> typedClass)
      Retrieves the first generic parameter type T from a class O that extends a typed class D<T, ...>.
      Type Parameters:
      T - the target type to retrieve
      D - the type that declares the generic parameter, such as D<T, ...>
      O - the type that infers the generic parameter, such as O extends D<T, ...>
      Parameters:
      clazz - the instance to extract the target type from
      typedClass - 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 type T from a class O that extends a typed class D<..., T, ...>, by its parameter name.
      Type Parameters:
      D - the type that declares the generic parameter, such as D<..., T, ..>
      O - the type that infers the generic parameter, such as O extends D<..., T, ...>
      Parameters:
      clazz - the instance to extract the target type from
      typedClass - the type that declares the generic parameter
      Returns:
      the parameterName types from the generic parameters