use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class MainFrameModel method parseFileWithComments.
/**
* Parse a file and return the parse tree with comment nodes.
* @param file the file to parse.
* @return the root node of the parse tree.
* @throws IOException if the file could not be read.
* @throws ANTLRException if the file is not a Java source.
*/
public DetailAST parseFileWithComments(File file) throws IOException, ANTLRException {
final FileText fileText = getFileText(file);
final FileContents contents = new FileContents(fileText);
return TreeWalker.parseWithComments(contents);
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class MainFrameModel method parseFile.
/**
* Parse a file and return the parse tree.
* @param file the file to parse.
* @return the root node of the parse tree.
* @throws IOException if the file could not be read.
* @throws ANTLRException if the file is not a Java source.
*/
public DetailAST parseFile(File file) throws IOException, ANTLRException {
final FileText fileText = getFileText(file);
final FileContents contents = new FileContents(fileText);
return TreeWalker.parse(contents);
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class SuppressWithNearbyCommentFilter method accept.
@Override
public boolean accept(AuditEvent event) {
boolean accepted = true;
if (event.getLocalizedMessage() != null) {
// Lazy update. If the first event for the current file, update file
// contents and tag suppressions
final FileContents currentContents = FileContentsHolder.getCurrentFileContents();
if (getFileContents() != currentContents) {
setFileContents(currentContents);
tagSuppressions();
}
if (matchesTag(event)) {
accepted = false;
}
}
return accepted;
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class AstTreeStringPrinter method parseFileText.
/**
* 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, boolean withComments) throws CheckstyleException {
final FileContents contents = new FileContents(text);
final DetailAST result;
try {
if (withComments) {
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;
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class TreeWalker method processFiltered.
@Override
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
// check if already checked and passed the file
if (!ordinaryChecks.isEmpty() || !commentChecks.isEmpty()) {
final FileContents contents = getFileContents();
final DetailAST rootAST = JavaParser.parse(contents);
if (!ordinaryChecks.isEmpty()) {
walk(rootAST, contents, AstState.ORDINARY);
}
if (!commentChecks.isEmpty()) {
final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST);
walk(astWithComments, contents, AstState.WITH_COMMENTS);
}
if (filters.isEmpty()) {
addViolations(violations);
} else {
final SortedSet<Violation> filteredViolations = getFilteredViolations(file.getAbsolutePath(), contents, rootAST);
addViolations(filteredViolations);
}
violations.clear();
}
}
Aggregations