Class AbstractToString

All Implemented Interfaces:
BugChecker.BinaryTreeMatcher, BugChecker.CompoundAssignmentTreeMatcher, BugChecker.MethodInvocationTreeMatcher, Suppressible, Serializable
Direct Known Subclasses:
AnnotationMirrorToString, AnnotationValueToString, ArrayToString, LiteProtoToString, ObjectToString, StreamToString, SymbolToString, TreeToString, TypeToString

An abstract matcher for implicit and explicit calls to Object.toString(), for use on types that do not have a human-readable toString() implementation.

See examples in StreamToString and ArrayToString.

See Also:
  • Constructor Details

  • Method Details

    • typePredicate

      protected abstract TypePredicate typePredicate()
      The type to match on.
    • implicitToStringFix

      protected abstract Optional<Fix> implicitToStringFix(com.sun.source.tree.ExpressionTree tree, VisitorState state)
      Constructs a fix for an implicit toString call, e.g. from string concatenation or from passing an argument to println or StringBuilder.append.
      tree - the tree node for the expression being converted to a String
    • descriptionMessageForDefaultMatch

      protected Optional<String> descriptionMessageForDefaultMatch( type, VisitorState state)
      Adds the description message for match on the type without fixes.
    • allowableToStringKind

      protected boolean allowableToStringKind( toStringKind)
      Whether this kind of toString call is allowable for this check.
    • toStringFix

      protected abstract Optional<Fix> toStringFix(com.sun.source.tree.Tree parent, com.sun.source.tree.ExpressionTree expression, VisitorState state)
      Constructs a fix for an explicit toString call, e.g. from Object.toString() or String.valueOf().
      parent - the expression's parent (e.g. String.valueOf(expression))
    • matchMethodInvocation

      public Description matchMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, VisitorState state)
      Specified by:
      matchMethodInvocation in interface BugChecker.MethodInvocationTreeMatcher
    • matchBinary

      public Description matchBinary(com.sun.source.tree.BinaryTree tree, VisitorState state)
      Specified by:
      matchBinary in interface BugChecker.BinaryTreeMatcher
    • matchCompoundAssignment

      public Description matchCompoundAssignment(com.sun.source.tree.CompoundAssignmentTree tree, VisitorState state)
      Specified by:
      matchCompoundAssignment in interface BugChecker.CompoundAssignmentTreeMatcher