use of org.powermock.reflect.exceptions.MethodNotFoundException in project powermock by powermock.
the class WhiteboxImpl method getMethod.
/**
* Convenience method to get a method from a class type without having to
* catch the checked exceptions otherwise required. These exceptions are
* wrapped as runtime exceptions.
*
* The method will first try to look for a declared method in the same
* class. If the method is not declared in this class it will look for the
* method in the super class. This will continue throughout the whole class
* hierarchy. If the method is not found an {@link MethodNotFoundException}
* is thrown. Since the method name is not specified an
*
* @param type The type of the class where the method is located.
* @param parameterTypes All parameter types of the method (may be {@code null}).
* @return A . {@link TooManyMethodsFoundException} is thrown if two or more
* methods matches the same parameter types in the same class.
*/
public static Method getMethod(Class<?> type, Class<?>... parameterTypes) {
Class<?> thisType = type;
if (parameterTypes == null) {
parameterTypes = new Class<?>[0];
}
List<Method> foundMethods = new LinkedList<Method>();
while (thisType != null) {
Method[] methodsToTraverse = null;
if (thisType.isInterface()) {
// Interfaces only contain public (and abstract) methods, no
// need to traverse the hierarchy.
methodsToTraverse = getAllPublicMethods(thisType);
} else {
methodsToTraverse = thisType.getDeclaredMethods();
}
for (Method method : methodsToTraverse) {
if (checkIfParameterTypesAreSame(method.isVarArgs(), parameterTypes, method.getParameterTypes())) {
foundMethods.add(method);
if (foundMethods.size() == 1) {
method.setAccessible(true);
}
}
}
if (foundMethods.size() == 1) {
return foundMethods.get(0);
} else if (foundMethods.size() > 1) {
break;
}
thisType = thisType.getSuperclass();
}
if (foundMethods.isEmpty()) {
throw new MethodNotFoundException("No method was found with parameter types: [ " + getArgumentTypesAsString((Object[]) parameterTypes) + " ] in class " + getOriginalUnmockedType(type).getName() + ".");
} else {
throwExceptionWhenMultipleMethodMatchesFound("method name", foundMethods.toArray(new Method[foundMethods.size()]));
}
// Will never happen
return null;
}
use of org.powermock.reflect.exceptions.MethodNotFoundException 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