Class TreeParser


  • public class TreeParser
    extends java.lang.Object
    A utility class for parsing Java expression snippets, and converting them to proper Javac AST nodes.

    This is useful for parsing EnsuresNonNull*, and KeyFor values.

    Currently, it handles four tree types only:

    • Identifier tree (e.g. id)
    • Literal tree (e.g. 2, 3)
    • Method invocation tree (e.g. method(2, 3))
    • Member select tree (e.g. Class.field, instance.method())
    • Array access tree (e.g. array[id])
    Notable limitation: Doesn't handle spaces, or non-method-argument parenthesis.

    It's implemented via a Recursive-Descend parser.

    • Constructor Summary

      Constructors 
      Constructor Description
      TreeParser​(javax.annotation.processing.ProcessingEnvironment env)
      Create a TreeParser.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.sun.source.tree.ExpressionTree parseTree​(java.lang.String s)
      Parses the snippet in the string as an internal Javac AST expression node.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TreeParser

        public TreeParser​(javax.annotation.processing.ProcessingEnvironment env)
        Create a TreeParser.
    • Method Detail

      • parseTree

        public com.sun.source.tree.ExpressionTree parseTree​(java.lang.String s)
        Parses the snippet in the string as an internal Javac AST expression node.
        Parameters:
        s - the Java snippet
        Returns:
        the AST corresponding to the snippet