All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.thucydides.junit.internals.MethodInvoker Maven / Gradle / Ivy

There is a newer version: 4.2.9
Show newest version
package net.thucydides.junit.internals;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * A centralized way to invoke a method on a parameter.
 * Raises an IllegalArgumentException if something goes wrong.
 */
public class MethodInvoker {

    private final Object target;

    protected MethodInvoker(final Object target) {
        this.target = target;
    }

    public static MethodInvoker on(final Object target) {
        return new MethodInvoker(target);
    }

    public Object run(final Method method, final Object... parameters) {
        try {
            return invokeMethod(method, parameters);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("Could not access method",e);
        } catch (InvocationTargetException e) {
            throw new IllegalArgumentException("Could not invoke method",e);
        }

    }

    protected Object invokeMethod(final Method method, final Object[] parameters)
            throws IllegalAccessException, InvocationTargetException {
        return method.invoke(target, parameters);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy