Introducing the WrapFactory class that should be used in place of the deprecated WrapHandler interface.

This commit is contained in:
igor%mir2.org 2002-06-09 09:23:00 +00:00
Родитель 1be4f943b3
Коммит f5b045f5e5
7 изменённых файлов: 173 добавлений и 53 удалений

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

@ -1646,25 +1646,82 @@ public class Context {
FunctionObject.setCachingEnabled(cachingEnabled);
}
// Proxy to allow to use deprecated WrapHandler in place of WrapFactory
private static class WrapHandlerProxy extends WrapFactory {
WrapHandler _handler;
WrapHandlerProxy(WrapHandler handler) {
_handler = handler;
}
public Object wrap(Context cx, Scriptable scope,
Object obj, Class staticType)
{
if (obj == null) { return obj; }
Object result = _handler.wrap(scope, obj, staticType);
if (result == null) {
result = super.wrap(cx, scope, obj, staticType);
}
return result;
}
public Scriptable wrapNewObject(Context cx, Scriptable scope,
Object obj)
{
Object wrap = _handler.wrap(scope, obj, obj.getClass());
if (wrap instanceof Scriptable) {
return (Scriptable)wrap;
}
if (wrap == null) {
return super.wrapNewObject(cx, scope, obj);
}
throw new RuntimeException
("Please upgrade from WrapHandler to WrapFactory");
}
}
/**
* Set a WrapHandler for this Context.
* <p>
* The WrapHandler allows custom object wrapping behavior for
* Java object manipulated with JavaScript.
* @see org.mozilla.javascript.WrapHandler
* @since 1.5 Release 2
* @deprecated As of Rhino 1.5 Release 4, use
* {@link WrapFactory} and {@link #setWrapHandler(WrapFactory)}
*/
public void setWrapHandler(WrapHandler wrapHandler) {
this.wrapHandler = wrapHandler;
WrapFactory proxy = (wrapHandler == null) ? null
: new WrapHandlerProxy(wrapHandler);
setWrapFactory(proxy);
}
/**
* @deprecated As of Rhino 1.5 Release 4, use
* {@link WrapFactory} and {@link #getWrapHandler(WrapFactory)}
*/
public WrapHandler getWrapHandler() {
WrapHandlerProxy proxy = (WrapHandlerProxy)getWrapFactory();
return (proxy == null) ? null : proxy._handler;
}
/**
* Set a WrapFactory for this Context.
* <p>
* The WrapFactory allows custom object wrapping behavior for
* Java object manipulated with JavaScript.
* @see org.mozilla.javascript.WrapFactory
* @since 1.5 Release 4
*/
public void setWrapFactory(WrapFactory wrapFactory) {
if (wrapFactory == null) throw new IllegalArgumentException();
this.wrapFactory = wrapFactory;
}
/**
* Return the current WrapHandler, or null if none is defined.
* @see org.mozilla.javascript.WrapHandler
* @since 1.5 Release 2
* @since 1.5 Release 4
*/
public WrapHandler getWrapHandler() {
return wrapHandler;
public WrapFactory getWrapFactory() {
if (wrapFactory == null) {
wrapFactory = new WrapFactory();
}
return wrapFactory;
}
/**
@ -2209,7 +2266,7 @@ public class Context {
private boolean generatingSource=true;
private boolean compileFunctionsWithDynamicScopeFlag;
private int optimizationLevel;
WrapHandler wrapHandler;
private WrapFactory wrapFactory;
Debugger debugger;
private Object debuggerData;
private int enterCount;

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

@ -217,7 +217,7 @@ class JavaMembers {
if (method == null)
throw reportMemberNotFound(name);
Class[] types = method.getParameterTypes();
Object[] args = { NativeJavaObject.coerceType(types[0], value,
Object[] args = { NativeJavaObject.coerceType(types[0], value,
true) };
method.invoke(javaObject, args);
} catch (IllegalAccessException accessEx) {

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

@ -220,14 +220,9 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
for (int i = 0; i < args.length; i++) {
args[i] = NativeJavaObject.coerceType(paramTypes[i], args[i], true);
}
Object instance;
try {
// we need to force this to be wrapped, because construct _has_
// to return a scriptable
return
(Scriptable) NativeJavaObject.wrap(topLevel,
ctor.newInstance(args),
classObject);
instance = ctor.newInstance(args);
} catch (InstantiationException instEx) {
throw Context.reportRuntimeError2(
"msg.cant.instantiate",
@ -243,6 +238,9 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
throw Context.reportRuntimeError1(
"msg.java.internal.private", accessEx.getMessage());
}
// we need to force this to be wrapped, because construct _has_
// to return a scriptable
return cx.getWrapFactory().wrapNewObject(cx, topLevel, instance);
}
public String toString() {

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

@ -241,7 +241,8 @@ public class NativeJavaMethod extends NativeFunction implements Function {
" expect = " + staticType);
}
Object wrapped = NativeJavaObject.wrap(scope, retval, staticType);
Object wrapped = cx.getWrapFactory().wrap(cx, scope,
retval, staticType);
if (debug) {
Class actualType = (wrapped == null) ? null
@ -342,8 +343,8 @@ public class NativeJavaMethod extends NativeFunction implements Function {
if (arg instanceof Wrapper) {
arg = ((Wrapper)arg).unwrap();
if (!(arg instanceof Number)) {
// Since numbers are internally represented as
// java.lang.Double, etc. then java.lang.Doubles are
// Since numbers are internally represented as
// java.lang.Double, etc. then java.lang.Doubles are
// distinquished by being wrapped. Thus don't unwrap
// here or we'll get overloading wrong.
args[i] = arg;

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

@ -161,16 +161,17 @@ public class NativeJavaObject implements Scriptable, Wrapper, Externalizable {
return members.getIds(false);
}
public static Object wrap(Scriptable scope, Object obj, Class staticType)
public static Object wrap(Scriptable scope, Object obj, Class staticType) {
Context cx = Context.getContext();
return cx.getWrapFactory().wrap(cx, scope, obj, staticType);
}
static Object defaultWrap(Context cx, Scriptable scope,
Object obj, Class staticType)
{
if (obj == null)
return obj;
Context cx = Context.getCurrentContext();
if (cx != null && cx.wrapHandler != null) {
Object result = cx.wrapHandler.wrap(scope, obj, staticType);
if (result != null)
return result;
}
Class cls = obj.getClass();
if (staticType != null && staticType.isPrimitive()) {
if (staticType == Void.TYPE)

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

@ -0,0 +1,84 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Norris Boyd
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
// API class
package org.mozilla.javascript;
/**
* Embeddings that wish to provide their own custom wrappings for Java
* objects may extend this call and call Context.setWrapFactory.
* XXX
* @see org.mozilla.javascript.Context#setWrapFactory(WrapFactory)
* @since 1.5 Release 4
*/
public class WrapFactory {
/**
* Wrap the object.
* <p>
* The value returned must be one of
* <UL>
* <LI>java.lang.Boolean</LI>
* <LI>java.lang.String</LI>
* <LI>java.lang.Number</LI>
* <LI>org.mozilla.javascript.Scriptable objects</LI>
* <LI>The value returned by Context.getUndefinedValue()</LI>
* <LI>null</LI>
* @param cx the current Context for this thread
* @param scope the scope of the executing script
* @param obj the object to be wrapped
* @staticType the static type of the object to be wrapped
* @return the wrapped value.
*/
public Object wrap(Context cx, Scriptable scope,
Object obj, Class staticType)
{
return NativeJavaObject.defaultWrap(cx, scope, obj, staticType);
}
/**
* Wrap an object newly created by a constructor call.
* @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.
*/
public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj)
{
return (Scriptable)NativeJavaObject.defaultWrap(cx, scope, obj, null);
}
}

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

@ -37,32 +37,11 @@
package org.mozilla.javascript;
/**
* Embeddings that wish to provide their own custom wrappings for Java
* objects may implement this interface and call Context.setWrapHandler.
* @see org.mozilla.javascript.Context#setWrapHandler
/*
* @deprecated As of Rhino 1.5 Release 4, use {@link WrapFactory}
*/
public interface WrapHandler {
/**
* Wrap the object.
* <p>
* The value returned must be one of
* <UL>
* <LI>java.lang.Boolean</LI>
* <LI>java.lang.String</LI>
* <LI>java.lang.Number</LI>
* <LI>org.mozilla.javascript.Scriptable objects</LI>
* <LI>The value returned by Context.getUndefinedValue()</LI>
* <LI>null</LI>
* <p>
* If null is returned, the value obj will be wrapped as if
* no WrapHandler had been called.
* </UL>
* @param scope the scope of the executing script
* @param obj the object to be wrapped
* @staticType the static type of the object to be wrapped
* @return the wrapped value.
*/
public Object wrap(Scriptable scope, Object obj, Class staticType);
}