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
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
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();
// actually may be empty.
for (int i = openingBrace.getLineNo() - 1; i <= lastLine - 2; i++) {
if (contents.lineIsBlank(i) || contents.lineIsComment(i)) {
length--;
}
}
}
return length;
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class EmptyLineSeparatorCheck method hasEmptyLine.
/**
* Checks, whether there are empty lines within the specified line range. Line numbering is
* started from 1 for parameter values
*
* @param startLine number of the first line in the range
* @param endLine number of the second line in the range
* @return {@code true} if found any blank line within the range, {@code false}
* otherwise
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private boolean hasEmptyLine(int startLine, int endLine) {
// Initial value is false - blank line not found
boolean result = false;
final FileContents fileContents = getFileContents();
for (int line = startLine; line <= endLine; line++) {
// Check, if the line is blank. Lines are numbered from 0, so subtract 1
if (fileContents.lineIsBlank(line - 1)) {
result = true;
break;
}
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class TreeWalkerTest method testProcessWithParserThrowable.
@Test
public void testProcessWithParserThrowable() throws Exception {
final TreeWalker treeWalker = new TreeWalker();
treeWalker.configure(createModuleConfig(TypeNameCheck.class));
final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
treeWalker.setModuleFactory(factory);
treeWalker.setupChild(createModuleConfig(TypeNameCheck.class));
final File file = new File(temporaryFolder, "file.java");
final List<String> lines = new ArrayList<>();
lines.add(" classD a {} ");
final FileText fileText = new FileText(file, lines);
treeWalker.setFileContents(new FileContents(fileText));
try {
treeWalker.processFiltered(file, fileText);
assertWithMessage("Exception is expected").fail();
} catch (CheckstyleException exception) {
assertWithMessage("Error message is unexpected").that(exception.getMessage().contains("occurred while parsing file")).isTrue();
}
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class TreeWalkerTest method testBehaviourWithOrdinaryAndCommentChecks.
@Test
public void testBehaviourWithOrdinaryAndCommentChecks() throws Exception {
final TreeWalker treeWalker = new TreeWalker();
treeWalker.configure(createModuleConfig(TypeNameCheck.class));
treeWalker.configure(createModuleConfig(CommentsIndentationCheck.class));
final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<>(), Thread.currentThread().getContextClassLoader());
treeWalker.setModuleFactory(factory);
treeWalker.setupChild(createModuleConfig(TypeNameCheck.class));
treeWalker.setupChild(createModuleConfig(CommentsIndentationCheck.class));
final File file = new File(temporaryFolder, "file.java");
final List<String> lines = new ArrayList<>();
lines.add(" class a%$# {} ");
final FileText fileText = new FileText(file, lines);
treeWalker.setFileContents(new FileContents(fileText));
try {
treeWalker.processFiltered(file, fileText);
assertWithMessage("file is not compilable, exception is expected").fail();
} catch (CheckstyleException exception) {
final String message = "IllegalStateException occurred while parsing file";
assertWithMessage("Error message is unexpected").that(exception.getMessage().contains(message)).isTrue();
}
}
use of com.puppycrawl.tools.checkstyle.api.FileContents in project checkstyle by checkstyle.
the class XpathFileGeneratorAstFilterTest method createTreeWalkerAuditEvent.
private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, Violation violation) throws Exception {
final File file = new File(getPath(fileName));
final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
final FileContents fileContents = new FileContents(fileText);
final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
}
Aggregations