Package com.puppycrawl.tools.checkstyle
Class AstTreeStringPrinter
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AstTreeStringPrinter
-
public final class AstTreeStringPrinter extends java.lang.ObjectClass for printing AST to String.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringLINE_SEPARATOROS specific line separator.private static java.util.regex.PatternNEWLINENewline pattern.private static java.util.regex.PatternRETURNReturn pattern.private static java.util.regex.PatternTABTab pattern.
-
Constructor Summary
Constructors Modifier Constructor Description privateAstTreeStringPrinter()Prevent instances.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.lang.StringescapeAllControlChars(java.lang.String text)Replace all control chars with escaped symbols.private static java.lang.StringgetIndentation(DetailAST ast)Get indentation for an AST node.private static java.lang.StringgetNodeInfo(DetailAST node)Get string representation of the node as token name, node text, line number and column number.private static java.lang.StringparseAndPrintJavadocTree(DetailAST node)Parses block comment as javadoc and prints its tree.private static DetailASTparseFile(java.io.File file, boolean withComments)Parse a file and return the parse tree.private static DetailASTparseFileText(FileText text, boolean withComments)Parse a text and return the parse tree.static java.lang.StringprintAst(FileText text, boolean withComments)Parse a file and print the parse tree.static java.lang.StringprintFileAst(java.io.File file, boolean withComments)Parse a file and print the parse tree.private static java.lang.StringprintJavaAndJavadocTree(DetailAST ast)Prints full tree (java + comments + javadoc) of the DetailAST.static java.lang.StringprintJavaAndJavadocTree(java.io.File file)Prints full AST (java + comments + javadoc) of the java file.private static java.lang.StringprintTree(DetailAST ast)Print AST.
-
-
-
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, boolean withComments) throws java.io.IOException, CheckstyleExceptionParse a file and print the parse tree.- Parameters:
file- the file to print.withComments- true to include comments to AST- 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, CheckstyleExceptionPrints full AST (java + comments + javadoc) of the java file.- Parameters:
file- java file- Returns:
- Full tree
- Throws:
java.io.IOException- Failed to open a fileCheckstyleException- 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
-
printAst
public static java.lang.String printAst(FileText text, boolean withComments) throws CheckstyleException
Parse a file and print the parse tree.- Parameters:
text- the text to parse.withComments- true to include comments to AST- Returns:
- the AST of the file in String form.
- Throws:
CheckstyleException- if the file is not a Java source.
-
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.
-
parseFile
private static DetailAST parseFile(java.io.File file, boolean withComments) throws java.io.IOException, CheckstyleException
Parse a file and return the parse tree.- Parameters:
file- the file to parse.withComments- true to include comment nodes to the tree- Returns:
- the root node of the parse tree.
- Throws:
java.io.IOException- if the file could not be read.CheckstyleException- if the file is not a Java source.
-
parseFileText
private static DetailAST parseFileText(FileText text, boolean withComments) throws CheckstyleException
Parse a text and return the parse tree.- Parameters:
text- the text to parse.withComments- true to include comment nodes to the tree- Returns:
- the root node of the parse tree.
- Throws:
CheckstyleException- if the file is not a Java source.
-
-