Added WrapFactory.wrapAsJavaObject to simplify customization of LiveConnect

This commit is contained in:
igor%mir2.org 2003-05-20 12:46:09 +00:00
Родитель dc36e6ad66
Коммит 6b6368d8c8
1 изменённых файлов: 40 добавлений и 11 удалений

Просмотреть файл

@ -50,8 +50,8 @@ package org.mozilla.javascript;
* @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory) * @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory)
* @since 1.5 Release 4 * @since 1.5 Release 4
*/ */
public class WrapFactory { public class WrapFactory
{
/** /**
* Wrap the object. * Wrap the object.
* <p> * <p>
@ -81,8 +81,9 @@ public class WrapFactory {
return new Integer((int) ((Character) obj).charValue()); return new Integer((int) ((Character) obj).charValue());
return obj; return obj;
} }
if (obj instanceof Scriptable) if (obj instanceof Scriptable) {
return obj; return obj;
}
if (!isJavaPrimitiveWrap()) { if (!isJavaPrimitiveWrap()) {
if (obj instanceof String || obj instanceof Number if (obj instanceof String || obj instanceof Number
|| obj instanceof Boolean) || obj instanceof Boolean)
@ -94,9 +95,10 @@ public class WrapFactory {
} }
} }
Class cls = obj.getClass(); Class cls = obj.getClass();
if (cls.isArray()) if (cls.isArray()) {
return NativeJavaArray.wrap(scope, obj); 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 * @param obj the object to be wrapped
* @return the wrapped value. * @return the wrapped value.
*/ */
public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) { public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj)
if (obj instanceof Scriptable) {
if (obj instanceof Scriptable) {
return (Scriptable)obj; return (Scriptable)obj;
}
Class cls = obj.getClass(); Class cls = obj.getClass();
if (cls.isArray()) if (cls.isArray()) {
return NativeJavaArray.wrap(scope, obj); 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. * scripts can access any Java method available in these objects.
* Use {@link #setJavaPrimitiveWrap(boolean)} to change this. * Use {@link #setJavaPrimitiveWrap(boolean)} to change this.
*/ */
public final boolean isJavaPrimitiveWrap() { public final boolean isJavaPrimitiveWrap()
{
return javaPrimitiveWrap; return javaPrimitiveWrap;
} }
/** /**
* @see #isJavaPrimitiveWrap() * @see #isJavaPrimitiveWrap()
*/ */
public final void setJavaPrimitiveWrap(boolean value) { public final void setJavaPrimitiveWrap(boolean value)
{
javaPrimitiveWrap = value; javaPrimitiveWrap = value;
} }