Search in sources :

Example 1 with Parameter

use of org.apache.felix.service.command.Parameter in project felix by apache.

the class Basic method help.

@Descriptor("displays information about a specific command")
public void help(@Descriptor("target command") String name) {
    Map<String, List<Method>> commands = getCommands();
    List<Method> methods = null;
    // If the specified command doesn't have a scope, then
    // search for matching methods by ignoring the scope.
    int scopeIdx = name.indexOf(':');
    if (scopeIdx < 0) {
        for (Entry<String, List<Method>> entry : commands.entrySet()) {
            String k = entry.getKey().substring(entry.getKey().indexOf(':') + 1);
            if (name.equals(k)) {
                name = entry.getKey();
                methods = entry.getValue();
                break;
            }
        }
    } else // Otherwise directly look up matching methods.
    {
        methods = commands.get(name);
    }
    if ((methods != null) && (methods.size() > 0)) {
        for (Method m : methods) {
            Descriptor d = m.getAnnotation(Descriptor.class);
            if (d == null) {
                System.out.println("\n" + m.getName());
            } else {
                System.out.println("\n" + m.getName() + " - " + d.value());
            }
            System.out.println("   scope: " + name.substring(0, name.indexOf(':')));
            // Get flags and options.
            Class<?>[] paramTypes = m.getParameterTypes();
            Map<String, Parameter> flags = new TreeMap<String, Parameter>();
            Map<String, String> flagDescs = new TreeMap<String, String>();
            Map<String, Parameter> options = new TreeMap<String, Parameter>();
            Map<String, String> optionDescs = new TreeMap<String, String>();
            List<String> params = new ArrayList<String>();
            Annotation[][] anns = m.getParameterAnnotations();
            for (int paramIdx = 0; paramIdx < anns.length; paramIdx++) {
                Class<?> paramType = m.getParameterTypes()[paramIdx];
                if (paramType == CommandSession.class) {
                    /* Do not bother the user with a CommandSession. */
                    continue;
                }
                Parameter p = findAnnotation(anns[paramIdx], Parameter.class);
                d = findAnnotation(anns[paramIdx], Descriptor.class);
                if (p != null) {
                    if (p.presentValue().equals(Parameter.UNSPECIFIED)) {
                        options.put(p.names()[0], p);
                        if (d != null) {
                            optionDescs.put(p.names()[0], d.value());
                        }
                    } else {
                        flags.put(p.names()[0], p);
                        if (d != null) {
                            flagDescs.put(p.names()[0], d.value());
                        }
                    }
                } else if (d != null) {
                    params.add(paramTypes[paramIdx].getSimpleName());
                    params.add(d.value());
                } else {
                    params.add(paramTypes[paramIdx].getSimpleName());
                    params.add("");
                }
            }
            // Print flags and options.
            if (flags.size() > 0) {
                System.out.println("   flags:");
                for (Entry<String, Parameter> entry : flags.entrySet()) {
                    // Print all aliases.
                    String[] names = entry.getValue().names();
                    System.out.print("      " + names[0]);
                    for (int aliasIdx = 1; aliasIdx < names.length; aliasIdx++) {
                        System.out.print(", " + names[aliasIdx]);
                    }
                    System.out.println("   " + flagDescs.get(entry.getKey()));
                }
            }
            if (options.size() > 0) {
                System.out.println("   options:");
                for (Entry<String, Parameter> entry : options.entrySet()) {
                    // Print all aliases.
                    String[] names = entry.getValue().names();
                    System.out.print("      " + names[0]);
                    for (int aliasIdx = 1; aliasIdx < names.length; aliasIdx++) {
                        System.out.print(", " + names[aliasIdx]);
                    }
                    System.out.println("   " + optionDescs.get(entry.getKey()) + ((entry.getValue().absentValue() == null) ? "" : " [optional]"));
                }
            }
            if (params.size() > 0) {
                System.out.println("   parameters:");
                for (Iterator<String> it = params.iterator(); it.hasNext(); ) {
                    System.out.println("      " + it.next() + "   " + it.next());
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) Descriptor(org.apache.felix.service.command.Descriptor) Parameter(org.apache.felix.service.command.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) Descriptor(org.apache.felix.service.command.Descriptor)

Example 2 with Parameter

use of org.apache.felix.service.command.Parameter in project felix by apache.

the class Shell method help.

@Descriptor("displays information about a specific command")
public void help(CommandSession session, @Descriptor("target command") String name) {
    Map<String, List<Method>> commands = getReflectionCommands(session);
    List<Method> methods = null;
    // If the specified command doesn't have a scope, then
    // search for matching methods by ignoring the scope.
    int scopeIdx = name.indexOf(':');
    if (scopeIdx < 0) {
        for (Entry<String, List<Method>> entry : commands.entrySet()) {
            String k = entry.getKey().substring(entry.getKey().indexOf(':') + 1);
            if (name.equals(k)) {
                name = entry.getKey();
                methods = entry.getValue();
                break;
            }
        }
    } else // Otherwise directly look up matching methods.
    {
        methods = commands.get(name);
    }
    if ((methods != null) && (methods.size() > 0)) {
        for (Method m : methods) {
            Descriptor d = m.getAnnotation(Descriptor.class);
            if (d == null) {
                System.out.println("\n" + m.getName());
            } else {
                System.out.println("\n" + m.getName() + " - " + d.value());
            }
            System.out.println("   scope: " + name.substring(0, name.indexOf(':')));
            // Get flags and options.
            Class<?>[] paramTypes = m.getParameterTypes();
            Map<String, Parameter> flags = new TreeMap<>();
            Map<String, String> flagDescs = new TreeMap<>();
            Map<String, Parameter> options = new TreeMap<>();
            Map<String, String> optionDescs = new TreeMap<>();
            List<String> params = new ArrayList<>();
            Annotation[][] anns = m.getParameterAnnotations();
            for (int paramIdx = 0; paramIdx < anns.length; paramIdx++) {
                Class<?> paramType = m.getParameterTypes()[paramIdx];
                if (paramType == CommandSession.class) {
                    /* Do not bother the user with a CommandSession. */
                    continue;
                }
                Parameter p = findAnnotation(anns[paramIdx], Parameter.class);
                d = findAnnotation(anns[paramIdx], Descriptor.class);
                if (p != null) {
                    if (p.presentValue().equals(Parameter.UNSPECIFIED)) {
                        options.put(p.names()[0], p);
                        if (d != null) {
                            optionDescs.put(p.names()[0], d.value());
                        }
                    } else {
                        flags.put(p.names()[0], p);
                        if (d != null) {
                            flagDescs.put(p.names()[0], d.value());
                        }
                    }
                } else if (d != null) {
                    params.add(paramTypes[paramIdx].getSimpleName());
                    params.add(d.value());
                } else {
                    params.add(paramTypes[paramIdx].getSimpleName());
                    params.add("");
                }
            }
            // Print flags and options.
            if (flags.size() > 0) {
                System.out.println("   flags:");
                for (Entry<String, Parameter> entry : flags.entrySet()) {
                    // Print all aliases.
                    String[] names = entry.getValue().names();
                    System.out.print("      " + names[0]);
                    for (int aliasIdx = 1; aliasIdx < names.length; aliasIdx++) {
                        System.out.print(", " + names[aliasIdx]);
                    }
                    System.out.println("   " + flagDescs.get(entry.getKey()));
                }
            }
            if (options.size() > 0) {
                System.out.println("   options:");
                for (Entry<String, Parameter> entry : options.entrySet()) {
                    // Print all aliases.
                    String[] names = entry.getValue().names();
                    System.out.print("      " + names[0]);
                    for (int aliasIdx = 1; aliasIdx < names.length; aliasIdx++) {
                        System.out.print(", " + names[aliasIdx]);
                    }
                    System.out.println("   " + optionDescs.get(entry.getKey()) + ((entry.getValue().absentValue() == null) ? "" : " [optional]"));
                }
            }
            if (params.size() > 0) {
                System.out.println("   parameters:");
                for (Iterator<String> it = params.iterator(); it.hasNext(); ) {
                    System.out.println("      " + it.next() + "   " + it.next());
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) Descriptor(org.apache.felix.service.command.Descriptor) Parameter(org.apache.felix.service.command.Parameter) List(java.util.List) ArrayList(java.util.ArrayList) Descriptor(org.apache.felix.service.command.Descriptor)

Example 3 with Parameter

use of org.apache.felix.service.command.Parameter in project felix by apache.

the class Reflective method transformParameters.

/**
 * transform name/value parameters into ordered argument list.
 * params: --param2, value2, --flag1, arg3
 * args: true, value2, arg3
 * @return new ordered list of args.
 */
private static List<Object> transformParameters(Method method, List<Object> in) {
    Annotation[][] pas = method.getParameterAnnotations();
    ArrayList<Object> out = new ArrayList<>();
    ArrayList<Object> parms = new ArrayList<>(in);
    for (Annotation[] as : pas) {
        for (Annotation a : as) {
            if (a instanceof Parameter) {
                int i = -1;
                Parameter p = (Parameter) a;
                for (String name : p.names()) {
                    i = parms.indexOf(name);
                    if (i >= 0)
                        break;
                }
                if (i >= 0) {
                    // parameter present
                    parms.remove(i);
                    Object value = p.presentValue();
                    if (Parameter.UNSPECIFIED.equals(value)) {
                        if (i >= parms.size())
                            // missing parameter, so try other methods
                            return null;
                        value = parms.remove(i);
                    }
                    out.add(value);
                } else {
                    out.add(p.absentValue());
                }
            }
        }
    }
    out.addAll(parms);
    return out;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.apache.felix.service.command.Parameter) Annotation(java.lang.annotation.Annotation)

Aggregations

ArrayList (java.util.ArrayList)3 Parameter (org.apache.felix.service.command.Parameter)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 Descriptor (org.apache.felix.service.command.Descriptor)2 Annotation (java.lang.annotation.Annotation)1