/** * Recursively build list of property paths for bean. * * @param clazz * of bean * @return property paths */ public static List<String> getPropertyPaths(Class<?> clazz) { return addClassPropertyPaths(new ArrayList<String>(), "", clazz); }
private static List<String> addClassPropertyPaths(List<String> ret, String currentPath, Class<?> clazz) { PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { addPropertyPaths(ret, currentPath, propertyDescriptor); } return ret; }
@Test public void testGetPropertyPaths() throws Exception { List<String> propertyPaths = BeanUtil.getPropertyPaths(Person.class); assertEquals("name", propertyPaths.get(0)); }
@Test public void testGetNestedPropertyPaths() throws Exception { List<String> propertyPaths = BeanUtil.getPropertyPaths(Review.class); assertEquals("reviewBody", propertyPaths.get(0)); assertEquals("reviewRating.ratingValue", propertyPaths.get(1)); } }
private static List<String> addClassPropertyPaths(List<String> ret, String currentPath, Class<?> clazz) { PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { addPropertyPaths(ret, currentPath, propertyDescriptor); } return ret; }
/** * Recursively build list of property paths for bean. * * @param clazz * of bean * @return property paths */ public static List<String> getPropertyPaths(Class<?> clazz) { return addClassPropertyPaths(new ArrayList<String>(), "", clazz); }
private static List<String> addPropertyPaths(List<String> ret, String currentPath, PropertyDescriptor propertyDescriptor) { String propertyName = propertyDescriptor.getName(); if ("class".equals(propertyName)) { return Collections.emptyList(); } Class<?> propertyType = propertyDescriptor.getPropertyType(); if (DataType.isSingleValueType(propertyType)) { if (!currentPath.isEmpty()) { currentPath += "."; } ret.add(currentPath + propertyName); } else if (DataType.isArrayOrCollection(propertyType)) { LOG.warn((currentPath.isEmpty() ? currentPath : currentPath + ".") + propertyName + ": property paths for collections not supported yet"); // TODO support for collection properties } else { currentPath += propertyName; addClassPropertyPaths(ret, currentPath, propertyType); } return ret; }
private static List<String> addPropertyPaths(List<String> ret, String currentPath, PropertyDescriptor propertyDescriptor) { String propertyName = propertyDescriptor.getName(); if ("class".equals(propertyName)) { return Collections.emptyList(); } Class<?> propertyType = propertyDescriptor.getPropertyType(); if (DataType.isSingleValueType(propertyType)) { if (!currentPath.isEmpty()) { currentPath += "."; } ret.add(currentPath + propertyName); } else if (DataType.isArrayOrCollection(propertyType)) { LOG.warn((currentPath.isEmpty() ? currentPath : currentPath + ".") + propertyName + ": property paths for collections not supported yet"); // TODO support for collection properties } else { currentPath += propertyName; addClassPropertyPaths(ret, currentPath, propertyType); } return ret; }