我想创建我的规则。我看到了这个链接:http ://checkstyle.sourceforge.net/writingchecks.html 我创建了一个简单的maven项目,它只包含一个类:
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
你愿意帮助我!