/** * Similar to {@link PropertyUtils#getPropertyClass(Class,String)} but returns the property class for the tail of the given property path. * * @param clazz bean to be accessed * @param propertyPath bean's fieldName * @return property class */ public static Class<?> getPropertyClass(Class<?> clazz, List<String> propertyPath) { Class<?> current = clazz; for(String propertyName : propertyPath){ current = getPropertyClass(current, propertyName); } return current; }
private Class<?> getAttributeType(QuerySpec querySpec, List<String> attributePath) { try { return PropertyUtils.getPropertyClass(querySpec.getResourceClass(), attributePath); } catch (PropertyException e) { if (allowUnknownAttributes) { return String.class; } else { throw e; } } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private Collection<D> getOrCreateCollection(Object source, String fieldName) { Object property = PropertyUtils.getProperty(source, fieldName); if (property == null) { Class<?> propertyClass = PropertyUtils.getPropertyClass(source.getClass(), fieldName); boolean isList = List.class.isAssignableFrom(propertyClass); property = isList ? new ArrayList() : new HashSet(); PropertyUtils.setProperty(source, fieldName, property); } return (Collection<D>) property; }
private static void addMergeInclusions(Deque<String> attributePath, JpaQueryExecutor<?> executor, Class<?> resourceClass) { JpaMergeRelations annotation = resourceClass.getAnnotation(JpaMergeRelations.class); if (annotation != null) { for (String attrName : annotation.attributes()) { attributePath.push(attrName); executor.fetch(new ArrayList<>(attributePath)); // recurse Class attrType = PropertyUtils.getPropertyClass(resourceClass, attrName); addMergeInclusions(attributePath, executor, attrType); attributePath.pop(); } } }
private void applyFilter(QuerySpec querySpec, String parameterName, Set<String> stringValues) { // find operation FilterOperator filterOp = null; String attributePathString = parameterName; for (FilterOperator op : deserializer.getSupportedOperators()) { String opSuffix = "." + op.toString().toLowerCase().replace("_", ""); if (parameterName.toLowerCase().endsWith(opSuffix)) { attributePathString = parameterName.substring(0, parameterName.length() - opSuffix.length()); filterOp = op; break; } } if (filterOp == null) { filterOp = deserializer.getDefaultOperator(); } List<String> attributePath = splitPath(attributePathString); Class<?> attributeType = PropertyUtils.getPropertyClass(querySpec.getResourceClass(), attributePath); Set<Object> typedValues = new HashSet<>(); for (String stringValue : stringValues) { @SuppressWarnings({ "unchecked", "rawtypes" }) Object value = typeParser.parse(stringValue, (Class) attributeType); typedValues.add(value); } Object value = typedValues.size() == 1 ? typedValues.iterator().next() : typedValues; querySpec.addFilter(new FilterSpec(attributePath, filterOp, value)); }