/** * Checks method for @JsonRpcParam annotations and returns named parameters. * * @param method the method * @param arguments the arguments * @return named parameters or empty if no annotations found * @throws IllegalArgumentException if some parameters are annotated and others not */ private static Map<String, Object> getNamedParameters(Method method, Object[] arguments) { Map<String, Object> namedParams = new LinkedHashMap<>(); Annotation[][] paramAnnotations = method.getParameterAnnotations(); for (int i = 0; i < paramAnnotations.length; i++) { Annotation[] ann = paramAnnotations[i]; for (Annotation an : ann) { if (JsonRpcParam.class.isInstance(an)) { JsonRpcParam jAnn = (JsonRpcParam) an; namedParams.put(jAnn.value(), arguments[i]); break; } } } if (arguments != null && arguments.length > 0 && namedParams.size() > 0 && namedParams.size() != arguments.length) { throw new IllegalArgumentException("JsonRpcParam annotations were not found for all parameters on method " + method.getName()); } return namedParams; }
private void collectArgumentsBasedOnName(Method method, Set<String> paramNames, List<JsonRpcParam> allNames, ObjectNode paramNodes) { Class<?>[] types = method.getParameterTypes(); int numParameters = types.length; for (int i = 0; i < numParameters; i++) { JsonRpcParam param = allNames.get(i); if (param != null && paramNames.contains(param.value())) { if (types[i].isArray() && method.isVarArgs() && numParameters == 1) { collectVarargsFromNode(paramNodes.get(param.value())); } else { addArgument(paramNodes.get(param.value())); } } else { addArgument(NullNode.getInstance()); } } }
public ParameterCount(Set<String> paramNames, ObjectNode paramNodes, List<Class<?>> parameterTypes, Method method) { this.allNames = getAnnotatedParameterNames(method); this.method = method; int typeCount = 0; int nameCount = 0; int at = 0; for (JsonRpcParam name : this.allNames) { if (missingAnnotation(name)) { continue; } String paramName = name.value(); boolean hasParamName = paramNames.contains(paramName); if (hasParamName) { nameCount += 1; } if (hasParamName && isMatchingType(paramNodes.get(paramName), parameterTypes.get(at))) { typeCount += 1; } at += 1; } this.typeCount = typeCount; this.nameCount = nameCount; }