Class TreeFinder

java.lang.Object
com.sun.source.util.TreeScanner<Void,List<Insertion>>
org.checkerframework.afu.annotator.find.TreeFinder
All Implemented Interfaces:
TreeVisitor<Void,List<Insertion>>

public class TreeFinder extends TreeScanner<Void,List<Insertion>>
A TreeScanner that is able to locate program elements in an AST based on Criteria. getInsertionsByPosition(JCTree.JCCompilationUnit,List) scans a tree and creates (in field insertions) a mapping of source positions (as character offsets) to insertion text.
  • Field Details

    • dbug

      public static final DebugWriter dbug
      Debugging logger.
    • warn

      public static final DebugWriter warn
      Warning logger.
  • Constructor Details

    • TreeFinder

      public TreeFinder(com.sun.tools.javac.tree.JCTree.JCCompilationUnit tree)
      Creates a TreeFinder from a source tree.
      Parameters:
      tree - the source tree to search
  • Method Details

    • largestContainingArray

      public static TreePath largestContainingArray(TreePath p)
      If this code location is not an array type, return null. Otherwise, starting at an array type, walk up the AST as long as still an array, and stop at the largest containing array (with nothing but arrays in between).
      Parameters:
      p - a tree path
      Returns:
      a path to the largest containing array, or null if none
    • getPath

      public TreePath getPath(Tree target)
      An alternative to TreePath.getPath(CompilationUnitTree,Tree) that caches its results.
    • scan

      public Void scan(Tree node, List<Insertion> p)
      Scans this tree, using the list of insertions to generate the source position to insertion text mapping. Insertions are removed from the list when positions are found for them. Thus, they are inserted at the first location (the leftmost highest in the tree) that they match.

      When a match is found, this routine removes the insertion from p and adds it to the insertions map as a value, with a key that is a pair. On return, p contains only the insertions for which no match was found.

      Overrides:
      scan in class TreeScanner<Void,List<Insertion>>
      Parameters:
      node - AST node being considered for annotation insertions
      p - list of insertions not yet placed
    • reportInsertionError

      public static void reportInsertionError(Insertion i, Throwable e)
      Reports an error inserting an insertion to System.err.
      Parameters:
      i - the insertion that caused the error
      e - the error. If there's a message it will be printed.
    • getPaths

      public com.google.common.collect.SetMultimap<ASTRecord,Insertion> getPaths()
    • getInsertionsByPosition

      public com.google.common.collect.SetMultimap<org.plumelib.util.IPair<Integer,ASTPath>,Insertion> getInsertionsByPosition(com.sun.tools.javac.tree.JCTree.JCCompilationUnit node, List<Insertion> p)
      Scans the given tree with the given insertion list and returns the mapping from source position to insertion text. The positions are sorted in decreasing order of index, so that inserting one doesn't throw off the index for a subsequent one.

      N.B.: This method calls scan() internally.

      Parameters:
      node - the tree to scan
      p - the list of insertion criteria
      Returns:
      the source position to insertion text mapping
    • getPositions

      public com.google.common.collect.SetMultimap<org.plumelib.util.IPair<Integer,ASTPath>,Insertion> getPositions(com.sun.tools.javac.tree.JCTree.JCCompilationUnit node, Insertions insertions)
      Scans the given tree with the given Insertions and returns the mapping from source position to insertion text.

      N.B.: This method calls scan() internally.

      Parameters:
      node - the tree to scan
      insertions - the insertion criteria
      Returns:
      the source position to insertion text mapping
    • toString

      public static String toString(TreePath path)
      Returns a printed representation of a TreePath.
      Parameters:
      path - a TreePath
      Returns:
      a printed representation of the given TreePath
    • leafToStringTruncated

      public static String leafToStringTruncated(TreePath path, int length)
      Returns a string representation of the leaf of the given path, using toStringTruncated(com.sun.source.tree.Tree, int).
      Parameters:
      path - a path
      length - the maximum length for the result; must be at least 6
      Returns:
      a one-line string representation of the leaf of the given path that is no longer than length characters long
    • toStringOneLine

      public static String toStringOneLine(Tree tree)
      Returns toString(), but without line separators.
      Parameters:
      tree - a tree
      Returns:
      a one-line string representation of the tree
    • toStringTruncated

      public static String toStringTruncated(Tree tree, int length)
      Returns either toStringOneLine(com.sun.source.tree.Tree) if it is no more than length characters, or toStringOneLine(com.sun.source.tree.Tree) quoted and truncated.
      Parameters:
      tree - a tree
      length - the maximum length for the result; must be at least 6
      Returns:
      a one-line string representation of the tree that is no longer than length characters long