Java Javascript Mozilla Library getFunctionArgsString(FunctionNode fn)

Here you can find the source of getFunctionArgsString(FunctionNode fn)

Description

Iterates through a function's parameters and returns a string representation of them, suitable for presentation as part of the method's signature.

License

BSD License

Parameter

Parameter Description
fn The function node.

Return

The string representation of the function's arguments.

Declaration

public static final String getFunctionArgsString(FunctionNode fn) 

Method Source Code

//package com.java2s;
/*/*w w w .j  av a 2s.c  o  m*/
 * 06/05/2014
 *
 * Copyright (C) 2014 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com/rsyntaxtextarea
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSTALanguageSupport.License.txt file for details.
 */

import java.util.List;
import org.mozilla.javascript.Token;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.Name;

public class Main {
    /**
     * Iterates through a function's parameters and returns a string
     * representation of them, suitable for presentation as part of the
     * method's signature.
     *
     * @param fn The function node.
     * @return The string representation of the function's arguments.
     */
    public static final String getFunctionArgsString(FunctionNode fn) {
        StringBuilder sb = new StringBuilder("(");
        int paramCount = fn.getParamCount();
        if (paramCount > 0) {
            List<AstNode> fnParams = fn.getParams();
            for (int i = 0; i < paramCount; i++) {
                String paramName = null;
                AstNode paramNode = fnParams.get(i);
                switch (paramNode.getType()) {
                case Token.NAME:
                    paramName = ((Name) paramNode).getIdentifier();
                    break;
                default:
                    System.out.println("Unhandled class for param: " + paramNode.getClass());
                    paramName = "?";
                    break;
                }
                sb.append(paramName);
                if (i < paramCount - 1) {
                    sb.append(", ");
                }
            }
        }
        sb.append(')');
        return sb.toString();
    }
}

Related

  1. from(Scriptable scriptable)
  2. functionArg(Object[] args, int pos, boolean required)
  3. getArray(Scriptable scope, NativeArray array)
  4. getClassOrObjectProto(Scriptable scope, String className)
  5. getDirectColRefExpr(Node refNode, boolean mode)
  6. getGlobalContextForValidation()
  7. getJavaObject(Scriptable scope, String name)
  8. getJsArray(Scriptable scope, Vector v)
  9. getMapArgument(Object[] args, int pos)