Search in sources :

Example 1 with Dispatcher

use of org.cubeengine.butler.Dispatcher in project core by CubeEngine.

the class CubeCommandManager method injectCommands.

public void injectCommands(Injector injector, Object module, List<Field> fields) {
    for (Field field : fields) {
        Class<? extends ContainerCommand> parent = field.getAnnotation(ModuleCommand.class).value();
        Dispatcher dispatcher = parent == ContainerCommand.class ? this : getDispatcher(parent);
        Object command = injector.getInstance(field.getType());
        boolean isCommand = CommandBase.class.isAssignableFrom(command.getClass());
        if (isCommand) {
            dispatcher.addCommand(((CommandBase) command));
            if (command instanceof Dispatcher) {
                this.injectedDispatchers.put(command.getClass(), ((Dispatcher) command));
            }
        } else {
            addCommands(dispatcher, module, command);
        }
        try {
            field.setAccessible(true);
            field.set(module, command);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) CommandBase(org.cubeengine.butler.CommandBase) Dispatcher(org.cubeengine.butler.Dispatcher)

Aggregations

Field (java.lang.reflect.Field)1 CommandBase (org.cubeengine.butler.CommandBase)1 Dispatcher (org.cubeengine.butler.Dispatcher)1