java.lang.Object
com.randomnoun.common.jessop.engine.rhinoOracle.ScriptUtils

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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    assertHelper(Object condition, Object... args)
    Helper for console.assert().
    static void
    checkArguments(Object[] args, int min, int max)
    Make sure that number of arguments is valid.
    static sun.org.mozilla.javascript.internal.Scriptable
    getClassOrObjectProto(sun.org.mozilla.javascript.internal.Scriptable scope, String className)
    Return a class prototype, or the object prototype if the class is not defined.
    static Map
    getMapArgument(Object[] args, int pos, boolean allowNull)
    Get an argument as Map
    static Object
    getObjectArgument(Object[] args, int pos, boolean allowNull)
    Get an argument as object
    static sun.org.mozilla.javascript.internal.ScriptableObject
    getScriptableArgument(Object[] args, int pos, boolean allowNull)
    Get an argument as ScriptableObject
    static String
    getStringArgument(Object[] args, int pos, boolean allowNull)
    Get an argument as string
    static Object
    javaToJS(Object obj, sun.org.mozilla.javascript.internal.Scriptable scope)
    Coerce/wrap a java object to a JS object, and mask Lists and Maps as native JS objects.
    static Object
    Unwrap a JS object to a java object.
    static int
    toInt(Object obj, int defaultValue)
    Try to convert an object to an int value, returning the default value if conversion fails.
    static void
    traceHelper(sun.org.mozilla.javascript.internal.Function function, 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 Details

  • Method Details

    • javaToJS

      public static Object javaToJS(Object obj, sun.org.mozilla.javascript.internal.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 Object jsToJava(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 sun.org.mozilla.javascript.internal.Scriptable getClassOrObjectProto(sun.org.mozilla.javascript.internal.Scriptable scope, 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(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:
      IllegalArgumentException - if the number of arguments is not valid
    • getScriptableArgument

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

      public static String getStringArgument(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 Map getMapArgument(Object[] args, int pos, boolean allowNull) throws IllegalArgumentException
      Get an argument as Map
      Parameters:
      args - the argument array
      pos - the position of the requested argument
      Returns:
      the argument as map
      Throws:
      IllegalArgumentException - if the argument can't be converted to a map
    • getObjectArgument

      public static Object getObjectArgument(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(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(sun.org.mozilla.javascript.internal.Function function, 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(Object condition, 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