org.mozilla.javascript.ScriptableObject.defineClass()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 JavaScript  
字(12.9k)|赞(0)|评价(0)|浏览(231)

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

ScriptableObject.defineClass介绍

[英]Defines JavaScript objects from a Java class that implements Scriptable. If the given class has a method

static void init(Context cx, Scriptable scope, boolean sealed);

or its compatibility form

static void init(Scriptable scope);

then it is invoked and no further initialization is done.

However, if no such a method is found, then the class's constructors and methods are used to initialize a class in the following manner.

First, the zero-parameter constructor of the class is called to create the prototype. If no such constructor exists, a EvaluatorException is thrown.

Next, all methods are scanned for special prefixes that indicate that they have special meaning for defining JavaScript objects. These special prefixes are

  • jsFunction_ for a JavaScript function
  • jsStaticFunction_ for a JavaScript function that is a property of the constructor
  • jsGet_ for a getter of a JavaScript property
  • jsSet_ for a setter of a JavaScript property
  • jsConstructor for a JavaScript function that is the constructor

If the method's name begins with "jsFunction_", a JavaScript function is created with a name formed from the rest of the Java method name following "jsFunction_". So a Java method named "jsFunction_foo" will define a JavaScript method "foo". Calling this JavaScript function will cause the Java method to be called. The parameters of the method must be of number and types as defined by the FunctionObject class. The JavaScript function is then added as a property of the prototype.

If the method's name begins with "jsStaticFunction_", it is handled similarly except that the resulting JavaScript function is added as a property of the constructor object. The Java method must be static. If the method's name begins with "jsGet_" or "jsSet_", the method is considered to define a property. Accesses to the defined property will result in calls to these getter and setter methods. If no setter is defined, the property is defined as READONLY.

If the method's name is "jsConstructor", the method is considered to define the body of the constructor. Only one method of this name may be defined. You may use the varargs forms for constructors documented in FunctionObject#FunctionObject(String,Member,Scriptable)If no method is found that can serve as constructor, a Java constructor will be selected to serve as the JavaScript constructor in the following manner. If the class has only one Java constructor, that constructor is used to define the JavaScript constructor. If the the class has two constructors, one must be the zero-argument constructor (otherwise an EvaluatorException would have already been thrown when the prototype was to be created). In this case the Java constructor with one or more parameters will be used to define the JavaScript constructor. If the class has three or more constructors, an EvaluatorExceptionwill be thrown.

Finally, if there is a method

static void finishInit(Scriptable scope, FunctionObject constructor, 
Scriptable prototype)

it will be called to finish any initialization. The scope argument will be passed, along with the newly created constructor and the newly created prototype.
[中]从实现可编写脚本的Java类中定义JavaScript对象。如果给定的类有一个方法

static void init(Context cx, Scriptable scope, boolean sealed);

或其兼容形式

static void init(Scriptable scope);

,则会调用它,并且不会进行进一步的初始化。
但是,如果找不到这样的方法,则使用类的构造函数和方法按以下方式初始化类。
首先,调用类的零参数构造函数来创建原型。如果不存在这样的构造函数,则抛出EvaluatorException。
接下来,所有方法都会被扫描以寻找特殊的前缀,这些前缀表示它们对定义JavaScript对象具有特殊意义。这些特殊前缀是
*jsFunction_用于JavaScript函数
*jsStaticFunction_用于作为构造函数属性的JavaScript函数
*jsGet_获取JavaScript属性的getter
*jsSet_用于JavaScript属性的setter
*jsConstructor用于作为构造函数的JavaScript函数
如果该方法的名称以“jsFunction_3;”开头,则会创建一个JavaScript函数,其名称由“jsFunction_3;”后面的Java方法名称的其余部分组成。因此,名为“jsFunction_foo”的Java方法将定义一个JavaScript方法“foo”。调用此JavaScript函数将导致调用Java方法。方法的参数必须是FunctionObject类定义的数量和类型。然后将JavaScript函数添加为原型的属性。
如果该方法的名称以“jsStaticFunction_3;”开头,则处理方式与此类似,只是生成的JavaScript函数被添加为构造函数对象的属性。Java方法必须是静态的。如果该方法的名称以“jsGet_u”或“jsSet_u”开头,则认为该方法定义了一个属性。对已定义属性的访问将导致调用这些getter和setter方法。如果未定义setter,则该属性定义为只读。
如果方法名为“jsConstructor”,则该方法被视为定义构造函数的主体。只能定义一个此名称的方法。您可以使用FunctionObject#FunctionObject(字符串、成员、可脚本化)中记录的构造函数的varargs表单。如果找不到可用作构造函数的方法,则将选择Java构造函数以以下方式作为JavaScript构造函数。如果类只有一个Java构造函数,则该构造函数用于定义JavaScript构造函数。如果类有两个构造函数,那么其中一个必须是零参数构造函数(否则在创建原型时就会抛出EvaluatorException)。在这种情况下,将使用带有一个或多个参数的Java构造函数来定义JavaScript构造函数。如果类有三个或更多构造函数,则会抛出EvaluatorException。
最后,如果有一种方法

