⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 methodlimitcheck.java

📁 Checkstyle 可寻找:·不能使用的或者多余的输入 ·空格更好的地方不使用跳格符
💻 JAVA
字号:
package com.mycompany.checks;

import com.puppycrawl.tools.checkstyle.api.*;

public class MethodLimitCheck extends Check
{
    /** the maximum number of methods per class/interface */
    private int max = 30;

    /**
     * Give user a chance to configure max in the config file.
     * @param aMax the user specified maximum parsed from configuration property.
     */
    public void setMax(int aMax)
    {
        max = aMax;
    }

    /**
     * We are interested in CLASS_DEF and INTERFACE_DEF Tokens.
     * @see Check
     */
    public int[] getDefaultTokens()
    {
        return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
    }

    /**
     * @see Check
     */
    public void visitToken(DetailAST ast)
    {
        // the tree below a CLASS_DEF/INTERFACE_DEF looks like this:

        // CLASS_DEF
        //   MODIFIERS
        //   class name (IDENT token type)
        //   EXTENDS_CLAUSE
        //   IMPLEMENTS_CLAUSE
        //   OBJBLOCK
        //     {
        //     some other stuff like variable declarations etc.
        //     METHOD_DEF
        //     more stuff, the users might mix methods, variables, etc.
        //     METHOD_DEF
        //     ...and so on
        //     }

        // We use helper methods to navigate in the syntax tree

        // 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 > max) {
            log(ast.getLineNo(), "too.many.methods", new Integer(max));
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -