Search in sources :

Example 1 with ReflectionException

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);
            }
        }
    };
}
Also used : List(java.util.List) ReflectionException(ch.raffael.mddoclet.core.util.ReflectionException) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) ReflectionException(ch.raffael.mddoclet.core.util.ReflectionException) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Parameter(java.lang.reflect.Parameter)

Aggregations

ReflectionException (ch.raffael.mddoclet.core.util.ReflectionException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1