Class ConstructorInstantiator

java.lang.Object
org.mockito.internal.creation.instance.ConstructorInstantiator
All Implemented Interfaces:
Instantiator

public class ConstructorInstantiator extends Object implements Instantiator
  • Field Details

    • hasOuterClassInstance

      private final boolean hasOuterClassInstance
      Whether or not the constructors used for creating an object refer to an outer instance or not. This member is only used to for constructing error messages. If an outer inject exists, it would be the first ([0]) element of the constructorArgs array.
    • constructorArgs

      private final Object[] constructorArgs
  • Constructor Details

    • ConstructorInstantiator

      public ConstructorInstantiator(boolean hasOuterClassInstance, Object... constructorArgs)
  • Method Details

    • newInstance

      public <T> T newInstance(Class<T> cls)
      Description copied from interface: Instantiator
      Creates instance of given class
      Specified by:
      newInstance in interface Instantiator
    • withParams

      private <T> T withParams(Class<T> cls, Object... params)
    • invokeConstructor

      private static <T> T invokeConstructor(Constructor<?> constructor, Object... params) throws InstantiationException, IllegalAccessException, InvocationTargetException
      Throws:
      InstantiationException
      IllegalAccessException
      InvocationTargetException
    • paramsException

      private InstantiationException paramsException(Class<?> cls, Exception e)
    • constructorArgTypes

      private String constructorArgTypes()
    • noMatchingConstructor

      private InstantiationException noMatchingConstructor(Class<?> cls)
    • constructorArgsString

      private String constructorArgsString()
    • multipleMatchingConstructors

      private InstantiationException multipleMatchingConstructors(Class<?> cls, List<Constructor<?>> constructors)
    • paramsMatch

      private static boolean paramsMatch(Class<?>[] types, Object[] params)
    • evaluateConstructor

      private void evaluateConstructor(List<Constructor<?>> matchingConstructors, Constructor<?> constructor)
      Evalutes constructor against the currently found matchingConstructors and determines if it's a better match to the given arguments, a worse match, or an equivalently good match.

      This method tries to emulate the behavior specified in JLS 15.12.2. Compile-Time Step 2: Determine Method Signature. A constructor X is deemed to be a better match than constructor Y to the given argument list if they are both applicable, constructor X has at least one parameter than is more specific than the corresponding parameter of constructor Y, and constructor Y has no parameter than is more specific than the corresponding parameter in constructor X.

      If constructor is a better match than the constructors in the matchingConstructors list, the list is cleared, and it's added to the list as a singular best matching constructor (so far).
      If constructor is an equivalently good of a match as the constructors in the matchingConstructors list, it's added to the list.
      If constructor is a worse match than the constructors in the matchingConstructors list, the list will remain unchanged.

      Parameters:
      matchingConstructors - A list of equivalently best matching constructors found so far
      constructor - The constructor to be evaluated against this list