// Command name completer
commandCompleter = new StringsCompleter(getNames(session, command));
// Build options completer
for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
Option option = field.getAnnotation(Option.class);
if (option != null) {
fields.put(option, field);
options.put(option.name(), option);
String[] aliases = option.aliases();
if (aliases != null) {
for (String alias : aliases) {
options.put(alias, option);
}
}
}
Argument argument = field.getAnnotation(Argument.class);
if (argument != null) {
Integer key = argument.index();
if (arguments.containsKey(key)) {
LOGGER.warn("Duplicate @Argument annotations on class " + type.getName() + " for index: " + key + " see: " + field);
} else {
arguments.put(key, field);
}
}
}
}
options.put(HelpOption.HELP.name(), HelpOption.HELP);
optionsCompleter = new StringsCompleter(options.keySet());
// Build arguments completers
List<Completer> argsCompleters = null;
Map<String, Completer> optionalCompleters = null;
if (function instanceof CompletableFunction) {
Map<String, Completer> focl = ((CompletableFunction) function).getOptionalCompleters();
List<Completer> fcl = ((CompletableFunction) function).getCompleters();
if (focl != null || fcl != null) {
argsCompleters = new ArrayList<Completer>();
if (fcl != null) {
for (Completer c : fcl) {
argsCompleters.add(c == null ? NullCompleter.INSTANCE : c);
}
}
optionalCompleters = focl;
}
}
if (argsCompleters == null) {
final Map<Integer, Object> values = getCompleterValues(function);
argsCompleters = new ArrayList<Completer>();
boolean multi = false;
for (int key = 0; key < arguments.size(); key++) {
Completer completer = null;
Field field = arguments.get(key);
if (field != null) {
Argument argument = field.getAnnotation(Argument.class);
multi = (argument != null && argument.multiValued());
org.apache.karaf.shell.commands.Completer ann = field.getAnnotation(org.apache.karaf.shell.commands.Completer.class);
if (ann != null) {
Class clazz = ann.value();
String[] value = ann.values();
if (clazz != null) {
if (value.length > 0 && clazz == StringsCompleter.class) {
completer = new StringsCompleter(value, ann.caseSensitive());
} else {
BundleContext context = FrameworkUtil.getBundle(function.getClass()).getBundleContext();
completer = new ProxyServiceCompleter(context, clazz);
}
}
} else if (values.containsKey(key)) {
Object value = values.get(key);
if (value instanceof String[]) {
completer = new StringsCompleter((String[]) value);
} else if (value instanceof Collection) {
completer = new StringsCompleter((Collection<String>) value);
} else {
LOGGER.warn("Could not use value " + value + " as set of completions!");
}
} else {
completer = getDefaultCompleter(session, field);
}
}
if (completer == null) {
completer = NullCompleter.INSTANCE;
}
argsCompleters.add(completer);
}
if (argsCompleters.isEmpty() || !multi) {
argsCompleters.add(NullCompleter.INSTANCE);
}
optionalCompleters = new HashMap<String, Completer>();
for (Option option : fields.keySet()) {
Completer completer = null;
Field field = fields.get(option);
if (field != null) {
org.apache.karaf.shell.commands.Completer ann = field.getAnnotation(org.apache.karaf.shell.commands.Completer.class);
if (ann != null) {
Class clazz = ann.value();
String[] value = ann.values();
if (clazz != null) {
if (value.length > 0 && clazz == StringsCompleter.class) {
completer = new StringsCompleter(value, ann.caseSensitive());
} else {
BundleContext context = FrameworkUtil.getBundle(function.getClass()).getBundleContext();
completer = new ProxyServiceCompleter(context, clazz);
}
}
}
}
if (completer == null) {
completer = NullCompleter.INSTANCE;
}
optionalCompleters.put(option.name(), completer);
if (option.aliases() != null) {
for (String alias : option.aliases()) {
optionalCompleters.put(alias, completer);
}
}
}
}