0

谢谢。

public class ParameterNameConvention extends AbstractJavaRule {

private final static String PATTERN = "[p][a-zA-Z]+";

public Object visit(ASTMethodDeclaration node, Object data) {
    RuleContext result = (RuleContext) data;
    String rulePattern = (!getStringProperty("rulePattern")
            .equalsIgnoreCase("")) ? getStringProperty("rulePattern")
            : PATTERN;
    if (node.containsChildOfType(ASTFormalParameter.class)) {
        Iterator iterator = node.findChildrenOfType(
                ASTFormalParameter.class).iterator();
        while (iterator.hasNext()) {
            ASTFormalParameter element = (ASTFormalParameter) iterator
                    .next();
            Iterator decIdIterator = element.findChildrenOfType(
                    ASTVariableDeclaratorId.class).iterator();
            while (decIdIterator.hasNext()) {
                ASTVariableDeclaratorId decElement = (ASTVariableDeclaratorId) decIdIterator
                        .next();
                if (!decElement.getImage().matches(rulePattern)) {

                    result.getReport()
                            .addRuleViolation(
                                    new RuleViolation(
                                            this,
                                            node.getBeginLine(),
                                            "Parameter '"
                                                    + decElement.getImage()
                                                    + "' should match regular expression pattern '"
                                                    + rulePattern + "'",
                                            result));
                }
            }
        }
    }
    return result;
}

}

对于new RuleViolation有一个问题:

构造函数 RuleViolation(ParameterNameConvention, int, String, RuleContext) 未定义

为什么?

4

1 回答 1

0

您使用的是哪个版本的 PMD?在 PMD 4.2.5 中,RuleViolation的构造函数是:

RuleViolation(Rule rule, RuleContext ctx, SimpleNode node) 

RuleViolation(Rule rule, RuleContext ctx, SimpleNode node, java.lang.String specificMsg) 

您的代码与其中任何一个都不匹配。

于 2013-06-14T18:58:01.147 回答