感谢。
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)未定义
为什么呢?
答案 0 :(得分: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)
您的代码与其中任何一个都不匹配。