static void finishInit(Scriptable scope, FunctionObject constructor, 
Scriptable prototype)

它将被调用以完成任何初始化。scope参数将与新创建的构造函数和新创建的原型一起传递。

代码示例

代码示例来源:origin: facebook/stetho

private void importConsole(@NonNull ScriptableObject scope) throws StethoJsException {
 // Set the `console` object
 try {
  ScriptableObject.defineClass(scope, JsConsole.class);
  JsConsole console = new JsConsole(scope);
  scope.defineProperty("console", console, ScriptableObject.DONTENUM);
 } catch (Exception e) {
  throw new StethoJsException(e, "Failed to setup javascript console");
 }
}

代码示例来源:origin: rnewson/couchdb-lucene

public DocumentConverter(final Context context, final View view) throws IOException, JSONException {
  this.context = context;
  scope = context.initStandardObjects();
  context.setLanguageVersion(Context.VERSION_1_8);
  // Allow custom document helper class.
  try {
    ScriptableObject.defineClass(scope, RhinoDocument.class);
  } catch (IllegalAccessException e) {
    throw new RuntimeException(e);
  } catch (InstantiationException e) {
    throw new RuntimeException(e);
  } catch (InvocationTargetException e) {
    throw new RuntimeException(e);
  }
  // Add a log object
  ScriptableObject.putProperty(scope, "log", new JSLog());
  // Compile user-specified function
  try {
    viewFun = view.compileFunction(context, scope);
  } catch (final RhinoException e) {
    LOG.error("View code for " + view + " does not compile.");
    throw e;
  }
}

代码示例来源:origin: org.apache.xmlgraphics/batik-bridge

/**
 * Defines the class for the global object.
 */
protected void defineGlobalWrapperClass(Scriptable global) {
  try {
    ScriptableObject.defineClass(global, GlobalWrapper.class);
  } catch (Exception ex) {
    // cannot happen
  }
}

代码示例来源:origin: org.apache.xmlgraphics/batik-bridge

/**
 * Defines the class for the global object.
 */
protected void defineGlobalWrapperClass(Scriptable global) {
  try {
    ScriptableObject.defineClass(global, WindowWrapper.class);
  } catch (Exception ex) {
    // cannot happen
  }
}

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

/**
 * Defines the class for the global object.
 */
protected void defineGlobalWrapperClass(Scriptable global) {
  try {
    ScriptableObject.defineClass(global, WindowWrapper.class);
  } catch (Exception ex) {
    // cannot happen
  }
}

代码示例来源:origin: fr.avianey.apache-xmlgraphics/batik

/**
 * Defines the class for the global object.
 */
protected void defineGlobalWrapperClass(Scriptable global) {
  try {
    ScriptableObject.defineClass(global, WindowWrapper.class);
  } catch (Exception ex) {
    // cannot happen
  }
}

代码示例来源:origin: fr.avianey.apache-xmlgraphics/batik

/**
 * Defines the class for the global object.
 */
protected void defineGlobalWrapperClass(Scriptable global) {
  try {
    ScriptableObject.defineClass(global, GlobalWrapper.class);
  } catch (Exception ex) {
    // cannot happen
  }
}

代码示例来源:origin: com.atlassian.pluginkit/ringojs-kit

public static <T extends Scriptable> void defineClass(Scriptable scope, Class<T> clazz, boolean sealed) throws IllegalAccessException, InstantiationException, InvocationTargetException
{
  ScriptableObject.defineClass(scope, clazz, sealed);
}

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

/**
 * Defines the class for the global object.
 */
protected void defineGlobalWrapperClass(Scriptable global) {
  try {
    ScriptableObject.defineClass(global, GlobalWrapper.class);
  } catch (Exception ex) {
    // cannot happen
  }
}

代码示例来源:origin: com.atlassian.pluginkit/ringojs-kit

public static <T extends Scriptable> String defineClass(Scriptable scope, Class<T> clazz, boolean sealed, boolean mapInheritance) throws IllegalAccessException, InstantiationException, InvocationTargetException
{
  return ScriptableObject.defineClass(scope, clazz, sealed, mapInheritance);
}

代码示例来源:origin: com.atlassian.pluginkit/ringojs-kit

