Checkstyle中的自定义规则,用于确保将Logger声明为静态最终类属性

时间:2012-01-24 20:18:52

标签: checkstyle

我已经编写了上面的规则,当我尝试从命令行运行它时,我不断收到相同的消息: 无法创建Checker:无法初始化模块TreeWalker - 无法实例化LoggerAttrCheck

我已经将我的支票拆分为Checker中的一个和Treewalker中的一个,根据手册中的建议虔诚地复制了这些条目,没有任何乐趣。任何人都有自定义规则的类似问题。我是WinXP,java 1.6(Eclipse),checkstyle-5.1文件夹在路径中。 我可以提供代码,但这有点像环境问题。

代码如下:

package com.mystuff.checkstyle.hecks;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.CheckUtils;

/**
 * 
 * 
 * This package provides the custom checks that were required outside 
 * of the standard checks provided
 * 
 */
public class LoggerAttrCheck extends Check
{
   /**
    *  
    * 
    * The Logger must be declared as a static final class attribute
    * 
    */
    @Override
    public int[] getDefaultTokens()
    {
        return new int[] { TokenTypes.VARIABLE_DEF};
    }

    @Override
    public void visitToken(DetailAST aAST)
    {
        if(aAST.getType()==TokenTypes.VARIABLE_DEF)
          visitVariableDef(aAST);

    }
    /**
     * Checks type of given variable.
     * @param aAST variable to check.
     */
    private void visitVariableDef(DetailAST aAST)
    {
        checkVariableDefn(aAST);
    }

    /**
     * 
     * Checks variable to see if its a Logger and static final
     *      * @param aAST node to check.
     */
    private void checkVariableDefn(DetailAST aAST)
    {
        final DetailAST type = aAST.findFirstToken(TokenTypes.TYPE);
        final FullIdent ident = CheckUtils.createFullType(type);

        if ((ident.getText().equals("Logger"))) 
        {
              if((!aAST.branchContains(TokenTypes.FINAL))||(!aAST.branchContainsTokenTypes.LITERAL_STATIC)))
          {
          log(type.getLineNo(), type.getColumnNo(),
                    "Logger not defined as static final class attribute", type.getText());
          }    
        }
    }
}

这构建了com.stuff.checkstyle.checks.jar,因此checkstyle_packages.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE checkstyle-packages PUBLIC
    "-//Puppy Crawl//DTD Package Names 1.3//EN"
    "http://www.puppycrawl.com/dtds/packages_1_3.dtd">

<checkstyle-packages>
    <package name="com.mystuff.checkstyle.checks"/>
</checkstyle-packages>

感谢所有的想法!

1 个答案:

答案 0 :(得分:0)

你的checkstyle配置文件是什么样的?您的checkstyle_packages.xml似乎被忽略了。我发现我无法将我的包添加到checkstyle-packages中,因此使用完整的包名称声明了我的检查:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
  <module name="TreeWalker">
    <!-- Blundell specific checks -->
    <module name="com.blundell.checks.AntiHungarian" />
  </module>
</module>

请注意,检查的java类名称是AntiHungarianCheck,但您声明的只是AntiHungarian

取自我的自定义checkstyle示例:

http://blog.blundell-apps.com/create-your-own-checkstyle-check/

和源代码:

https://github.com/blundell/CreateYourOwnCheckStyleCheck