/** Builds query parameter line by figuring out what should be submitted */ private StringBuilder buildParameterList(Method method, StringBuilder query) { for (Parameter p : ReflectionUtils.getParameters(method)) { QueryParameter qp = p.annotation(QueryParameter.class); if (qp != null) { String name = qp.value(); if (name.length() == 0) name = p.name(); if (name == null || name.length() == 0) continue; // unknown parameter name. we'll report the error when the form is submitted. RelativePath rp = p.annotation(RelativePath.class); if (rp != null) name = rp.value() + '/' + name; if (query.length() == 0) query.append("+qs(this)"); if (name.equals("value")) { // The special 'value' parameter binds to the the current field query.append(".addThis()"); } else { query.append(".nearBy('" + name + "')"); } continue; } Method m = ReflectionUtils.getPublicMethodNamed(p.type(), "fromStapler"); if (m != null) buildParameterList(m, query); } return query; }
/** * Converts a POJO to a list of query string parameters, does not support nesting. * * @param object to convert to query string parameters * @return A list of NameValuePair each representing a different parameter * @throws IllegalAccessException if SecurityPolicy does not allow getting value using reflection */ public static List<NameValuePair> convertObjectToParameters(Object object) throws IllegalAccessException { List<NameValuePair> parameters = new ArrayList<>(); Field[] fields = object.getClass().getDeclaredFields(); for (Field f : fields) { QueryParameter annot = f.getAnnotation(QueryParameter.class); if (annot != null) { f.setAccessible(true); Object fieldValue = f.get(object); // skip if null if (fieldValue == null) { continue; } // if array add multiple parameters if (fieldValue.getClass().isArray()) { for (int i = 0; i < Array.getLength(fieldValue); i++) { parameters.add( new BasicNameValuePair(annot.value(), Array.get(fieldValue, i).toString())); } } else if (fieldValue instanceof Iterable) { // or iterable for (Object element : (Iterable) fieldValue) { parameters.add(new BasicNameValuePair(annot.value(), element.toString())); } } else { // Convert booleans to numbers if (fieldValue instanceof Boolean && ((Boolean) fieldValue).booleanValue()) { parameters.add(new BasicNameValuePair(annot.value(), "1")); } else { parameters.add(new BasicNameValuePair(annot.value(), fieldValue.toString())); } } } } return parameters; }
private List<String> buildFillDependencies(Method method, List<String> depends) { for (Parameter p : ReflectionUtils.getParameters(method)) { QueryParameter qp = p.annotation(QueryParameter.class); if (qp != null) { String name = qp.value(); if (name.length() == 0) name = p.name(); if (name == null || name.length() == 0) continue; // unknown parameter name. we'll report the error when the form is submitted. RelativePath rp = p.annotation(RelativePath.class); if (rp != null) name = rp.value() + '/' + name; depends.add(name); continue; } Method m = ReflectionUtils.getPublicMethodNamed(p.type(), "fromStapler"); if (m != null) buildFillDependencies(m, depends); } return depends; }