use of ch.raffael.mddoclet.core.util.ReflectionException in project markdown-doclet by Abnaxos.
the class ReflectedOptions method processorForMethod.
public static OptionProcessor processorForMethod(Object target, Method method) {
List<ArgumentConverter<?>> converters = new ArrayList<>(method.getParameterCount());
for (Parameter parameter : method.getParameters()) {
ArgumentConverter<?> converter;
OptionConsumer.Converter converterAnnotation = parameter.getAnnotation(OptionConsumer.Converter.class);
if (converterAnnotation != null) {
try {
converter = converterAnnotation.value().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new ReflectionException("Error instantiating converter for parameter " + parameter + " method " + method);
}
} else {
converter = StandardArgumentConverters.forType(parameter.getParameterizedType());
if (converter == null) {
throw new ReflectionException("No argument converter found for parameter " + parameter.getName() + " of " + method);
}
}
converters.add(converter);
}
return (name, arguments) -> {
if (arguments.size() != converters.size()) {
throw new InvalidOptionArgumentsException("Unexpected argument count: " + arguments.size() + "!=" + converters.size() + "(expeted)");
}
Object[] methodArguments = new Object[arguments.size()];
for (int i = 0; i < arguments.size(); i++) {
methodArguments[i] = converters.get(i).convert(arguments.get(i));
}
try {
method.invoke(target, methodArguments);
} catch (IllegalAccessException e) {
throw new ArgumentsProcessingException(e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof InvalidOptionArgumentsException) {
throw (InvalidOptionArgumentsException) e.getTargetException();
} else {
throw new ArgumentsProcessingException(e);
}
}
};
}
Aggregations