Class LocalVariableScanner

All Implemented Interfaces:
TreeVisitor<Void,Void>

public class LocalVariableScanner extends CommonScanner
LocalVariableScanner stores information about the names and offsets of local variables inside a method, and can also be used to scan the source tree and determine the index of a local variable with a given name, so that the i^th index corresponds to the i^th declaration of a local variable with that name, using 0-based indexing.
  • Method Details

    • indexOfVarTree

      public static int indexOfVarTree(TreePath origpath, Tree varTree, String varName)
      Computes the index i of the given tree along the given tree path such that it is the i^th declaration of the local variable with the given var name, using 0-based indexing.
      Parameters:
      origpath - the source path that ends in varTree
      varTree - the variable tree that declares the local variable
      varName - the name of the local variable
      Returns:
      the index of the variable tree with respect to the given local variable name
    • visitVariable

      public Void visitVariable(VariableTree node, Void p)
      Specified by:
      visitVariable in interface TreeVisitor<Void,Void>
      Overrides:
      visitVariable in class TreeScanner<Void,Void>
    • addToMethodNameIndexMap

      public static void addToMethodNameIndexMap(com.sun.tools.javac.util.Pair<String,com.sun.tools.javac.util.Pair<Integer,Integer>> varInfo, String name)
      Adds the given variable specified as a pair of method name and (index, start-offset) under the given name to the list of all local variables.
      Parameters:
      varInfo - a pair of the method and a pair describing the local variable index and start offset of the local variable
      name - the name of the local variable
    • getFromMethodNameIndexMap

      public static String getFromMethodNameIndexMap(com.sun.tools.javac.util.Pair<String,com.sun.tools.javac.util.Pair<Integer,Integer>> varInfo)
      Gets the name of the local variable in the given method, and at the given index and offset.
      Parameters:
      varInfo - a pair of the method name and a pair of the local variable's index and start offset
      Returns:
      the name of the local variable at the specified location
    • addToMethodNameCounter

      public static void addToMethodNameCounter(String methodName, String varName, Integer offset)
      Adds to the given method the fact that the local variable with the given name is declared at the given start offset.
      Parameters:
      methodName - the method containing the local variable
      varName - the name of the local variable
      offset - the start offset of the local variable
    • getFromMethodNameCounter

      public static List<Integer> getFromMethodNameCounter(String methodName, String varName)
      Returns a list of all start bytecode offsets of variable declarations with the given variable name in the given method.
      Parameters:
      methodName - the name of the method
      varName - the name of the local variable
      Returns:
      a list of start offsets for live ranges of all local variables with the given name in the given method