package be.selckin.swu.events;

import be.selckin.swu.events.AnnotationEventDispatcher.EventException;

import com.google.common.base.Preconditions;
import org.apache.wicket.event.IEvent;

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

public class PayloadAndEventCaller implements EventCaller {
    private final Method method;

    public PayloadAndEventCaller(Method method) {
        this.method = Preconditions.checkNotNull(method);
    }

    @Override
    public void call(Object obj, IEvent<?> event) {
        try {
            method.invoke(obj, event.getPayload(), event);
        } catch (IllegalAccessException e) {
            throw new EventException("Failed to call payload & event @OnEvent method " + method + " on " + obj, e);
        } catch (InvocationTargetException e) {
            throw new EventException("Failed to call payload & event @OnEvent method " + method + " on " + obj, e);
        }
    }
}
