public void throwExceptionWhenMultipleConstructorMatchesFound(java.lang.reflect.Constructor[] constructors) { if (constructors == null || constructors.length < 2) { throw new IllegalArgumentException("Internal error: throwExceptionWhenMultipleConstructorMatchesFound needs at least two constructors."); } StringBuilder sb = new StringBuilder(); sb.append("Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're referring to.\n"); sb.append("Matching constructors in class ").append(constructors[0].getDeclaringClass().getName()) .append(" were:\n"); for (java.lang.reflect.Constructor constructor : constructors) { sb.append(constructor.getName()).append("( "); final Class<?>[] parameterTypes = constructor.getParameterTypes(); for (Class<?> paramType : parameterTypes) { sb.append(paramType.getName()).append(".class "); } sb.append(")\n"); } throw new TooManyConstructorsFoundException(sb.toString()); }
/** * Throw exception when multiple constructor matches found. * * @param constructors * the constructors */ static void throwExceptionWhenMultipleConstructorMatchesFound(Constructor<?>[] constructors) { if (constructors == null || constructors.length < 2) { throw new IllegalArgumentException( "Internal error: throwExceptionWhenMultipleConstructorMatchesFound needs at least two constructors."); } StringBuilder sb = new StringBuilder(); sb.append("Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're refering to.\n"); sb.append("Matching constructors in class ").append(constructors[0].getDeclaringClass().getName()) .append(" were:\n"); for (Constructor<?> constructor : constructors) { sb.append(constructor.getName()).append("( "); final Class<?>[] parameterTypes = constructor.getParameterTypes(); for (Class<?> paramType : parameterTypes) { sb.append(paramType.getName()).append(".class "); } sb.append(")\n"); } throw new TooManyConstructorsFoundException(sb.toString()); }
/** * Throw exception when multiple constructor matches found. * * @param constructors the constructors */ static void throwExceptionWhenMultipleConstructorMatchesFound(Constructor<?>[] constructors) { if (constructors == null || constructors.length < 2) { throw new IllegalArgumentException( "Internal error: throwExceptionWhenMultipleConstructorMatchesFound needs at least two constructors."); } StringBuilder sb = new StringBuilder(); sb.append("Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're referring to.\n"); sb.append("Matching constructors in class ").append(constructors[0].getDeclaringClass().getName()) .append(" were:\n"); for (Constructor<?> constructor : constructors) { sb.append(constructor.getName()).append("( "); final Class<?>[] parameterTypes = constructor.getParameterTypes(); for (Class<?> paramType : parameterTypes) { sb.append(paramType.getName()).append(".class "); } sb.append(")\n"); } throw new TooManyConstructorsFoundException(sb.toString()); }
constructor = potentialContstructorPrimitive; } else { throw new TooManyConstructorsFoundException( "Could not determine which constructor to execute. Please specify the parameter types by hand.");
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; }