private <P> P createProxy(Class<P> paramInterface, String... args) { Map<String, MethodParamPair> params = new HashMap<String, MethodParamPair>(); paramMap.clear(); convertedParamMap.clear(); wantHelp = false; for (Method method : paramInterface.getDeclaredMethods()) { Option option = ReflectionUtils.getOptionAnnotation(method); String name = getParameterName(method, option); if (name.length() > maxParamLength) { maxParamLength = name.length(); } String description = option.description(); String defaultValue = option.defaultValue(); if (defaultValue.equals(Option.OPTIONAL)) { defaultValue = null; } Class<?> returnType = ReflectionUtils.getBoxedClass(method.getReturnType()); Param param = new Param(name, description, defaultValue, returnType); paramMap.put(name, param); MethodParamPair pair = new MethodParamPair(method, param); params.put(name, pair); } Map<Method, Object> valuesMap = convert(params, args); for (Map.Entry<Method, Object> entry : valuesMap.entrySet()) { Method method = entry.getKey(); Option option = ReflectionUtils.getOptionAnnotation(method); String name = getParameterName(method, option); convertedParamMap.put(name, entry.getValue()); } if (wantHelp) { displayUsage(); System.exit(0); } ParamInvocationHandler invocationHandler = new ParamInvocationHandler(valuesMap); return (P) Proxy.newProxyInstance( paramInterface.getClassLoader(), new Class<?>[] {paramInterface}, invocationHandler); }
private <P> String getParameterName(Method method, Option option) { return option.name().length() == 0 ? ReflectionUtils.getParamName(method) : option.name(); }