use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class EmptyLineSeparatorCheck method getEmptyLines.
/**
* Get list of empty lines.
* @param ast the ast to check.
* @return list of line numbers for empty lines.
*/
private List<Integer> getEmptyLines(DetailAST ast) {
final DetailAST lastToken = ast.getLastChild().getLastChild();
int lastTokenLineNo = 0;
if (lastToken != null) {
lastTokenLineNo = lastToken.getLineNo();
}
final List<Integer> emptyLines = new ArrayList<>();
final FileContents fileContents = getFileContents();
for (int lineNo = ast.getLineNo(); lineNo < lastTokenLineNo; lineNo++) {
if (fileContents.lineIsBlank(lineNo)) {
emptyLines.add(lineNo);
}
}
return emptyLines;
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project commons by twitter.
the class JavadocMethodRegexCheck method isShortEnoughToSkip.
/**
* Return true iff the method is short enough that we don't require
* a javadoc.
*/
private boolean isShortEnoughToSkip(final DetailAST aAST) {
// Based on code from
// com/puppycrawl/tools/checkstyle/api/MethodLengthCheck.java
final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
if (openingBrace != null) {
final DetailAST closingBrace = openingBrace.findFirstToken(TokenTypes.RCURLY);
int length = closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
// skip blank lines
final FileContents contents = getFileContents();
final int lastLine = closingBrace.getLineNo();
for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
if (contents.lineIsBlank(i)) {
length--;
}
}
if (length <= minLineCount) {
return true;
}
}
return false;
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class TreeWalker method processFiltered.
@Override
protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
// check if already checked and passed the file
if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
final String msg = "%s occurred during the analysis of file %s.";
final String fileName = file.getPath();
try {
final FileText text = FileText.fromLines(file, lines);
final FileContents contents = new FileContents(text);
final DetailAST rootAST = parse(contents);
getMessageCollector().reset();
walk(rootAST, contents, AstState.ORDINARY);
final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);
walk(astWithComments, contents, AstState.WITH_COMMENTS);
} catch (final TokenStreamRecognitionException tre) {
final String exceptionMsg = String.format(Locale.ROOT, msg, "TokenStreamRecognitionException", fileName);
throw new CheckstyleException(exceptionMsg, tre);
} catch (RecognitionException | TokenStreamException ex) {
final String exceptionMsg = String.format(Locale.ROOT, msg, ex.getClass().getSimpleName(), fileName);
throw new CheckstyleException(exceptionMsg, ex);
}
}
}
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 MethodLengthCheck method getLengthOfBlock.
/**
* Returns length of code only without comments and blank lines.
* @param openingBrace block opening brace
* @param closingBrace block closing brace
* @return number of lines with code for current block
*/
private int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace) {
int length = closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
if (!countEmpty) {
final FileContents contents = getFileContents();
final int lastLine = closingBrace.getLineNo();
for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
length--;
}
}
}
return length;
}
Aggregations