public static <T extends Scriptable> void defineClass(Scriptable scope, Class<T> clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException
{
  ScriptableObject.defineClass(scope, clazz);
}

代码示例来源:origin: httpunit/httpunit

private static void initHTMLObjects( Scriptable scope ) throws IllegalAccessException, InstantiationException,
    InvocationTargetException, ClassDefinitionException, PropertyException {
  ScriptableObject.defineClass( scope, Window.class );
  ScriptableObject.defineClass( scope, Document.class );
  ScriptableObject.defineClass( scope, Style.class );
  ScriptableObject.defineClass( scope, Location.class );
  ScriptableObject.defineClass( scope, Navigator.class );
  ScriptableObject.defineClass( scope, Screen.class );
  ScriptableObject.defineClass( scope, Link.class );
  ScriptableObject.defineClass( scope, Form.class );
  ScriptableObject.defineClass( scope, Control.class );
  ScriptableObject.defineClass( scope, Link.class );
  ScriptableObject.defineClass( scope, Image.class );
  ScriptableObject.defineClass( scope, Options.class );
  ScriptableObject.defineClass( scope, Option.class );
  ScriptableObject.defineClass( scope, ElementArray.class );
  ScriptableObject.defineClass( scope, HTMLElement.class );
}

代码示例来源:origin: org.kohsuke.httpunit/httpunit

private static void initHTMLObjects( Scriptable scope ) throws IllegalAccessException, InstantiationException,
    InvocationTargetException, ClassDefinitionException, PropertyException {
  ScriptableObject.defineClass( scope, Window.class );
  ScriptableObject.defineClass( scope, Document.class );
  ScriptableObject.defineClass( scope, Style.class );
  ScriptableObject.defineClass( scope, Location.class );
  ScriptableObject.defineClass( scope, Navigator.class );
  ScriptableObject.defineClass( scope, Screen.class );
  ScriptableObject.defineClass( scope, Link.class );
  ScriptableObject.defineClass( scope, Form.class );
  ScriptableObject.defineClass( scope, Control.class );
  ScriptableObject.defineClass( scope, Link.class );
  ScriptableObject.defineClass( scope, Image.class );
  ScriptableObject.defineClass( scope, Options.class );
  ScriptableObject.defineClass( scope, Option.class );
  ScriptableObject.defineClass( scope, ElementArray.class );
  ScriptableObject.defineClass( scope, HTMLElement.class );
}

代码示例来源:origin: javanettasks/httpunit

private static void initHTMLObjects( Scriptable scope ) throws IllegalAccessException, InstantiationException,
    InvocationTargetException, ClassDefinitionException, PropertyException {
  ScriptableObject.defineClass( scope, Window.class );
  ScriptableObject.defineClass( scope, Document.class );
  ScriptableObject.defineClass( scope, Style.class );
  ScriptableObject.defineClass( scope, Location.class );
  ScriptableObject.defineClass( scope, Navigator.class );
  ScriptableObject.defineClass( scope, Screen.class );
  ScriptableObject.defineClass( scope, Link.class );
  ScriptableObject.defineClass( scope, Form.class );
  ScriptableObject.defineClass( scope, Control.class );
  ScriptableObject.defineClass( scope, Link.class );
  ScriptableObject.defineClass( scope, Image.class );
  ScriptableObject.defineClass( scope, Options.class );
  ScriptableObject.defineClass( scope, Option.class );
  ScriptableObject.defineClass( scope, ElementArray.class );
  ScriptableObject.defineClass( scope, HTMLElement.class );
}

代码示例来源:origin: io.apigee.trireme/trireme-util

@Override
public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime)
  throws InvocationTargetException, IllegalAccessException, InstantiationException
{
  ScriptableObject.defineClass(global, Referenceable.class);
  ScriptableObject.defineClass(global, JdbcImpl.class);
  ScriptableObject.defineClass(global, JdbcConnection.class, false, true);
  ScriptableObject.defineClass(global, JdbcResultHandle.class);
  JdbcImpl impl = (JdbcImpl)cx.newObject(global, JdbcImpl.CLASS_NAME);
  impl.init(runtime);
  return impl;
}

代码示例来源:origin: io.apigee.trireme/trireme-util

@Override
public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime)
  throws InvocationTargetException, IllegalAccessException, InstantiationException
{
  ScriptableObject.defineClass(global, IconvModuleImpl.class);
  Scriptable exports = cx.newObject(global, IconvModuleImpl.CLASS_NAME);
  ScriptableObject.defineClass(exports, IconvImpl.class);
  return exports;
}

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

public static void defineClass(ScriptableObject scope) {
  try {
    ScriptableObject.defineClass(scope, Environment.class);
  } catch (Exception e) {
    throw new Error(e.getMessage());
  }
}

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

public static void defineClass(ScriptableObject scope) {
  try {
    ScriptableObject.defineClass(scope, Environment.class);
  } catch (Exception e) {
    throw new Error(e.getMessage());
  }
}

代码示例来源:origin: io.apigee.trireme/trireme-util

@Override
public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime)
  throws InvocationTargetException, IllegalAccessException, InstantiationException
{
  Scriptable exp = cx.newObject(global);
  exp.setPrototype(global);
  exp.setParentScope(null);
  ScriptableObject.defineClass(exp, ContextImpl.class);
  ScriptableObject.defineClass(exp, ScriptImpl.class);
  return exp;
}

代码示例来源:origin: io.apigee.trireme/trireme-util

@Override
public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime)
  throws InvocationTargetException, IllegalAccessException, InstantiationException
{
  ScriptableObject.defineClass(global, XmlWrapImpl.class);
  XmlWrapImpl wrap = (XmlWrapImpl)cx.newObject(global, XmlWrapImpl.CLASS_NAME);
  wrap.initTransformer(cx, runtime);
  return wrap;
}

相关文章

微信公众号

ScriptableObject类方法