Checkstyle:访问变量名称?
我们有一个标准,变量名称必须拼写不同(不仅仅是大小写)。例如,这是非法的:
int INDEX = 0;
int index = 0;
我正在尝试编写一个 Checkstyle 扩展来支持这一点,但是 ast.getText()
返回“variable_def”而不是实际的变量名称。我该怎么做?谢谢。
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();
}
}
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. 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系。我发现了问题。您无法获取 VARIABLE_DEF 的文本。您必须深入了解 IDENT...然后获取其上的文本。
Nevermind. I found the problem. You can't get the text of a VARIABLE_DEF. You have to drill down to the IDENT...then get the text on that.