/** * Get whether this parameter consumes non-flag arguments. * * @return true if this parameter consumes non-flag arguments */ boolean isNonFlagConsumer() { return getBinding().getBehavior(this) != BindingBehavior.PROVIDES && !isValueFlag(); }
/** * Match a {@link BindingMatch} according to the given parameter. * * @param parameter the parameter * @return a binding */ private BoundMethod match(ParameterData parameter) { for (BoundMethod binding : bindings) { Annotation classifer = parameter.getClassifier(); Type type = parameter.getType(); if (binding.classifier != null) { if (classifer != null && classifer.annotationType().equals(binding.classifier)) { if (binding.type == null || binding.type.equals(type)) { return binding; } } } else if (binding.type.equals(type)) { return binding; } } throw new RuntimeException("Unknown type"); }
if (parameter.isOptional()) { if (parameter.getFlag() != null) { return !parameter.isValueFlag() || context.hasFlag(parameter.getFlag()); } else { int numberFree = context.argsLength() - scoped.position(); for (int j = i; j < parameters.length; j++) { if (parameters[j].isNonFlagConsumer() && !parameters[j].isOptional()) { numberFree -= parameters[j].getConsumedCount();
ParameterData parameter = new ParameterData(); parameter.setType(type); parameter.setModifiers(annotations[i]); parameter.setFlag(((Switch) annotation).value(), type != boolean.class); } else if (annotation instanceof Optional) { parameter.setOptional(true); String[] value = ((Optional) annotation).value(); if (value.length > 0) { parameter.setDefaultValue(value); } else if (parameter.getBinding() == null) { parameter.setBinding(builder.getBindings().get(annotation.annotationType())); parameter.setClassifier(annotation); parameter.setName(names.length > 0 ? names[i] : generateName(type, parameter.getClassifier(), i)); if (parameter.isValueFlag()) { valueFlags.add(parameter.getFlag()); if (parameter.getBinding() == null) { parameter.setBinding(builder.getBindings().get(type)); if (parameter.getBinding() == null) { throw new ParametricException("Don't know how to handle the parameter type '" + type + "' in\n" + method.toGenericString()); parameter.validate(method, i + 1); if (parameter.isOptional() && parameter.getFlag() == null) {
args.add(parameter.getClassifier()); args.add(parameter.getModifiers()); } catch (IllegalArgumentException e) { throw new RuntimeException( "Processing of classifier " + parameter.getClassifier() + " and type " + parameter.getType() + " failed for method\n" + binding.method + "\nbecause the parameters for that method are wrong", e); } catch (IllegalAccessException e) {
args[i] = parameter.getBinding().bind(parameter, usedArguments, false); } catch (MissingParameterException e) { if (!parameter.isOptional()) { throw e; } catch (ParameterException e) { assert parameter != null; String name = parameter.getName();
/** * Get the default value for a parameter. * * @param i the index of the parameter * @param scoped the scoped context * @return a value * @throws ParameterException on an error * @throws CommandException on an error */ private Object getDefaultValue(int i, ContextArgumentStack scoped) throws ParameterException, CommandException, InvocationTargetException { CommandContext context = scoped.getContext(); ParameterData parameter = parameters[i]; String[] defaultValue = parameter.getDefaultValue(); if (defaultValue != null) { try { return parameter.getBinding().bind(parameter, new StringArgumentStack(context, defaultValue, false), false); } catch (MissingParameterException e) { throw new ParametricException( "The default value of the parameter using the binding " + parameter.getBinding().getClass() + " in the method\n" + method.toGenericString() + "\nis invalid"); } } return null; }
/** * Get whether this parameter is entered by the user. * * @return true if this parameter is entered by the user. */ boolean isUserInput() { return getBinding().getBehavior(this) != BindingBehavior.PROVIDES; }
Character paramFlag = parameter.getFlag(); if (paramFlag != null && flag == paramFlag) { found = true;
/** * Return the number of arguments this binding consumes. * * @return -1 if unknown or unavailable */ int getConsumedCount() { return getBinding().getConsumedCount(this); }
BindingBehavior behavior = getBinding().getBehavior(this); boolean indeterminate = (behavior == BindingBehavior.INDETERMINATE); if (!isValueFlag() && indeterminate) { throw new ParametricException( "@Switch missing for indeterminate consumer\n\n" + "Notably:\nFor the type " + type + ", the binding " + getBinding().getClass().getCanonicalName() + "\nmay or may not consume parameters (isIndeterminateConsumer(" + type + ") = true)" + "\nand therefore @Switch(flag) is required for parameter #" + parameterIndex + " of \n" + throw new ParametricException( "getConsumedCount() does not return -1 for binding " + getBinding().getClass().getCanonicalName() + "\neven though its behavior type is " + behavior.name() + "\nfor parameter #" + parameterIndex + " of \n" + throw new ParametricException( "getConsumedCount() must not return 0 for binding " + getBinding().getClass().getCanonicalName() + "\nwhen its behavior type is " + behavior.name() + " and not PROVIDES " + "\nfor parameter #" + parameterIndex + " of \n" +
@Override public void updateDescription(Object object, Method method, ParameterData[] parameters, SimpleDescription description) { Command annotation = method.getAnnotation(Command.class); // Handle the case for old commands where no usage is set and all of its // parameters are provider bindings, so its usage information would // be blank and would imply that there were no accepted parameters if (annotation != null && annotation.usage().isEmpty() && (annotation.min() > 0 || annotation.max() > 0)) { boolean hasUserParameters = false; for (ParameterData parameter : parameters) { if (parameter.getBinding().getBehavior(parameter) != BindingBehavior.PROVIDES) { hasUserParameters = true; break; } } if (!hasUserParameters) { description.overrideUsage("(unknown usage information)"); } } }