2

ルールを作成したい。私はこのリンクを見ました:http://checkstyle.sourceforge.net/writingchecks.html そして私は単純なMavenプロジェクトを作成します、それはただ1つのクラスを含みます:

package com.posco.myapp;
import com.puppycrawl.tools.checkstyle.api.*;
public class MethodLimitCheck extends Check{

 private static final int DEFAULT_MAX = 30;
    private int max = DEFAULT_MAX;

    @Override
    public int[] getDefaultTokens()
    {
        return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
    }

    @Override
    public void visitToken(DetailAST ast)
    {
        // find the OBJBLOCK node below the CLASS_DEF/INTERFACE_DEF
        DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
        // count the number of direct children of the OBJBLOCK
        // that are METHOD_DEFS
        int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF);
        // report error if limit is reached
        if (methodDefs > this.max) {
            log(ast.getLineNo(),
                "too many methods, only " + this.max + " are allowed");
        }
        if (methodDefs < this.max) {
            log(ast.getLineNo(),
                "too many methods, only " + this.max + " are allowed");
        }
   }

そして、このコードをconfig.xmlファイルに入れました

<module name="TreeWalker">
    <!-- myCheck.                     -->
    <module name="com.posco.myapp.MethodLimitCheck">
    </module>

次に、コマンドラインで実行します(mavenを使用してmyjarを作成した後):

java -classpath my-core-1.0.jar:checkstyle-5.5-all.jar com.puppycrawl.tools.checkstyle.Main -c config.xml

エラーは次のとおりです。classNotFoundException:com.puppycrawl.tools.checkstyle.gui.Main

あなたは私をヘップしたいです!

4

1 に答える 1

1

Maven を使用している場合は、checkstyle プラグインを maven pom.xml に追加できます。したがって、プロジェクトを構築したり、maven install を実行したりするたびに、checkstyle エラーがチェックされます。Checkstyle のセットアップは、pom.xml にいくつかの行を追加するだけの非常に簡単です。

于 2012-08-06T06:12:45.940 に答える