Class BeanTraverseSession

Direct Known Subclasses:
JsonSchemaGeneratorSession, SerializerSession

public class BeanTraverseSession extends BeanSession
ContextSession that lives for the duration of a single use of BeanTraverseContext.

Used by serializers and other classes that traverse POJOs for the following purposes:

  • Keeping track of how deep it is in a model for indentation purposes.
  • Ensuring infinite loops don't occur by setting a limit on how deep to traverse a model.
  • Ensuring infinite loops don't occur from loops in the model (when detectRecursions is enabled.
Notes:
  • This class is not thread safe and is typically discarded after one use.
See Also:
  • Field Details

    • indent

      public int indent
      The current indentation depth into the model.
  • Constructor Details

  • Method Details

    • setCurrentProperty

      protected final void setCurrentProperty(BeanPropertyMeta currentProperty)
      Sets the current bean property being traversed for proper error messages.
      Parameters:
      currentProperty - The current property being traversed.
    • setCurrentClass

      protected final void setCurrentClass(ClassMeta<?> currentClass)
      Sets the current class being traversed for proper error messages.
      Parameters:
      currentClass - The current class being traversed.
    • push

      protected final ClassMeta<?> push(String attrName, Object o, ClassMeta<?> eType) throws BeanRecursionException
      Push the specified object onto the stack.
      Parameters:
      attrName - The attribute name.
      o - The current object being traversed.
      eType - The expected class type.
      Returns:
      The ClassMeta of the object so that instanceof operations only need to be performed once (since they can be expensive).
      Throws:
      BeanRecursionException - If recursion occurred.
    • isRoot

      protected final boolean isRoot()
      Returns true if we're processing the root node.

      Must be called after push(String, Object, ClassMeta) and before pop().

      Returns:
      true if we're processing the root node.
    • willRecurse

      protected final boolean willRecurse(String attrName, Object o, ClassMeta<?> cm) throws BeanRecursionException
      Returns true if BeanTraverseContext.Builder.detectRecursions() is enabled, and the specified object is already higher up in the traversal chain.
      Parameters:
      attrName - The bean property attribute name, or some other identifier.
      o - The object to check for recursion.
      cm - The metadata on the object class.
      Returns:
      true if recursion detected.
      Throws:
      BeanRecursionException - If recursion occurred.
    • willExceedDepth

      protected final boolean willExceedDepth()
      Returns true if we're about to exceed the max depth for the document.
      Returns:
      true if we're about to exceed the max depth for the document.
    • pop

      protected final void pop()
      Pop an object off the stack.
    • isOptional

      protected final boolean isOptional(ClassMeta<?> cm)
      Same as ClassMeta.isOptional() but gracefully handles a null ClassMeta.
      Parameters:
      cm - The meta to check.
      Returns:
      true if the specified meta is an Optional.
    • getOptionalType

      protected final ClassMeta<?> getOptionalType(ClassMeta<?> cm)
      Returns the inner type of an Optional.
      Parameters:
      cm - The meta to check.
      Returns:
      The inner type of an Optional.
    • getOptionalValue

      protected final Object getOptionalValue(Object o)
      If the specified object is an Optional, returns the inner object.
      Parameters:
      o - The object to check.
      Returns:
      The inner object if it's an Optional, null if it's null, or else the same object.
    • onError

      protected void onError(Throwable t, String msg, Object... args)
      Logs a warning message.
      Parameters:
      t - The throwable that was thrown (if there was one).
      msg - The warning message.
      args - Optional MessageFormat-style arguments.
    • getStack

      protected String getStack(boolean full)
      Returns the current stack trace.
      Parameters:
      full - If true, returns a full stack trace.
      Returns:
      The current stack trace.
    • getLastLocation

      public final JsonMap getLastLocation()
      Returns information used to determine at what location in the parse a failure occurred.
      Returns:
      A map, typically containing something like {line:123,column:456,currentProperty:"foobar"}
    • isDetectRecursions

      public final boolean isDetectRecursions()
      Automatically detect POJO recursions.
      Returns:
      true if recursions should be checked for during traversal.
      See Also:
    • isIgnoreRecursions

      public final boolean isIgnoreRecursions()
      Ignore recursion errors.
      Returns:
      true if when we encounter the same object when traversing a tree, we set the value to null.
      Otherwise, a BeanRecursionException is thrown with the message "Recursion occurred, stack=...".
      See Also:
    • getInitialDepth

      public final int getInitialDepth()
      Initial depth.
      Returns:
      The initial indentation level at the root.
      See Also:
    • getMaxDepth

      public final int getMaxDepth()
      Max traversal depth.
      Returns:
      The depth at which traversal is aborted if depth is reached in the POJO tree.
      If this depth is exceeded, an exception is thrown.
      See Also:
    • properties

      protected JsonMap properties()
      Description copied from class: ContextSession
      Returns the properties on this bean as a map for debugging.
      Overrides:
      properties in class ContextSession
      Returns:
      The properties on this bean as a map for debugging.