ルールを作成したい。私はこのリンクを見ました: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
あなたは私をヘップしたいです!