Class MoreReflection


  • @ParametersAreNonnullByDefault
    public final class MoreReflection
    extends Object
    Static utility methods related to reflection.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Optional<Constructor> findConstructor​(Class type, Class[] argumentTypes)
      Returns the constructor for creating a new instance of type, whose parameters are assignable with the argumentTypes.
      static Optional<Method> findFactoryMethod​(Class type, Class[] argumentTypes)
      Returns the static factory method for creating instances of type, whose parameters are assignable with the argumentTypes.
      static Set<Class<?>> getAllInterfaces​(Class<?> type)
      Retrieves all interfaces implemented by the given class and its superclasses.
      static <T> Function<Object[],​T> getInstantiator​(Class<T> type, Class[] argumentTypes)
      Returns a Function that creates new instances of type, using as arguments instances of argumentTypes, i.e., using a constructor or factory method whose signature matches argumentTypes.
      static boolean isAssignable​(Class<?> leftOperand, Class<?> rightOperand)
      Checks whether a left operand type can be assigned from the right operand type If the operand is a primitive type, first converts it to its wrapper class.
      static boolean matches​(Executable executable, Class[] argumentTypes)
      Checks whether an Executable (Constructor or Method) is compatible with the argumentTypes, i.e., whether all parameters types are assignable from all types of argumentTypes, respectively.
      static <T> T newInstance​(Class<T> type)
      Gets or creates a instance of the given type by using the static method named name.
    • Method Detail

      • newInstance

        @Nonnull
        public static <T> T newInstance​(Class<T> type)
        Gets or creates a instance of the given type by using the static method named name.

        If the type is annotated by Singleton or by Builder, then the static method identified by the value of the annotation is used to get the instance. Otherwise, the default constructor is used.

        Type Parameters:
        T - the type of the instance
        Parameters:
        type - the class to look for
        Returns:
        the single instance if the type is a singleton, or a new instance
        Throws:
        ReflectionException - if an error occurs during the instantiation
      • getAllInterfaces

        @Nonnull
        public static Set<Class<?>> getAllInterfaces​(Class<?> type)
        Retrieves all interfaces implemented by the given class and its superclasses.

        The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order is maintained.

        Parameters:
        type - the class to look up
        Returns:
        a Set of interfaces in order
      • findConstructor

        public static Optional<Constructor> findConstructor​(Class type,
                                                            Class[] argumentTypes)
        Returns the constructor for creating a new instance of type, whose parameters are assignable with the argumentTypes.
        Parameters:
        type - the class to look for
        argumentTypes - a set of types allowing to find the suitable constructor.
        Returns:
        an Optional containing the suitable constructor, or Optional.empty() if no constructor is found.
      • findFactoryMethod

        public static Optional<Method> findFactoryMethod​(Class type,
                                                         Class[] argumentTypes)
        Returns the static factory method for creating instances of type, whose parameters are assignable with the argumentTypes.
        Parameters:
        type - the class to look for.
        argumentTypes - a set of types allowing to find the suitable method.
        Returns:
        an Optional containing the suitable factory method, or Optional.empty() if no constructor is found.
      • getInstantiator

        public static <T> Function<Object[],​T> getInstantiator​(Class<T> type,
                                                                     Class[] argumentTypes)
        Returns a Function that creates new instances of type, using as arguments instances of argumentTypes, i.e., using a constructor or factory method whose signature matches argumentTypes.
        Parameters:
        type - the class to look for.
        argumentTypes - the signature of the suitable constructor or method
        Returns:
        a Function that creates new instances of type, using either a Constructor or a static factory Method.
      • matches

        public static boolean matches​(Executable executable,
                                      Class[] argumentTypes)
        Checks whether an Executable (Constructor or Method) is compatible with the argumentTypes, i.e., whether all parameters types are assignable from all types of argumentTypes, respectively. The argumentTypes parameter is an array of Class objects that are assignment-compatible with the executable's formal parameter types, in declared order.
        Parameters:
        executable - the instance of Executable to be considered.
        argumentTypes - an array of types to be compared with the executable parameters.
        Returns:
        true, if each parameter of executable is assignable from an element of argumentTypes.
      • isAssignable

        public static boolean isAssignable​(Class<?> leftOperand,
                                           Class<?> rightOperand)
        Checks whether a left operand type can be assigned from the right operand type If the operand is a primitive type, first converts it to its wrapper class. Contrary to the Class.isAssignableFrom(java.lang.Class) method, this method takes ont account the assignment between Wrappers Classes and Primitive Types.
        Parameters:
        leftOperand - The Class or Primitive we want to assign into
        rightOperand - The Class or Primitive we want to assign
        Returns:
        true, if assignment is possible