Class ReflectionUtils

java.lang.Object
it.tidalwave.util.ReflectionUtils

public class ReflectionUtils extends Object
Adapted from this article
Author:
Ian Robertson, Fabrizio Giudici
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getTypeArguments

      @Nonnull public static <T> List<Class<?>> getTypeArguments(@Nonnull Class<T> baseClass, @Nonnull Class<? extends T> childClass)
      Get the actual type arguments a subclass has used to extend a generic base class. Note: if the base class is an interface, this method will work only if it is the first inherited interface in childClass.
      Type Parameters:
      T - the static type of the base class
      Parameters:
      baseClass - the base class
      childClass - the subclass
      Returns:
      a list of the raw classes for the actual type arguments.
    • instantiateWithDependencies

      public static <T> T instantiateWithDependencies(@Nonnull Class<? extends T> type, @Nonnull Map<Class<?>,Object> beans)
      Instantiates an object of the given class performing dependency injections through the constructor.
      Type Parameters:
      T - the generic type of the object to instantiate
      Parameters:
      type - the dynamic type of the object to instantiate; it is expected to have a single constructor
      beans - the bag of objects to instantiate
      Returns:
      the new instance
      Throws:
      RuntimeException - if something fails
      Since:
      3.2-ALPHA-17
    • injectDependencies

      public static void injectDependencies(@Nonnull Object object, @Nonnull Map<Class<?>,Object> beans)
      Performs dependency injection to an object by means of field introspection.
      Parameters:
      object - the object
      beans - the bag of objects to instantiate
      Since:
      3.2-ALPHA-17
    • getClass

      @Nullable public static Class<?> getClass(@Nonnull Type type)
      Returns the class literal associated to the given type.
      Parameters:
      type - the type to inspect
      Returns:
      the class literal; it might be null if fails