Java PMD,编写自定义规则集

时间:2016-05-19 08:42:25

标签: java pmd rule

我对java抽象规则的api有疑问。比如从这个网站https://pmd.github.io/pmd-5.3.6/customizing/howtowritearule.html

获得的这段特殊代码
package net.sourceforge.pmd.rules;

import java.util.concurrent.atomic.AtomicLong;

import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;

public class CountRule extends AbstractJavaRule {

       private static final String COUNT = "count";

       @Override
       public void start(RuleContext ctx) {
               ctx.setAttribute(COUNT, new AtomicLong());
               super.start(ctx);
       }

       @Override
       public Object visit(ASTExpression node, Object data) {
               // How many Expression nodes are there in all files parsed!  I must know!
               RuleContext ctx = (RuleContext)data;
               AtomicLong total = (AtomicLong)ctx.getAttribute(COUNT);
               total.incrementAndGet();
               return super.visit(node, data);
       }

       @Override
       public void end(RuleContext ctx) {
               AtomicLong total = (AtomicLong)ctx.getAttribute(COUNT);
               addViolation(ctx, null, new Object[] { total });
               ctx.removeAttribute(COUNT);
               super.end(ctx);
       }
}

当我运行此规则时,我似乎无法运行开始或结束方法,而是规则跳转到visit方法并开始访问抽象语法树中的所有表达式节点。有谁知道它为什么不调用开始和结束方法?

0 个答案:

没有答案