creatRuleViolation怎么定义​​呢?从这个救我 [英] creatRuleViolation how to define it? Save me from this

查看:109
本文介绍了creatRuleViolation怎么定义​​呢?从这个救我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助.我想牺牲这个声誉来获得一个正确的答案.

Help. I would like to sacrifice the reputation for a proper answer..

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(
                                        createRuleViolation(
                                                this,
                                                node.getBeginLine(),
                                                "Parameter '"
                                                        + decElement.getImage()
                                                        + "' should match regular expression pattern '"
                                                        + rulePattern + "'",
                                                result));
                    }
                }
            }
        }
        return result;
    }
}

但是,'creatRuleViolation'不起作用.如何定义?

However, 'creatRuleViolation' doesn't work. How to define it?

推荐答案

在这里,昨晚我做了一些研究来帮助您. createRuleViolation()AbstractRuleViolationFactory中定义为抽象,并在特定于语言的工厂子类(例如:JavaRuleViolationFactory)中实现,而在Rule类层次结构中不直接可用.

Here we go, I did a bit of research last night to help you out. createRuleViolation() is defined in AbstractRuleViolationFactory as abstract and implemented in language specific factory sub-classes (eg: JavaRuleViolationFactory), not directly available in Rule classes hierarchy.

而是使用通过AbstractJavaRuleAbstractRule继承的addViolationWithMessage()方法.该方法最终会在运行时在适当的工厂上调用create方法.

Instead use addViolationWithMessage() method that is inherited from AbstractRule via AbstractJavaRule. This method eventually calls the create method on appropriate factory at runtime.

我使用最新的PMD版本5.0.3尝试了您的代码,并且除了createRuleViolation()方法的问题以外,还需要进行一些其他调整才能使其正常工作.虽然主要是从MethodDeclaration节点导航到Parameter节点,但是可能会有更好的方法,但是现在可以使用了.我已经基于AST(抽象源树)修改了代码.

I tried your code with latest PMD version 5.0.3, and required few more tweaks other than the issue withcreateRuleViolation() method to get it working. Mainly to navigate from MethodDeclaration node to Parameter nodes, there could be a better way though, but it works now. I have modified the code based on the AST (abstract source tree).

package madhav.pmd.rule;

import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;

public class ParameterNameConvention extends AbstractJavaRule
{
    private final static String PATTERN = "[p][a-zA-Z]+";

    @Override
    public Object visit(final ASTMethodDeclaration node, final Object pData)
    {
        final RuleContext result = (RuleContext) pData;
        // TODO : get property
        final String rulePattern = PATTERN;
        final ASTMethodDeclarator methodDecNode = node.getFirstChildOfType(ASTMethodDeclarator.class);
        final ASTFormalParameters paramsNode = methodDecNode.getFirstChildOfType(ASTFormalParameters.class);
        if (paramsNode.getParameterCount() > 0)
        {
            for (final ASTFormalParameter element : paramsNode.findChildrenOfType(ASTFormalParameter.class))
            {
                for (final ASTVariableDeclaratorId decElement : element.findChildrenOfType(ASTVariableDeclaratorId.class))
                {
                    if (!decElement.getImage().matches(rulePattern))
                    {
                        addViolationWithMessage(result, node,
                                "Parameter '"
                                        + decElement.getImage()
                                        + "' should match regular expression pattern '"
                                        + rulePattern + "'",
                                node.getBeginLine(),
                                node.getEndLine());
                    }
                }
            }
        }
        return result;
    }
}

要在同一类上测试该规则,我命名了一个参数来满足该规则,并命名一个不满足该条件的参数(final ASTMethodDeclaration node, final Object pData).

To test the rule on the same class, I named one parameter to satisfy the rule and one not to satisfy (final ASTMethodDeclaration node, final Object pData).

规则集xml是

<?xml version="1.0"?>
<ruleset name="My rules">
  <description>My test rules</description>
  <rule name="ParameterNameConvention"
        message="Parameter must start with p"
        class="madhav.pmd.rule.ParameterNameConvention">
    <description>Don't use non-complaint parameters </description>

    <example>
        <![CDATA[
         void methodX(int value)
        ]]>
    </example>
  </rule>
</ruleset>

生成的xml PMD的结果是:

The result xml PMD generated is :

<?xml version="1.0" encoding="UTF-8"?>
<pmd version="5.0.3" timestamp="2013-06-13T16:03:52.404">
    <file name="D:\Projects\ZRules\src\madhav\pmd\rule\ParameterNameConvention.java">
        <violation beginline="16" endline="43" begincolumn="16" endcolumn="9" 
            rule="ParameterNameConvention" ruleset="My rules" package="madhav.pmd.rule" 
            class="ParameterNameConvention" priority="5">
                Parameter node should match regular expression pattern [p][a-zA-Z]+
        </violation>
    </file>
</pmd>

有关从命令行独立运行PMD的更多详细信息,请参阅PMD网站上的文档,或者如果您在Google周围搜索,也可以加载.

For more details on running PMD standalone from command line see the documentation on PMD website or there is loads available if you google around.

希望这会有所帮助.

这篇关于creatRuleViolation怎么定义​​呢?从这个救我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