Search in sources :

Example 1 with FormalComment

use of net.sourceforge.pmd.lang.java.ast.FormalComment in project pmd by pmd.

the class AbstractCommentRule method assignCommentsToDeclarations.

protected void assignCommentsToDeclarations(ASTCompilationUnit cUnit) {
    SortedMap<Integer, Node> itemsByLineNumber = orderedCommentsAndDeclarations(cUnit);
    FormalComment lastComment = null;
    AbstractJavaAccessNode lastNode = null;
    for (Entry<Integer, Node> entry : itemsByLineNumber.entrySet()) {
        Node value = entry.getValue();
        if (value instanceof AbstractJavaAccessNode) {
            AbstractJavaAccessNode node = (AbstractJavaAccessNode) value;
            // maybe the last comment is within the last node
            if (lastComment != null && isCommentNotWithin(lastComment, lastNode, node) && isCommentBefore(lastComment, node)) {
                node.comment(lastComment);
                lastComment = null;
            }
            if (!(node instanceof AbstractJavaAccessTypeNode)) {
                lastNode = node;
            }
        } else if (value instanceof FormalComment) {
            lastComment = (FormalComment) value;
        }
    }
}
Also used : AbstractJavaAccessNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode) FormalComment(net.sourceforge.pmd.lang.java.ast.FormalComment) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaAccessNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode) AbstractJavaAccessTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessTypeNode) AbstractJavaAccessTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessTypeNode)

Example 2 with FormalComment

use of net.sourceforge.pmd.lang.java.ast.FormalComment in project pmd by pmd.

the class UnusedImportsRule method visitComments.

private void visitComments(ASTCompilationUnit node) {
    if (imports.isEmpty()) {
        return;
    }
    for (Comment comment : node.getComments()) {
        if (!(comment instanceof FormalComment)) {
            continue;
        }
        for (Pattern p : PATTERNS) {
            Matcher m = p.matcher(comment.getImage());
            while (m.find()) {
                String s = m.group(1);
                imports.remove(new ImportWrapper(s, s, new DummyJavaNode(-1)));
                if (m.groupCount() > 1) {
                    s = m.group(2);
                    if (s != null) {
                        String[] params = s.split("\\s*,\\s*");
                        for (String param : params) {
                            final int firstDot = param.indexOf('.');
                            final String expectedImportName;
                            if (firstDot == -1) {
                                expectedImportName = param;
                            } else {
                                expectedImportName = param.substring(0, firstDot);
                            }
                            imports.remove(new ImportWrapper(param, expectedImportName, new DummyJavaNode(-1)));
                        }
                    }
                }
                if (imports.isEmpty()) {
                    return;
                }
            }
        }
    }
}
Also used : FormalComment(net.sourceforge.pmd.lang.java.ast.FormalComment) Comment(net.sourceforge.pmd.lang.java.ast.Comment) Pattern(java.util.regex.Pattern) FormalComment(net.sourceforge.pmd.lang.java.ast.FormalComment) ImportWrapper(net.sourceforge.pmd.lang.rule.ImportWrapper) Matcher(java.util.regex.Matcher) DummyJavaNode(net.sourceforge.pmd.lang.java.ast.DummyJavaNode)

Example 3 with FormalComment

use of net.sourceforge.pmd.lang.java.ast.FormalComment in project pmd by pmd.

the class AbstractCommentRuleTest method testFilteredCommentIn.

/**
 * Blank lines in comments should not raise an exception. See bug #1048.
 */
@Test
public void testFilteredCommentIn() {
    Token token = new Token();
    token.image = "/* multi line comment with blank lines\n\n\n */";
    String filtered = testSubject.filteredCommentIn(new MultiLineComment(token));
    assertEquals("multi line comment with blank lines", filtered);
    token.image = "/** a formal comment with blank lines\n\n\n */";
    filtered = testSubject.filteredCommentIn(new FormalComment(token));
    assertEquals("a formal comment with blank lines", filtered);
}
Also used : FormalComment(net.sourceforge.pmd.lang.java.ast.FormalComment) MultiLineComment(net.sourceforge.pmd.lang.java.ast.MultiLineComment) Token(net.sourceforge.pmd.lang.java.ast.Token) Test(org.junit.Test)

Aggregations

FormalComment (net.sourceforge.pmd.lang.java.ast.FormalComment)3 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Node (net.sourceforge.pmd.lang.ast.Node)1 AbstractJavaAccessNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode)1 AbstractJavaAccessTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessTypeNode)1 Comment (net.sourceforge.pmd.lang.java.ast.Comment)1 DummyJavaNode (net.sourceforge.pmd.lang.java.ast.DummyJavaNode)1 MultiLineComment (net.sourceforge.pmd.lang.java.ast.MultiLineComment)1 Token (net.sourceforge.pmd.lang.java.ast.Token)1 ImportWrapper (net.sourceforge.pmd.lang.rule.ImportWrapper)1 Test (org.junit.Test)1