我正在尝试使用 CheckStyle 检索在给定源文件中找到的所有方法中的所有参数名称。以下是相关代码:
public int[] getDefaultTokens()
{
return new int[] { TokenTypes.METHOD_DEF};
}
public void visitToken(DetailAST aDetailAST)
{
String returnType; // The return type of the method.
int numberOfParameters; // The number of parameters in the method's parameter list... not returned in log.
String [] parameterNames; // The names of all method parameters.
int openingBraceLine; // The line number of the opening method bracket.
returnType = aDetailAST.findFirstToken(TokenTypes.TYPE).getFirstChild().getText(); // get the return type.
numberOfParameters = aDetailAST.findFirstToken(TokenTypes.PARAMETERS).getChildCount(TokenTypes.PARAMETER_DEF); // get num of parameters.
parameterNames = new String[numberOfParameters]; // create array to store the parameter names.
if (numberOfParameters > 0) // only bother if parameters existed.
{
List <DetailAST> parameters = DetailASTUtil.getDetailASTsForTypeInBranch // Get all PARAMETER_DEF nodes.
(aDetailAST.findFirstToken(TokenTypes.PARAMETERS)
, TokenTypes.PARAMETER_DEF);
int i = 0;
for (DetailAST currentParameter: parameters) // iterate through all parameters.
{
parameterNames[i] = currentParameter.findFirstToken(TokenTypes.IDENT).getText();
// Get the parameter name, store it in the array.
i++; // iterate to next parameter name array storage index.
}
}
// parameterNames now contains all parameter names in the parameter list. Format it for log message.
String formattedParameterNames = "";
if (numberOfParameters > 1) // if more than one parameter was present, then create comma list.
{
for (int i = 0; i < parameterNames.length-1; i++) // put all names in comma-separated string except for last.
{
formattedParameterNames += parameterNames[i] + ", ";
}
formattedParameterNames += parameterNames[numberOfParameters-1]; // add the last element of the comma list.
}
else if (numberOfParameters == 1) // only one parameter -- don't comma-delimit.
{
formattedParameterNames = parameterNames[0];
}
if (numberOfParameters == 2) // debug to see if string formatting is messing up the parameter names or if tree traversal is bad.
{
formattedParameterNames = "Param 1: " + parameterNames[0] + " Param 2: " + parameterNames[1];
}
log(aDetailAST.getLineNo(), "[" + returnType + "]" + ", [" + formattedParameterNames + "], ");
// will be further parsed in actual applet since I don't think there's a way to get individual lines of code via CheckStyle... I would like if a getTextForLineofCode(lineNumber) func existed with CheckStyle, but I don't think it does.
}
public static List<DetailAST> getDetailASTsForTypeInBranch(DetailAST expr,
int tokenType) {
return getDetailASTsForTypeInBranch(expr, tokenType, null);
}
private static List<DetailAST> getDetailASTsForTypeInBranch(DetailAST expr,
int tokenType, List<DetailAST> list) {
if (list == null)
list = new ArrayList<DetailAST>();
DetailAST child = (DetailAST) expr.getFirstChild();
while (child != null) {
if (child.getType() == tokenType) {
list.add(child);
} else {
list = getDetailASTsForTypeInBranch(child, tokenType, list);
}
child = (DetailAST) child.getNextSibling();
}
return list;
}
当我在主小程序中检索此日志消息时,没有/单参数列表的函数看起来很好,但双参数函数要么根本没有注册,要么返回消息“secondParmeterNameHere]”,其中 secondParameterNameHere 是特定函数的第二个参数名称。
关于我获取所有参数名称的算法有什么问题的任何想法?谢谢。