use of org.powermock.reflect.exceptions.ConstructorNotFoundException 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;
}
use of org.powermock.reflect.exceptions.ConstructorNotFoundException in project powermock by powermock.
the class WhiteboxImpl method getConstructor.
/**
* Convenience method to get a (declared) constructor from a class type
* without having to catch the checked exceptions otherwise required. These
* exceptions are wrapped as runtime exceptions. The constructor is also set
* to accessible.
*
* @param type The type of the class where the constructor is located.
* @param parameterTypes All parameter types of the constructor (may be
* {@code null}).
* @return A .
*/
public static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes) {
Class<?> unmockedType = WhiteboxImpl.getOriginalUnmockedType(type);
try {
final Constructor<?> constructor = unmockedType.getDeclaredConstructor(parameterTypes);
constructor.setAccessible(true);
return constructor;
} catch (RuntimeException e) {
throw e;
} catch (Error e) {
throw e;
} catch (Throwable e) {
throw new ConstructorNotFoundException(String.format("Failed to lookup constructor with parameter types [ %s ] in class %s.", getArgumentTypesAsString((Object[]) parameterTypes), unmockedType.getName()), e);
}
}
Aggregations