/** * Collects references made in Javadoc comments. * @param ast node to inspect for Javadoc */ private void collectReferencesFromJavadoc(DetailAST ast) { final FileContents contents = getFileContents(); final int lineNo = ast.getLineNo(); final TextBlock textBlock = contents.getJavadocBefore(lineNo); if (textBlock != null) { referenced.addAll(collectReferencesFromJavadoc(textBlock)); } }
@Override public boolean shouldSuppress(int startLineNo, int startColNo, int endLineNo, int endColNo) { return currentContents .hasIntersectionWithComment(startLineNo, startColNo, endLineNo, endColNo); }
@Override public void beginTree(DetailAST rootAST) { currentImportControl = null; processCurrentFile = path.matcher(getFileContents().getFileName()).find(); fileName = getFileContents().getText().getFile().getName(); final int period = fileName.lastIndexOf('.'); if (period != -1) { fileName = fileName.substring(0, period); } }
@Override public void beginTree(DetailAST rootAST) { singlelineComments = getFileContents().getSingleLineComments(); blockComments = getFileContents().getBlockComments(); }
@Test public void testHasIntersectionWithBlockComment() { final FileContents fileContents = new FileContents(new FileText(new File("filename"), Arrays.asList(" /* */ ", " ", " /* test ", " */ ", " "))); fileContents.reportCComment(1, 2, 1, 5); fileContents.reportCComment(3, 2, 4, 2); assertTrue("Should return true when comments intersect", fileContents.hasIntersectionWithComment(2, 2, 3, 6)); }
@Test public void testReportJavadocComment2() { final FileContents fileContents = new FileContents("filename", " /** */ "); fileContents.reportCComment(1, 2, 1, 6); final TextBlock comment = fileContents.getJavadocBefore(2); assertEquals("Invalid comment", new Comment(new String[] {"/** *"}, 2, 1, 6).toString(), comment.toString()); }
@Test public void testGetJavadocBefore() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" "))); final Map<Integer, TextBlock> javadoc = new HashMap<>(); javadoc.put(0, new Comment(new String[] {"// "}, 2, 1, 2)); Whitebox.setInternalState(fileContents, "javadocComments", javadoc); final TextBlock javadocBefore = fileContents.getJavadocBefore(2); assertEquals("Invalid before javadoc", new Comment(new String[] {"// "}, 2, 1, 2).toString(), javadocBefore.toString()); }
@Test public void testSinglelineCommentNotIntersect() { // just to make UT coverage 100% final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportSingleLineComment(1, 2); assertFalse("Should return false when there is no intersection", fileContents.hasIntersectionWithComment(1, 0, 1, 1)); }
/** * Get source file name. * @return source file name. */ private String getFileName() { String name = getFileContents().getFileName(); name = name.substring(name.lastIndexOf(File.separatorChar) + 1); name = FILE_EXTENSION_PATTERN.matcher(name).replaceAll(""); return name; }
@Test public void testHasIntersectionWithSingleLineComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Arrays.asList(" ", " //test ", " //test ", " //test "))); fileContents.reportCppComment(4, 4); assertTrue("Should return true when comments intersect", fileContents.hasIntersectionWithComment(1, 3, 4, 6)); }
@Override public void beginTree(DetailAST rootAST) { matcher = format.matcher(getFileContents().getText().getFullText()); matchCount = 0; errorCount = 0; findMatch(); }
return; if (!getFileContents().getFileName().matches(".*\\.java")) { return; int parenthesesStart = lineText.indexOf("("); while (parenthesesStart != -1 && getFileContents().hasIntersectionWithComment(i, parenthesesStart, i, parenthesesStart)){ parenthesesStart = lineText.indexOf("(", parenthesesStart + 1); while (matcher.find()) { if (getFileContents().hasIntersectionWithComment(i, matcher.start(), i, matcher.end())) { continue;
/** * Determines the number semicolons in a method excluding those in * comments. * @param method Method to count * @return The number of semicolons in the method as an int */ private int countSemiColons(final DetailAST method) { final DetailAST openingbrace = method.findFirstToken(TokenTypes.SLIST); int count = 0; if (openingbrace != null) { final DetailAST closingbrace = openingbrace.findFirstToken(TokenTypes.RCURLY); final int lastline = closingbrace.getLineNo(); final int firstline = openingbrace.getLineNo(); final FileContents contents = this.getFileContents(); for (int line = firstline - 1; line < lastline; line += 1) { if (!contents.lineIsBlank(line) && !contents.lineIsComment(line) && contents.getLine(line).contains(";")) { count += 1; } } } return count; } }
@Test public void testExtractBlockComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Arrays.asList(" ", " ", " /* test ", " */ ", " "))); fileContents.reportCComment(3, 2, 4, 2); final Map<Integer, List<TextBlock>> blockComments = fileContents.getBlockComments(); final String[] text = blockComments.get(3).get(0).getText(); assertArrayEquals("Invalid comment text", new String[] {"/* test ", " *"}, text); }
@Test public void testDeprecatedAbbreviatedMethod() { // just to make UT coverage 100% final FileContents fileContents = new FileContents("filename", "123", "456"); fileContents.reportCppComment(1, 1); fileContents.reportCComment(1, 1, 1, 1); final Comment cppComment = new Comment(new String[] {"23"}, 1, 1, 2); final Comment cComment = new Comment(new String[] {"2"}, 1, 1, 1); assertEquals("Invalid cpp comment", cppComment.toString(), fileContents.getCppComments().get(1).toString()); assertEquals("Invalid c comment", cComment.toString(), fileContents.getCComments().get(1).get(0).toString()); }
@Test public void testFindNearestMatchDontAllowSameColumn() { final SuppressionCommentFilter suppressionCommentFilter = new SuppressionCommentFilter(); final FileContents contents = new FileContents("filename", "//CHECKSTYLE:OFF: ConstantNameCheck", "line2"); contents.reportSingleLineComment(1, 0); final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename", new LocalizedMessage(1, null, null, null, null, Object.class, null), null); final boolean result = suppressionCommentFilter.accept(dummyEvent); assertFalse("Filter should not accept event", result); }
@Test public void testTextFileName() { final FileContents fileContents = new FileContents("filename", "123", "456"); assertEquals("Invalid file name", "filename", fileContents.getText().getFile().getName()); }
@Override protected void processFiltered(File file, FileText fileText) { contents = new FileContents(fileText); }
@Test public void testInPackageInfo() { final FileContents fileContents = new FileContents(new FileText( new File("filename.package-info.java"), Collections.singletonList(" // "))); assertTrue("Should return true when in package info", fileContents.inPackageInfo()); }
@Test public void testReportComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportCComment(1, 2, 1, 2); final Map<Integer, List<TextBlock>> comments = fileContents.getCComments(); assertEquals("Invalid comment", new Comment(new String[] {"/"}, 2, 1, 2).toString(), comments.get(1).get(0).toString()); }