Skip to content

Commit

Permalink
Issue #5436: Split TreeWalker to TreeWalker and JavaParser
Browse files Browse the repository at this point in the history
  • Loading branch information
pbludov authored and romani committed Jan 24, 2018
1 parent 9bcb6bf commit 58327e1
Show file tree
Hide file tree
Showing 33 changed files with 504 additions and 515 deletions.
2 changes: 1 addition & 1 deletion config/import-control.xml
Expand Up @@ -167,7 +167,7 @@
<allow pkg="java.util" exact-match="true"/> <allow pkg="java.util" exact-match="true"/>
<allow pkg="com.puppycrawl.tools.checkstyle.api" local-only="true"/> <allow pkg="com.puppycrawl.tools.checkstyle.api" local-only="true"/>
<allow pkg="com.puppycrawl.tools.checkstyle.utils" local-only="true"/> <allow pkg="com.puppycrawl.tools.checkstyle.utils" local-only="true"/>
<allow class="com.puppycrawl.tools.checkstyle.TreeWalker" local-only="true"/> <allow class="com.puppycrawl.tools.checkstyle.JavaParser" local-only="true"/>
<allow class="com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode" <allow class="com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode"
local-only="true"/> local-only="true"/>
<allow class="com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser" local-only="true"/> <allow class="com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser" local-only="true"/>
Expand Down
2 changes: 1 addition & 1 deletion config/pmd.xml
Expand Up @@ -135,7 +135,7 @@
<properties> <properties>
<!-- TreeWalker integrates Checkstyle and antlr and CheckstyleAntTask integrates Checkstyle <!-- TreeWalker integrates Checkstyle and antlr and CheckstyleAntTask integrates Checkstyle
with Ant. Checker collects external resource locations and setup configuration. --> with Ant. Checker collects external resource locations and setup configuration. -->
<property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration[@Image='TreeWalker' or @Image='CheckstyleAntTask' or @Image='Checker' or @Image='Main']"/> <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration[@Image='CheckstyleAntTask' or @Image='Checker' or @Image='Main']"/>
</properties> </properties>
</rule> </rule>
<rule ref="rulesets/java/coupling.xml/CouplingBetweenObjects"> <rule ref="rulesets/java/coupling.xml/CouplingBetweenObjects">
Expand Down
Expand Up @@ -21,16 +21,11 @@


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;


import antlr.RecognitionException;
import antlr.TokenStreamException;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.DetailNode; import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText; import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtils; import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
Expand All @@ -42,22 +37,6 @@
*/ */
public final class AstTreeStringPrinter { public final class AstTreeStringPrinter {


/**
* Enum to be used for test if comments should be printed.
*/
public enum PrintOptions {

/**
* Comments has to be printed.
*/
WITH_COMMENTS,
/**
* Comments has NOT to be printed.
*/
WITHOUT_COMMENTS

}

/** Newline pattern. */ /** Newline pattern. */
private static final Pattern NEWLINE = Pattern.compile("\n"); private static final Pattern NEWLINE = Pattern.compile("\n");
/** Return pattern. */ /** Return pattern. */
Expand All @@ -76,14 +55,14 @@ private AstTreeStringPrinter() {
/** /**
* Parse a file and print the parse tree. * Parse a file and print the parse tree.
* @param file the file to print. * @param file the file to print.
* @param withComments true to include comments to AST * @param options {@link JavaParser.Options} to control the inclusion of comment nodes.
* @return the AST of the file in String form. * @return the AST of the file in String form.
* @throws IOException if the file could not be read. * @throws IOException if the file could not be read.
* @throws CheckstyleException if the file is not a Java source. * @throws CheckstyleException if the file is not a Java source.
*/ */
public static String printFileAst(File file, PrintOptions withComments) public static String printFileAst(File file, JavaParser.Options options)
throws IOException, CheckstyleException { throws IOException, CheckstyleException {
return printTree(parseFile(file, withComments)); return printTree(JavaParser.parseFile(file, options));
} }


/** /**
Expand All @@ -95,7 +74,7 @@ public static String printFileAst(File file, PrintOptions withComments)
*/ */
public static String printJavaAndJavadocTree(File file) public static String printJavaAndJavadocTree(File file)
throws IOException, CheckstyleException { throws IOException, CheckstyleException {
final DetailAST tree = parseFile(file, PrintOptions.WITH_COMMENTS); final DetailAST tree = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS);
return printJavaAndJavadocTree(tree); return printJavaAndJavadocTree(tree);
} }


Expand Down Expand Up @@ -143,13 +122,14 @@ private static String parseAndPrintJavadocTree(DetailAST node) {
/** /**
* Parse a file and print the parse tree. * Parse a file and print the parse tree.
* @param text the text to parse. * @param text the text to parse.
* @param withComments true to include comments to AST * @param options {@link JavaParser.Options} to control the inclusion of comment nodes.
* @return the AST of the file in String form. * @return the AST of the file in String form.
* @throws CheckstyleException if the file is not a Java source. * @throws CheckstyleException if the file is not a Java source.
*/ */
public static String printAst(FileText text, public static String printAst(FileText text, JavaParser.Options options)
PrintOptions withComments) throws CheckstyleException { throws CheckstyleException {
return printTree(parseFileText(text, withComments)); final DetailAST ast = JavaParser.parseFileText(text, options);
return printTree(ast);
} }


/** /**
Expand Down Expand Up @@ -226,48 +206,4 @@ private static String escapeAllControlChars(String text) {
return TAB.matcher(textWithoutReturns).replaceAll("\\\\t"); return TAB.matcher(textWithoutReturns).replaceAll("\\\\t");
} }


/**
* Parse a file and return the parse tree.
* @param file the file to parse.
* @param withComments true to include comment nodes to the tree
* @return the root node of the parse tree.
* @throws IOException if the file could not be read.
* @throws CheckstyleException if the file is not a Java source.
*/
private static DetailAST parseFile(File file, PrintOptions withComments)
throws IOException, CheckstyleException {
final FileText text = new FileText(file.getAbsoluteFile(),
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
return parseFileText(text, withComments);
}

/**
* Parse a text and return the parse tree.
* @param text the text to parse.
* @param withComments true to include comment nodes to the tree
* @return the root node of the parse tree.
* @throws CheckstyleException if the file is not a Java source.
*/
private static DetailAST parseFileText(FileText text, PrintOptions withComments)
throws CheckstyleException {
final FileContents contents = new FileContents(text);
final DetailAST result;
try {
if (withComments == PrintOptions.WITH_COMMENTS) {
result = TreeWalker.parseWithComments(contents);
}
else {
result = TreeWalker.parse(contents);
}
}
catch (RecognitionException | TokenStreamException ex) {
final String exceptionMsg = String.format(Locale.ROOT,
"%s occurred during the analysis of file %s.",
ex.getClass().getSimpleName(), text.getFile().getPath());
throw new CheckstyleException(exceptionMsg, ex);
}

return result;
}

} }

0 comments on commit 58327e1

Please sign in to comment.