Class ObjectExplorer


  • public final class ObjectExplorer
    extends Object
    A depth-first object graph explorer. The traversal starts at a root (an Object) and explores any other reachable object (recursively) or primitive value, excluding static fields from the traversal. The traversal is controlled by a user-supplied ObjectVisitor, which decides for each explored path whether to continue exploration of that path, and it can also return a value at the end of the traversal.
    • Method Detail

      • exploreObject

        public static <T> T exploreObject​(Object rootObject,
                                          ObjectVisitor<T> visitor)
        Explores an object graph (defined by a root object and whatever is reachable through it, following non-static fields) while using an ObjectVisitor to both control the traversal and return a value.

        Equivalent to exploreObject(rootObject, visitor, EnumSet.noneOf(Feature.class)).

        Type Parameters:
        T - the type of the value obtained (after the traversal) by the ObjectVisitor
        Parameters:
        rootObject - an object to be recursively explored
        visitor - a visitor that is notified for each explored path and decides whether to continue exploration of that path, and constructs a return value at the end of the exploration
        Returns:
        whatever value is returned by the visitor at the end of the traversal
        See Also:
        ObjectVisitor
      • exploreObject

        public static <T> T exploreObject​(Object rootObject,
                                          ObjectVisitor<T> visitor,
                                          EnumSet<ObjectExplorer.Feature> features)
        Explores an object graph (defined by a root object and whatever is reachable through it, following non-static fields) while using an ObjectVisitor to both control the traversal and return a value.

        The features further customizes the exploration behavior. In particular:

        In both cases above, the return value of ObjectVisitor.visit(Chain) is ignored, since neither primitive values or null can be further explored.
        Type Parameters:
        T - the type of the value obtained (after the traversal) by the ObjectVisitor
        Parameters:
        rootObject - an object to be recursively explored
        visitor - a visitor that is notified for each explored path and decides whether to continue exploration of that path, and constructs a return value at the end of the exploration
        features - a set of desired features that the object exploration should have
        Returns:
        whatever value is returned by the visitor at the end of the traversal
        See Also:
        ObjectVisitor