Class TreePrinter

  • All Implemented Interfaces:
    javax.annotation.processing.Processor

    @SupportedAnnotationTypes("*")
    @SupportedSourceVersion(RELEASE_8)
    public class TreePrinter
    extends AbstractTypeProcessor
    A utility class for pretty-printing the AST of a program.

    The class is actually an annotation processor; in order to use it, invoke the compiler on the source file(s) for which you wish to view the AST of the program. You may also wish to use the -proc:only javac option to stop compilation after annotation processing. (But, in general -proc:only causes type annotation processors not to be run.)

    A simple main method is also provided. You can invoke this tool as:

    java org.checkerframework.common.util.debug.TreePrinter *.java

    The visitor simply uses the javac Pretty visitor to output a nicely formatted version of the AST.

    TODO: I couldn't find a way to display the result of Pretty, therefore I wrote this simple class. If there already was a way, please let me know.

    TODO: what I really want is something like SignaturePrinter, but for the whole source of the program, that is, for each type in the program use the factory to determine the defaulted annotations on the type.

    See Also:
    TreeDebug
    • Field Summary

      • Fields inherited from class javax.annotation.processing.AbstractProcessor

        processingEnv
    • Constructor Summary

      Constructors 
      Constructor Description
      TreePrinter()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void main​(java.lang.String[] args)  
      void typeProcess​(javax.lang.model.element.TypeElement element, com.sun.source.util.TreePath tree)
      Processes a fully-analyzed class that contains a supported annotation (see AbstractProcessor.getSupportedAnnotationTypes()).
      • Methods inherited from class javax.annotation.processing.AbstractProcessor

        getCompletions, getSupportedAnnotationTypes, getSupportedOptions, getSupportedSourceVersion, isInitialized
      • Methods inherited from class java.lang.Object

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

      • TreePrinter

        public TreePrinter()
    • Method Detail

      • typeProcess

        public void typeProcess​(javax.lang.model.element.TypeElement element,
                                com.sun.source.util.TreePath tree)
        Description copied from class: AbstractTypeProcessor
        Processes a fully-analyzed class that contains a supported annotation (see AbstractProcessor.getSupportedAnnotationTypes()).

        The passed class is always valid type-checked Java code.

        Specified by:
        typeProcess in class AbstractTypeProcessor
        Parameters:
        element - element of the analyzed class
        tree - the tree path to the element, with the leaf being a ClassTree
      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception