Search in sources :

Example 1 with CompleterValues

use of org.apache.karaf.shell.commands.CompleterValues in project karaf by apache.

the class ArgumentCompleter method getCompleterValues.

private Map<Integer, Object> getCompleterValues(CommandWithAction function) {
    final Map<Integer, Object> values = new HashMap<>();
    Action action = null;
    try {
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Method method : type.getDeclaredMethods()) {
                CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
                if (completerMethod != null) {
                    int index = completerMethod.index();
                    Integer key = index;
                    if (index >= arguments.size() || index < 0) {
                        LOGGER.warn("Index out of range on @CompleterValues on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else if (values.containsKey(key)) {
                        LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else {
                        try {
                            Object value;
                            if (Modifier.isStatic(method.getModifiers())) {
                                value = method.invoke(null);
                            } else {
                                if (action == null) {
                                    action = function.createNewAction();
                                }
                                value = method.invoke(action);
                            }
                            values.put(key, value);
                        } catch (IllegalAccessException e) {
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
                        } catch (InvocationTargetException e) {
                            Throwable target = e.getTargetException();
                            if (target == null) {
                                target = e;
                            }
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
                        }
                    }
                }
            }
        }
    } finally {
        if (action != null) {
            try {
                function.releaseAction(action);
            } catch (Exception e) {
                LOGGER.warn("Failed to release action: " + action + ". " + e, e);
            }
        }
    }
    return values;
}
Also used : CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Action(org.apache.felix.gogo.commands.Action) CompleterValues(org.apache.karaf.shell.commands.CompleterValues) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with CompleterValues

use of org.apache.karaf.shell.commands.CompleterValues in project karaf by apache.

the class ArgumentCompleter method getCompleterValues.

private Map<Integer, Object> getCompleterValues(CommandWithAction function) {
    final Map<Integer, Object> values = new HashMap<>();
    Action action = null;
    try {
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Method method : type.getDeclaredMethods()) {
                CompleterValues completerMethod = method.getAnnotation(CompleterValues.class);
                if (completerMethod != null) {
                    int index = completerMethod.index();
                    Integer key = index;
                    if (index >= arguments.size() || index < 0) {
                        LOGGER.warn("Index out of range on @CompleterValues on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else if (values.containsKey(key)) {
                        LOGGER.warn("Duplicate @CompleterMethod annotations on class " + type.getName() + " for index: " + key + " see: " + method);
                    } else {
                        try {
                            Object value;
                            if (Modifier.isStatic(method.getModifiers())) {
                                value = method.invoke(null);
                            } else {
                                if (action == null) {
                                    action = function.createNewAction();
                                }
                                value = method.invoke(action);
                            }
                            values.put(key, value);
                        } catch (IllegalAccessException e) {
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + e, e);
                        } catch (InvocationTargetException e) {
                            Throwable target = e.getTargetException();
                            if (target == null) {
                                target = e;
                            }
                            LOGGER.warn("Could not invoke @CompleterMethod on " + function + ". " + target, target);
                        }
                    }
                }
            }
        }
    } finally {
        if (action != null) {
            try {
                function.releaseAction(action);
            } catch (Exception e) {
                LOGGER.warn("Failed to release action: " + action + ". " + e, e);
            }
        }
    }
    return values;
}
Also used : CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Action(org.apache.felix.gogo.commands.Action) CompleterValues(org.apache.karaf.shell.commands.CompleterValues) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 Action (org.apache.felix.gogo.commands.Action)2 CommandWithAction (org.apache.karaf.shell.commands.CommandWithAction)2 CompleterValues (org.apache.karaf.shell.commands.CompleterValues)2