Search in sources :

Example 1 with Comment

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

the class CommentContentRule method visit.

@Override
public Object visit(ASTCompilationUnit cUnit, Object data) {
    // NPE patch: Eclipse plugin doesn't call start() at onset?
    if (currentBadWords == null) {
        start(null);
    }
    for (Comment comment : cUnit.getComments()) {
        List<String> badWords = illegalTermsIn(comment);
        if (badWords.isEmpty()) {
            continue;
        }
        addViolationWithMessage(data, cUnit, errorMsgFor(badWords), comment.getBeginLine(), comment.getEndLine());
    }
    return super.visit(cUnit, data);
}
Also used : Comment(net.sourceforge.pmd.lang.java.ast.Comment)

Example 2 with Comment

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

the class CommentDefaultAccessModifierRule method visit.

@Override
public Object visit(final ASTCompilationUnit node, final Object data) {
    interestingLineNumberComments.clear();
    final List<Comment> comments = node.getComments();
    for (final Comment comment : comments) {
        if (comment.getImage().matches(getProperty(REGEX_DESCRIPTOR).trim())) {
            interestingLineNumberComments.add(comment.getBeginLine());
        }
    }
    return super.visit(node, data);
}
Also used : Comment(net.sourceforge.pmd.lang.java.ast.Comment)

Example 3 with Comment

use of net.sourceforge.pmd.lang.java.ast.Comment in project pmd-eclipse-plugin by pmd.

the class NodeImageDeriver method dumpComments.

private static void dumpComments(ASTCompilationUnit node) {
    for (Comment comment : node.getComments()) {
        System.out.println(comment.getClass().getName());
        System.out.println(comment.getImage());
    }
}
Also used : Comment(net.sourceforge.pmd.lang.java.ast.Comment)

Example 4 with Comment

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

the class GetCommentOnFunction method call.

public Object call(Context context, List args) throws FunctionCallException {
    if (!args.isEmpty()) {
        return Boolean.FALSE;
    }
    Node n = (Node) context.getNodeSet().get(0);
    if (n instanceof AbstractNode) {
        int codeBeginLine = ((AbstractNode) n).getBeginLine();
        int codeEndLine = ((AbstractNode) n).getEndLine();
        List<Comment> commentList = ((AbstractNode) n).getFirstParentOfType(ASTCompilationUnit.class).getComments();
        for (Comment comment : commentList) {
            if (comment.getBeginLine() == codeBeginLine || comment.getEndLine() == codeEndLine) {
                return comment.getImage();
            }
        }
    }
    return Boolean.FALSE;
}
Also used : Comment(net.sourceforge.pmd.lang.java.ast.Comment) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) Node(net.sourceforge.pmd.lang.ast.Node)

Example 5 with Comment

use of net.sourceforge.pmd.lang.java.ast.Comment 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)

Aggregations

Comment (net.sourceforge.pmd.lang.java.ast.Comment)7 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)3 Node (net.sourceforge.pmd.lang.ast.Node)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)1 DummyJavaNode (net.sourceforge.pmd.lang.java.ast.DummyJavaNode)1 FormalComment (net.sourceforge.pmd.lang.java.ast.FormalComment)1 JavadocElement (net.sourceforge.pmd.lang.java.ast.JavadocElement)1 ImportWrapper (net.sourceforge.pmd.lang.rule.ImportWrapper)1 TextStyle (org.eclipse.swt.graphics.TextStyle)1