/** * This is an amazing little recursive method. It walks a fanout of * nested collection to pull out the leaf nodes * * @param o o * @param propName property name * @param index index * @param path path * @return value which could be a collection */ private static Object getCollectionProp(Object o, String propName, int index, String[] path ) { o = _getFieldValuesFromCollectionOrArray(o, propName); if ( index + 1 == path.length ) { return o; } else { index++; return getCollectionProp(o, path[index], index, path); } }
/** * This is an amazing little recursive method. It walks a fanout of * nested collection to pull out the leaf nodes * * @param o o * @param propName property name * @param index index * @param path path * @return value which could be a collection */ private static Object getCollectionProp(Object o, String propName, int index, String[] path ) { o = _getFieldValuesFromCollectionOrArray(o, propName); if ( index + 1 == path.length ) { return o; } else { index++; return getCollectionProp(o, path[index], index, path); } }
/** * This method handles walking lists of lists. * * @param item item * @param path path * @return value at this path */ public static Object getPropByPath( Object item, String... path ) { Object o = item; for ( int index = 0; index < path.length; index++ ) { String propName = path[ index ]; if ( o == null ) { return null; } else if ( o.getClass().isArray() || o instanceof Collection ) { o = getCollectionProp(o, propName, index, path); break; } else { o = getProp( o, propName ); } } return Conversions.unifyListOrArray(o); }
/** * This method handles walking lists of lists. * * @param item item * @param path path * @return value at this path */ public static Object getPropByPath( Object item, String... path ) { Object o = item; for ( int index = 0; index < path.length; index++ ) { String propName = path[ index ]; if ( o == null ) { return null; } else if ( o.getClass().isArray() || o instanceof Collection ) { o = getCollectionProp(o, propName, index, path); break; } else { o = getProp( o, propName ); } } return Conversions.unifyListOrArray(o); }