use of org.powermock.reflect.exceptions.TooManyMethodsFoundException in project powermock by powermock.
the class MethodProxy method proxy.
/**
* Add a proxy for a method declared in class {@code declaringClass}.
* Each call to the method will be routed to the invocationHandler instead.
*/
public static void proxy(Class<?> declaringClass, String methodName, InvocationHandler invocationHandler) {
assertInvocationHandlerNotNull(invocationHandler);
if (declaringClass == null) {
throw new IllegalArgumentException("declaringClass cannot be null");
}
if (methodName == null || methodName.length() == 0) {
throw new IllegalArgumentException("methodName cannot be empty");
}
Method[] methods = Whitebox.getMethods(declaringClass, methodName);
if (methods.length == 0) {
throw new MethodNotFoundException(String.format("Couldn't find a method with name %s in the class hierarchy of %s", methodName, declaringClass.getName()));
} else if (methods.length > 1) {
throw new TooManyMethodsFoundException(String.format("Found %d methods with name %s in the class hierarchy of %s.", methods.length, methodName, declaringClass.getName()));
}
MockRepository.putMethodProxy(methods[0], invocationHandler);
}
use of org.powermock.reflect.exceptions.TooManyMethodsFoundException in project powermock by powermock.
the class WhiteboxImpl method throwExceptionWhenMultipleMethodMatchesFound.
/**
* Throw exception when multiple method matches found.
*
* @param helpInfo the help info
* @param methods the methods
*/
static void throwExceptionWhenMultipleMethodMatchesFound(String helpInfo, Method[] methods) {
if (methods == null || methods.length < 2) {
throw new IllegalArgumentException("Internal error: throwExceptionWhenMultipleMethodMatchesFound needs at least two methods.");
}
StringBuilder sb = new StringBuilder();
sb.append("Several matching methods found, please specify the ");
sb.append(helpInfo);
sb.append(" so that PowerMock can determine which method you're referring to.\n");
sb.append("Matching methods in class ").append(methods[0].getDeclaringClass().getName()).append(" were:\n");
for (Method method : methods) {
sb.append(method.getReturnType().getName()).append(" ");
sb.append(method.getName()).append("( ");
final Class<?>[] parameterTypes = method.getParameterTypes();
for (Class<?> paramType : parameterTypes) {
sb.append(paramType.getName()).append(".class ");
}
sb.append(")\n");
}
throw new TooManyMethodsFoundException(sb.toString());
}
use of org.powermock.reflect.exceptions.TooManyMethodsFoundException in project powermock by powermock.
the class Stubber method stubMethod.
/**
* Add a method that should be intercepted and return another value (
* {@code returnObject}) (i.e. the method is stubbed).
*/
public static void stubMethod(Class<?> declaringClass, String methodName, Object returnObject) {
if (declaringClass == null) {
throw new IllegalArgumentException("declaringClass cannot be null");
}
if (methodName == null || methodName.length() == 0) {
throw new IllegalArgumentException("methodName cannot be empty");
}
Method[] methods = Whitebox.getMethods(declaringClass, methodName);
if (methods.length == 0) {
throw new MethodNotFoundException(String.format("Couldn't find a method with name %s in the class hierarchy of %s", methodName, declaringClass.getName()));
} else if (methods.length > 1) {
throw new TooManyMethodsFoundException(String.format("Found %d methods with name %s in the class hierarchy of %s.", methods.length, methodName, declaringClass.getName()));
}
MockRepository.putMethodToStub(methods[0], returnObject);
}
Aggregations