Search in sources :

Example 6 with MethodNotFoundException

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;
}
Also used : Method(java.lang.reflect.Method) MethodNotFoundException(org.powermock.reflect.exceptions.MethodNotFoundException) LinkedList(java.util.LinkedList)

Example 7 with MethodNotFoundException

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);
}
Also used : TooManyMethodsFoundException(org.powermock.reflect.exceptions.TooManyMethodsFoundException) Method(java.lang.reflect.Method) MethodNotFoundException(org.powermock.reflect.exceptions.MethodNotFoundException)

Aggregations

Method (java.lang.reflect.Method)7 MethodNotFoundException (org.powermock.reflect.exceptions.MethodNotFoundException)7 LinkedList (java.util.LinkedList)3 TooManyMethodsFoundException (org.powermock.reflect.exceptions.TooManyMethodsFoundException)2 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 FieldNotFoundException (org.powermock.reflect.exceptions.FieldNotFoundException)1