Search in sources :

Example 1 with TooManyConstructorsFoundException

use of org.powermock.reflect.exceptions.TooManyConstructorsFoundException in project powermock by powermock.

the class WhiteboxImpl method getBestCandidateConstructor.

private static <T> Constructor<T> getBestCandidateConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] argumentTypes, Object[] arguments) {
    Constructor<T> constructor;
    Constructor<T> potentialConstructorWrapped = getPotentialConstructorWrapped(classThatContainsTheConstructorToTest, argumentTypes);
    Constructor<T> potentialConstructorPrimitive = getPotentialConstructorPrimitive(classThatContainsTheConstructorToTest, argumentTypes);
    if (potentialConstructorPrimitive == null && potentialConstructorWrapped == null) {
        // Check if we can find a matching var args constructor.
        constructor = getPotentialVarArgsConstructor(classThatContainsTheConstructorToTest, arguments);
        if (constructor == null) {
            throw new ConstructorNotFoundException("Failed to find a constructor with parameter types: [" + getArgumentTypesAsString(arguments) + "]");
        }
    } else if (potentialConstructorPrimitive == null) {
        constructor = potentialConstructorWrapped;
    } else if (potentialConstructorWrapped == null) {
        constructor = potentialConstructorPrimitive;
    } else if (arguments == null || arguments.length == 0 && potentialConstructorPrimitive != null) {
        constructor = potentialConstructorPrimitive;
    } else {
        throw new TooManyConstructorsFoundException("Could not determine which constructor to execute. Please specify the parameter types by hand.");
    }
    return constructor;
}
Also used : ConstructorNotFoundException(org.powermock.reflect.exceptions.ConstructorNotFoundException) TooManyConstructorsFoundException(org.powermock.reflect.exceptions.TooManyConstructorsFoundException)

Aggregations

ConstructorNotFoundException (org.powermock.reflect.exceptions.ConstructorNotFoundException)1 TooManyConstructorsFoundException (org.powermock.reflect.exceptions.TooManyConstructorsFoundException)1