2
我想創建我的規則。我看到這個鏈接:http://checkstyle.sourceforge.net/writingchecks.html 我創建一個簡單的Maven項目,它只包含一個類:如何運行自定義checkstyle規則?
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>
,然後,我用命令行運行(使用行家來創建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
你想要我!
謝謝拉尼,可能你是誤解。我想將MyRuleProject導出到jar文件。然後,我將使用以下命令檢查另一個不使用maven的項目(或.java文件): java -classpath my-core-1.0.jar:checkstyle-5.5-all.jar com.puppycrawl.tools.checkstyle .Main -c sun_checks.xml -r(myfile) – ryandao 2012-08-06 06:47:55