зеркало из https://github.com/mozilla/gecko-dev.git
Added WrapFactory.wrapAsJavaObject to simplify customization of LiveConnect
This commit is contained in:
Родитель
dc36e6ad66
Коммит
6b6368d8c8
|
@ -50,8 +50,8 @@ package org.mozilla.javascript;
|
|||
* @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory)
|
||||
* @since 1.5 Release 4
|
||||
*/
|
||||
public class WrapFactory {
|
||||
|
||||
public class WrapFactory
|
||||
{
|
||||
/**
|
||||
* Wrap the object.
|
||||
* <p>
|
||||
|
@ -81,8 +81,9 @@ public class WrapFactory {
|
|||
return new Integer((int) ((Character) obj).charValue());
|
||||
return obj;
|
||||
}
|
||||
if (obj instanceof Scriptable)
|
||||
if (obj instanceof Scriptable) {
|
||||
return obj;
|
||||
}
|
||||
if (!isJavaPrimitiveWrap()) {
|
||||
if (obj instanceof String || obj instanceof Number
|
||||
|| obj instanceof Boolean)
|
||||
|
@ -94,9 +95,10 @@ public class WrapFactory {
|
|||
}
|
||||
}
|
||||
Class cls = obj.getClass();
|
||||
if (cls.isArray())
|
||||
if (cls.isArray()) {
|
||||
return NativeJavaArray.wrap(scope, obj);
|
||||
return new NativeJavaObject(scope, obj, staticType);
|
||||
}
|
||||
return wrapAsJavaObject(cx, scope, obj);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -106,13 +108,38 @@ public class WrapFactory {
|
|||
* @param obj the object to be wrapped
|
||||
* @return the wrapped value.
|
||||
*/
|
||||
public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) {
|
||||
if (obj instanceof Scriptable)
|
||||
public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj)
|
||||
{
|
||||
if (obj instanceof Scriptable) {
|
||||
return (Scriptable)obj;
|
||||
}
|
||||
Class cls = obj.getClass();
|
||||
if (cls.isArray())
|
||||
if (cls.isArray()) {
|
||||
return NativeJavaArray.wrap(scope, obj);
|
||||
return new NativeJavaObject(scope, obj, (Class)null);
|
||||
}
|
||||
return wrapAsJavaObject(cx, scope, obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap Java object as Scriptable instance to allow full access to its
|
||||
* methods and fields from JavaScript.
|
||||
* <p>
|
||||
* {@link #wrap(Context, Scriptable, Object, Class)} and
|
||||
* {@link #wrapNewObject(Context, Scriptable, Object)} call this method
|
||||
* when they can not convert <tt>javaObject</tt> to JavaScript primitive
|
||||
* value or JavaScript array.
|
||||
* <p>
|
||||
* Subclasses can override the method to provide custom wrappers
|
||||
* for Java objects.
|
||||
* @param cx the current Context for this thread
|
||||
* @param scope the scope of the executing script
|
||||
* @param obj the object to be wrapped
|
||||
* @return the wrapped value which shall not be null
|
||||
*/
|
||||
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
|
||||
Object javaObject)
|
||||
{
|
||||
return new NativeJavaObject(scope, javaObject, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -126,14 +153,16 @@ public class WrapFactory {
|
|||
* scripts can access any Java method available in these objects.
|
||||
* Use {@link #setJavaPrimitiveWrap(boolean)} to change this.
|
||||
*/
|
||||
public final boolean isJavaPrimitiveWrap() {
|
||||
public final boolean isJavaPrimitiveWrap()
|
||||
{
|
||||
return javaPrimitiveWrap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #isJavaPrimitiveWrap()
|
||||
*/
|
||||
public final void setJavaPrimitiveWrap(boolean value) {
|
||||
public final void setJavaPrimitiveWrap(boolean value)
|
||||
{
|
||||
javaPrimitiveWrap = value;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче