Class AstTreeStringPrinter


  • public final class AstTreeStringPrinter
    extends java.lang.Object
    Class for printing AST to String.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String LINE_SEPARATOR
      OS specific line separator.
      private static java.util.regex.Pattern NEWLINE
      Newline pattern.
      private static java.util.regex.Pattern RETURN
      Return pattern.
      private static java.util.regex.Pattern TAB
      Tab pattern.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private AstTreeStringPrinter()
      Prevent instances.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.String escapeAllControlChars​(java.lang.String text)
      Replace all control chars with escaped symbols.
      private static java.lang.String getIndentation​(DetailAST ast)
      Get indentation for an AST node.
      private static java.lang.String getNodeInfo​(DetailAST node)
      Get string representation of the node as token name, node text, line number and column number.
      private static java.lang.String parseAndPrintJavadocTree​(DetailAST node)
      Parses block comment as javadoc and prints its tree.
      static java.lang.String printAst​(FileText text, JavaParser.Options options)
      Parse a file and print the parse tree.
      static java.lang.String printBranch​(DetailAST node)
      Print branch info from root down to given node.
      static java.lang.String printFileAst​(java.io.File file, JavaParser.Options options)
      Parse a file and print the parse tree.
      private static java.lang.String printJavaAndJavadocTree​(DetailAST ast)
      Prints full tree (java + comments + javadoc) of the DetailAST.
      static java.lang.String printJavaAndJavadocTree​(java.io.File file)
      Prints full AST (java + comments + javadoc) of the java file.
      private static java.lang.String printTree​(DetailAST ast)
      Print AST.
      • Methods inherited from class java.lang.Object

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

      • NEWLINE

        private static final java.util.regex.Pattern NEWLINE
        Newline pattern.
      • RETURN

        private static final java.util.regex.Pattern RETURN
        Return pattern.
      • TAB

        private static final java.util.regex.Pattern TAB
        Tab pattern.
      • LINE_SEPARATOR

        private static final java.lang.String LINE_SEPARATOR
        OS specific line separator.
    • Method Detail

      • printFileAst

        public static java.lang.String printFileAst​(java.io.File file,
                                                    JavaParser.Options options)
                                             throws java.io.IOException,
                                                    CheckstyleException
        Parse a file and print the parse tree.
        Parameters:
        file - the file to print.
        options - JavaParser.Options to control the inclusion of comment nodes.
        Returns:
        the AST of the file in String form.
        Throws:
        java.io.IOException - if the file could not be read.
        CheckstyleException - if the file is not a Java source.
      • printJavaAndJavadocTree

        public static java.lang.String printJavaAndJavadocTree​(java.io.File file)
                                                        throws java.io.IOException,
                                                               CheckstyleException
        Prints full AST (java + comments + javadoc) of the java file.
        Parameters:
        file - java file
        Returns:
        Full tree
        Throws:
        java.io.IOException - Failed to open a file
        CheckstyleException - error while parsing the file
      • printJavaAndJavadocTree

        private static java.lang.String printJavaAndJavadocTree​(DetailAST ast)
        Prints full tree (java + comments + javadoc) of the DetailAST.
        Parameters:
        ast - root DetailAST
        Returns:
        Full tree
      • parseAndPrintJavadocTree

        private static java.lang.String parseAndPrintJavadocTree​(DetailAST node)
        Parses block comment as javadoc and prints its tree.
        Parameters:
        node - block comment begin
        Returns:
        string javadoc tree
      • printBranch

        public static java.lang.String printBranch​(DetailAST node)
        Print branch info from root down to given node.
        Parameters:
        node - last item of the branch
        Returns:
        branch as string
      • printTree

        private static java.lang.String printTree​(DetailAST ast)
        Print AST.
        Parameters:
        ast - the root AST node.
        Returns:
        string AST.
      • getNodeInfo

        private static java.lang.String getNodeInfo​(DetailAST node)
        Get string representation of the node as token name, node text, line number and column number.
        Parameters:
        node - DetailAST
        Returns:
        node info
      • getIndentation

        private static java.lang.String getIndentation​(DetailAST ast)
        Get indentation for an AST node.
        Parameters:
        ast - the AST to get the indentation for.
        Returns:
        the indentation in String format.
      • escapeAllControlChars

        private static java.lang.String escapeAllControlChars​(java.lang.String text)
        Replace all control chars with escaped symbols.
        Parameters:
        text - the String to process.
        Returns:
        the processed String with all control chars escaped.