title: CheckStyle Custom Check -- Retrieve All Parameter Names
body: <p>I'm attempting to retrieve all parameter names in all methods found in a given source file using CheckStyle. Here is the relevant code:</p>
  
  <hr>
  
  <pre><code>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 &gt; 0) // only bother if parameters existed.
    {
       List &lt;DetailAST&gt; 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 &gt; 1) // if more than one parameter was present, then create comma list.
    {
       for (int i = 0; i &lt; 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.
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public static List&lt;DetailAST&gt; getDetailASTsForTypeInBranch(DetailAST expr,
          int tokenType) {
      return getDetailASTsForTypeInBranch(expr, tokenType, null);
  }
  
  private static List&lt;DetailAST&gt; getDetailASTsForTypeInBranch(DetailAST expr,
          int tokenType, List&lt;DetailAST&gt; list) {
      if (list == null)
          list = new ArrayList&lt;DetailAST&gt;();
      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;
  }
  </code></pre>
  
  <hr>
  
  <p>When I retrieve this log message back in my main applet, functions with no/single parameter lists appear just fine, but the double parameter functions either don't get registered at all or return the message "secondParmeterNameHere]" where secondParameterNameHere was the specific function's second parameter name.</p>
  
  <p>Any ideas on what is wrong with my algorithm for getting all of the parameter names? Thanks.</p>