package ru.kirillius.pf.sdn.web.RPC;

import ru.kirillius.pf.sdn.core.Context;

import java.lang.reflect.InvocationTargetException;

/**
 * Marker interface for JSON-RPC handlers with a helper to instantiate them via the application context.
 */
public interface RPC {
    /**
     * Instantiates an RPC handler using its context-aware constructor.
     */
    static <T extends RPC> T instantiate(Class<T> type, Context context) {
        try {
            return type.getConstructor(Context.class).newInstance(context);
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
                 IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
