org.mozilla.javascript.Wrapper类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 JavaScript  
字(9.7k)|赞(0)|评价(0)|浏览(461)

本文整理了Java中org.mozilla.javascript.Wrapper类的一些代码示例,展示了Wrapper类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Wrapper类的具体详情如下:
包路径:org.mozilla.javascript.Wrapper
类名称:Wrapper

Wrapper介绍

[英]Objects that can wrap other values for reflection in the JS environment will implement Wrapper. Wrapper defines a single method that can be called to unwrap the object.
[中]可以在JS环境中封装其他值以进行反射的对象将实现Wrapper。包装器定义了一个方法,可以调用该方法来展开对象。

代码示例

代码示例来源:origin: org.freemarker/freemarker

@Override
public TemplateModel wrap(Object obj) throws TemplateModelException {
  // So our existence builtins work as expected.
  if (obj == UNDEFINED_INSTANCE || obj == UniqueTag.NOT_FOUND) {
    return null;
  }
  // UniqueTag.NULL_VALUE represents intentionally set null in Rhino, and
  // BeansWrapper#nullModel also represents intentionally returned null.
  // I [A.Sz.] am fairly certain that this value is never passed out of
  // any of the Rhino code back to clients, but is instead always being
  // converted back to null. However, since this object is available to 
  // any 3rd party Scriptable implementations as well, they might return
  // it, so we'll just be on the safe side, and handle it.
  if (obj == UniqueTag.NULL_VALUE) {
    return super.wrap(null);
  }
  // So, say, a JavaAdapter for FreeMarker interfaces works
  if (obj instanceof Wrapper) {
    obj = ((Wrapper) obj).unwrap();
  }
  return super.wrap(obj);
}

代码示例来源:origin: org.ofbiz/ofbcore-jira-share

