Class ScriptUtils


  • public class ScriptUtils
    extends java.lang.Object
    A collection of Rhino utility methods.
    • Constructor Summary

      Constructors 
      Constructor Description
      ScriptUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertHelper​(java.lang.Object condition, java.lang.Object... args)
      Helper for console.assert().
      static void checkArguments​(java.lang.Object[] args, int min, int max)
      Make sure that number of arguments is valid.
      static org.mozilla.javascript.Scriptable getClassOrObjectProto​(org.mozilla.javascript.Scriptable scope, java.lang.String className)
      Return a class prototype, or the object prototype if the class is not defined.
      static java.util.Map getMapArgument​(java.lang.Object[] args, int pos, boolean allowNull)
      Get an argument as Map
      static java.lang.Object getObjectArgument​(java.lang.Object[] args, int pos, boolean allowNull)
      Get an argument as object
      static org.mozilla.javascript.ScriptableObject getScriptableArgument​(java.lang.Object[] args, int pos, boolean allowNull)
      Get an argument as ScriptableObject
      static java.lang.String getStringArgument​(java.lang.Object[] args, int pos, boolean allowNull)
      Get an argument as string
      static java.lang.Object javaToJS​(java.lang.Object obj, org.mozilla.javascript.Scriptable scope)
      Coerce/wrap a java object to a JS object, and mask Lists and Maps as native JS objects.
      static java.lang.Object jsToJava​(java.lang.Object obj)
      Unwrap a JS object to a java object.
      static int toInt​(java.lang.Object obj, int defaultValue)
      Try to convert an object to an int value, returning the default value if conversion fails.
      static void traceHelper​(org.mozilla.javascript.Function function, java.lang.Object... args)
      Get a snapshot of the current JavaScript evaluation state by creating an Error object and invoke the function on it passing along any arguments.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ScriptUtils

        public ScriptUtils()
    • Method Detail

      • javaToJS

        public static java.lang.Object javaToJS​(java.lang.Object obj,
                                                org.mozilla.javascript.Scriptable scope)
        Coerce/wrap a java object to a JS object, and mask Lists and Maps as native JS objects.
        Parameters:
        obj - the object to coerce/wrap
        scope - the scope
        Returns:
        the wrapped/masked java object
      • jsToJava

        public static java.lang.Object jsToJava​(java.lang.Object obj)
        Unwrap a JS object to a java object. This is much more conservative than Context.jsToJava in that it will preserve undefined values.
        Parameters:
        obj - a JavaScript value
        Returns:
        a Java object corresponding to obj
      • getClassOrObjectProto

        public static org.mozilla.javascript.Scriptable getClassOrObjectProto​(org.mozilla.javascript.Scriptable scope,
                                                                              java.lang.String className)
        Return a class prototype, or the object prototype if the class is not defined.
        Parameters:
        scope - the scope
        className - the class name
        Returns:
        the class or object prototype
      • checkArguments

        public static void checkArguments​(java.lang.Object[] args,
                                          int min,
                                          int max)
        Make sure that number of arguments is valid.
        Parameters:
        args - the argument array
        min - the minimum number of arguments
        max - the maximum number of arguments
        Throws:
        java.lang.IllegalArgumentException - if the number of arguments is not valid
      • getScriptableArgument

        public static org.mozilla.javascript.ScriptableObject getScriptableArgument​(java.lang.Object[] args,
                                                                                    int pos,
                                                                                    boolean allowNull)
                                                                             throws java.lang.IllegalArgumentException
        Get an argument as ScriptableObject
        Parameters:
        args - the argument array
        pos - the position of the requested argument
        Returns:
        the argument as ScriptableObject
        Throws:
        java.lang.IllegalArgumentException - if the argument can't be converted to a map
      • getStringArgument

        public static java.lang.String getStringArgument​(java.lang.Object[] args,
                                                         int pos,
                                                         boolean allowNull)
        Get an argument as string
        Parameters:
        args - the argument array
        pos - the position of the requested argument
        Returns:
        the argument as string
      • getMapArgument

        public static java.util.Map getMapArgument​(java.lang.Object[] args,
                                                   int pos,
                                                   boolean allowNull)
                                            throws java.lang.IllegalArgumentException
        Get an argument as Map
        Parameters:
        args - the argument array
        pos - the position of the requested argument
        Returns:
        the argument as map
        Throws:
        java.lang.IllegalArgumentException - if the argument can't be converted to a map
      • getObjectArgument

        public static java.lang.Object getObjectArgument​(java.lang.Object[] args,
                                                         int pos,
                                                         boolean allowNull)
        Get an argument as object
        Parameters:
        args - the argument array
        pos - the position of the requested argument
        Returns:
        the argument as object
      • toInt

        public static int toInt​(java.lang.Object obj,
                                int defaultValue)
        Try to convert an object to an int value, returning the default value if conversion fails.
        Parameters:
        obj - the value
        defaultValue - the default value
        Returns:
        the converted value
      • traceHelper

        public static void traceHelper​(org.mozilla.javascript.Function function,
                                       java.lang.Object... args)
        Get a snapshot of the current JavaScript evaluation state by creating an Error object and invoke the function on it passing along any arguments. Used to invoke console.trace() and friends because implementing this in JavaScript would mess with the evaluation state.
        Parameters:
        function - the function to call
        args - optional arguments to pass to the function.
      • assertHelper

        public static void assertHelper​(java.lang.Object condition,
                                        java.lang.Object... args)
        Helper for console.assert(). Implemented in Java in order not to modify the JavaScript stack.
        Parameters:
        condition - the condition to test
        args - one or more message parts