简体   繁体   English

Checkstyle:访问变量名称?

[英]Checkstyle: access variable names?

We have a standard that variable names must be spelled different (not just case). 我们有一个标准,变量名必须拼写不同(不仅仅是大小写)。 For example, this is illegal: 例如,这是非法的:

int INDEX = 0;
int index = 0;

I am trying write a Checkstyle extension to support this, but ast.getText() is returning "variable_def" instead of the actual variable name. 我正在尝试编写一个Checkstyle扩展来支持这个,但ast.getText()返回“variable_def”而不是实际的变量名。 How do I do it? 我该怎么做? Thanks. 谢谢。

package check;

import java.util.ArrayList;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class VariableSpelling extends Check {
    private ArrayList<String> variables = new ArrayList<String>();

    public int[] getDefaultTokens() {
        return new int[] { TokenTypes.VARIABLE_DEF };
    }

    /**
     * This method is called when a node is found
     */
    public void visitToken(DetailAST ast) {
        String var = ast.getText();
    }
}

Nevermind. 没关系。 I found the problem. 我发现了这个问题。 You can't get the text of a VARIABLE_DEF. 你无法获得VARIABLE_DEF的文本。 You have to drill down to the IDENT...then get the text on that. 你必须深入到IDENT ...然后得到文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM