我想创建我的规则。我看到了这个链接: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
你想骂我!
答案 0 :(得分:1)
如果您使用的是maven,可以将checkstyle插件添加到maven pom.xml。因此,无论何时建立项目或运行maven安装,它都会检查checkstyle错误。 Checkstyle设置非常简单,比如在pom.xml中添加一些行。