Context cx = Context.enter();
theReturnValue = ScriptRuntime.call(cx, fun, global, args, null);
if (theReturnValue instanceof Wrapper) {
  theReturnValue = ((Wrapper) theReturnValue).unwrap();
Context.exit();

代码示例来源:origin: org.apache.cocoon/cocoon-flowscript-impl

public Object getProperty(Object obj, String propertyName) {
  Context cx = null;
  try {
    cx = Context.enter();
    Scriptable s = (Scriptable)obj;
    Object result = ScriptableObject.getProperty(s, propertyName);
      result = ((Wrapper)result).unwrap();
    } else if (result == Undefined.instance) {
      result = null;
    Context.exit();

代码示例来源:origin: apache/cxf

Context cx = ContextFactory.getGlobal().enterContext();
try {
  Scriptable scope = cx.newObject(scriptScope);
  scope.setPrototype(scriptScope);
  scope.setParentScope(null);
  if (isE4X) {
    try {
      inDoc = Context.toObject(node, scope);
      Object[] args = {inDoc};
      inDoc = cx.newObject(scope, "XML", args);
    } catch (Exception ex) {
      ex.printStackTrace();
  Object jsResp = invokeFunc.call(cx, scope, scope, args);
  if (jsResp instanceof Wrapper) {
    jsResp = ((Wrapper)jsResp).unwrap();

代码示例来源:origin: com.google.code.scriptengines/scriptengines-javascript

/**
 * The bindings function takes a JavaScript scope object 
 * of type ExternalScriptable and returns the underlying Bindings
 * instance.
 *
 *    var page = scope(pageBindings);
 *    with (page) {
 *       // code that uses page scope 
 *    } 
 *    var b = bindings(page);
 *    // operate on bindings here.
 */
public static Object bindings(Context cx, Scriptable thisObj, Object[] args,
    Function funObj) {
  if (args.length == 1) {
    Object arg = args[0];
    if (arg instanceof Wrapper) {
      arg = ((Wrapper)arg).unwrap();
    }
    if (arg instanceof ExternalScriptable) {
      ScriptContext ctx = ((ExternalScriptable)arg).getContext();
      Bindings bind = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
      return Context.javaToJS(bind, 
            ScriptableObject.getTopLevelScope(thisObj));
    }
  }
  return cx.getUndefinedValue();
}

代码示例来源:origin: viltgroup/minium

@Override
  protected String doCall(Context cx, Scriptable scope) throws RuntimeException {
    if (obj == null) {
      return "null";
    } else if (obj instanceof Wrapper) {
      Object unwrapped = ((Wrapper) obj).unwrap();
      return unwrapped.toString();
    } else if (obj instanceof Scriptable) {
      return (String) cx.evaluateString((Scriptable) obj,
          "(function(obj) { " +
          "  try { " +
          "    var str = JSON.stringify(obj); " +
          "    if (typeof str === 'string') return str; " +
          "  } catch (e) { } " +
          "  return obj.toString(); " +
          "})(this)",
          "<toString>", 1, null);
    } else if (obj instanceof Undefined) {
      return "undefined";
    }
    return obj.toString();
  }
});

代码示例来源:origin: org.apache.sling/org.apache.sling.scripting.javascript

private Locale getLocale() {
    if (locale == null) {
      
      try {
        // check whether we have a request object which has the locale
        Object reqObj = ScriptRuntime.name(Context.getCurrentContext(),
          this, SlingBindings.REQUEST);
        if (reqObj instanceof Wrapper) {
          Object wrapped = ((Wrapper) reqObj).unwrap();
          if (wrapped instanceof HttpServletRequest) {
            locale = ((HttpServletRequest) wrapped).getLocale();
          }
        }
      } catch (Exception e) {
        // ignore any exceptions resulting from this and use default
      }

      // default, if the no request locale or no request is available
      if (locale == null) {
        locale = Locale.getDefault();
      }

    }

    return locale;
  }
}

代码示例来源:origin: rhino/js

public Scriptable construct(Context cx, Scriptable scope, Object[] args)
{
  ClassLoader loader = null;
  if (args.length != 0) {
    Object arg = args[0];
    if (arg instanceof Wrapper) {
      arg = ((Wrapper)arg).unwrap();
    }
    if (arg instanceof ClassLoader) {
      loader = (ClassLoader)arg;
    }
  }
  if (loader == null) {
    Context.reportRuntimeError0("msg.not.classloader");
    return null;
  }
  return new NativeJavaPackage(true, "", loader);
}

代码示例来源:origin: ro.isdc.wro4j/rhino

private static Class<?> getClass(Object[] args) {
  if (args.length == 0) {
    throw reportRuntimeError("msg.expected.string.arg");
  }
  Object arg0 = args[0];
  if (arg0 instanceof Wrapper) {
    Object wrapped = ((Wrapper)arg0).unwrap();
    if (wrapped instanceof Class)
      return (Class<?>)wrapped;
  }
  String className = Context.toString(args[0]);
  try {
    return Class.forName(className);
  }
  catch (ClassNotFoundException cnfe) {
    throw reportRuntimeError("msg.class.not.found", className);
  }
}

代码示例来源:origin: io.apisense/rhino-android

public Scriptable construct(Context cx, Scriptable scope, Object[] args)
throws RhinoException {
  if (args.length == 2) {
    Class<?> clazz = null;
    Object obj1 = args[0];
    if (obj1 instanceof Wrapper) {
      Object o = ((Wrapper)obj1).unwrap();
      if (o instanceof Class && ((Class)o).isInterface()) {
        clazz = (Class) o;
      }
    } else if (obj1 instanceof Class) {
      if (((Class)obj1).isInterface()) {
        clazz = (Class) obj1;
      }
    }
    if (clazz == null) {
      throw Context.reportRuntimeError("JavaAdapter: first arg should be interface Class");
    }
    Scriptable topLevel = ScriptableObject.getTopLevelScope(scope);
    return cx.toObject(engine.getInterface(args[1],  clazz), topLevel);
  } else {
    throw Context.reportRuntimeError("JavaAdapter requires two arguments");
  }
}

代码示例来源:origin: rhino/js

return ((Wrapper)x).unwrap() == ((Wrapper)y).unwrap();
warnAboutNonJSObject(x);
return x == y;

代码示例来源:origin: ro.isdc.wro4j/rhino

public Scriptable construct(Context cx, Scriptable scope, Object[] args)
{
  ClassLoader loader = null;
  if (args.length != 0) {
    Object arg = args[0];
    if (arg instanceof Wrapper) {
      arg = ((Wrapper)arg).unwrap();
    }
    if (arg instanceof ClassLoader) {
      loader = (ClassLoader)arg;
    }
  }
  if (loader == null) {
    Context.reportRuntimeError0("msg.not.classloader");
    return null;
  }
  NativeJavaPackage pkg = new NativeJavaPackage(true, "", loader);
  ScriptRuntime.setObjectProtoAndParent(pkg, scope);
  return pkg;
}

代码示例来源:origin: rhq-project/rhq

Object arg = args[0];
  if (arg instanceof Wrapper) {
    arg = ((Wrapper)arg).unwrap();
return Context.getUndefinedValue();

代码示例来源:origin: ro.isdc.wro4j/rhino

private static InputStream toInputStream(Object value)
  throws IOException
{
  InputStream is = null;
  String s = null;
  if (value instanceof Wrapper) {
    Object unwrapped = ((Wrapper)value).unwrap();
    if (unwrapped instanceof InputStream) {
      is = (InputStream)unwrapped;
    } else if (unwrapped instanceof byte[]) {
      is = new ByteArrayInputStream((byte[])unwrapped);
    } else if (unwrapped instanceof Reader) {
      s = readReader((Reader)unwrapped);
    } else if (unwrapped instanceof char[]) {
      s = new String((char[])unwrapped);
    }
  }
  if (is == null) {
    if (s == null) { s = ScriptRuntime.toString(value); }
    is = new ByteArrayInputStream(s.getBytes());
  }
  return is;
}

代码示例来源:origin: org.apache.sling/org.apache.sling.scripting.javascript

/**
   * Returns the named toplevel property converted to the requested
   * <code>type</code> or <code>null</code> if no such property exists or the
   * property is of the wrong type.
   */
  @SuppressWarnings("unchecked")
  private <Type> Type getProperty(Context cx, Scriptable scope, String name,
      Class<Type> type) {
    Object prop = ScriptRuntime.name(cx, scope, name);

    if (prop instanceof Wrapper) {
      prop = ((Wrapper) prop).unwrap();
    }

    if (type.isInstance(prop)) {
      return (Type) prop; // unchecked case
    }

    return null;
  }
}

代码示例来源:origin: com.github.tntim96/rhino

final XML ecmaToXml(Object object) {
    throw ScriptRuntime.typeError("Cannot convert " + object + " to XML");
      return list.getXML();
    } else {
      throw ScriptRuntime.typeError("Cannot convert list of >1 element to XML");
    object = ((Wrapper) object).unwrap();
  String s = ScriptRuntime.toString(object);

代码示例来源:origin: io.apisense/rhino-android

/**
 * The bindings function takes a JavaScript scope object
 * of type ExternalScriptable and returns the underlying Bindings
 * instance.
 *
 *    var page = scope(pageBindings);
 *    with (page) {
 *       // code that uses page scope
 *    }
 *    var b = bindings(page);
 *    // operate on bindings here.
 */
public static Object bindings(Context cx, Scriptable thisObj, Object[] args,
    Function funObj) {
  if (args.length == 1) {
    Object arg = args[0];
    if (arg instanceof Wrapper) {
      arg = ((Wrapper)arg).unwrap();
    }
    if (arg instanceof ExternalScriptable) {
      ScriptContext ctx = ((ExternalScriptable)arg).getContext();
      Bindings bind = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
      return Context.javaToJS(bind,
            ScriptableObject.getTopLevelScope(thisObj));
    }
  }
  return cx.getUndefinedValue();
}

代码示例来源:origin: com.sun.phobos/phobos-rhino

public Scriptable construct(Context cx, Scriptable scope, Object[] args)
{
  ClassLoader loader = null;
  if (args.length != 0) {
    Object arg = args[0];
    if (arg instanceof Wrapper) {
      arg = ((Wrapper)arg).unwrap();
    }
    if (arg instanceof ClassLoader) {
      loader = (ClassLoader)arg;
    }
  }
  if (loader == null) {
    Context.reportRuntimeError0("msg.not.classloader");
    return null;
  }
  return new NativeJavaPackage(true, "", loader);
}

代码示例来源:origin: com.github.tntim96/rhino

private static Class<?> getClass(Object[] args) {
  if (args.length == 0) {
    throw reportRuntimeError("msg.expected.string.arg");
  }
  Object arg0 = args[0];
  if (arg0 instanceof Wrapper) {
    Object wrapped = ((Wrapper)arg0).unwrap();
    if (wrapped instanceof Class)
      return (Class<?>)wrapped;
  }
  String className = Context.toString(args[0]);
  try {
    return Class.forName(className);
  }
  catch (ClassNotFoundException cnfe) {
    throw reportRuntimeError("msg.class.not.found", className);
  }
}

代码示例来源:origin: com.sun.phobos/phobos-rhino

return ((Wrapper)x).unwrap() == ((Wrapper)y).unwrap();
warnAboutNonJSObject(x);
return x == y;

相关文章