| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.lang.reflect.Method; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class InterfaceAdapter |
| 16 |
{ |
| 17 |
private final Object proxyHelper; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
static Object create(Context cx, Class<?> cl, ScriptableObject object) |
| 28 |
{ |
| 29 |
if (!cl.isInterface()) throw new IllegalArgumentException(); |
| 30 |
|
| 31 |
Scriptable topScope = ScriptRuntime.getTopCallScope(cx); |
| 32 |
ClassCache cache = ClassCache.get(topScope); |
| 33 |
InterfaceAdapter adapter; |
| 34 |
adapter = (InterfaceAdapter)cache.getInterfaceAdapter(cl); |
| 35 |
ContextFactory cf = cx.getFactory(); |
| 36 |
if (adapter == null) { |
| 37 |
Method[] methods = cl.getMethods(); |
| 38 |
if ( object instanceof Callable) { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
int length = methods.length; |
| 44 |
if (length == 0) { |
| 45 |
throw Context.reportRuntimeError1( |
| 46 |
"msg.no.empty.interface.conversion", cl.getName()); |
| 47 |
} |
| 48 |
if (length > 1) { |
| 49 |
String methodName = methods[0].getName(); |
| 50 |
for (int i = 1; i < length; i++) { |
| 51 |
if (!methodName.equals(methods[i].getName())) { |
| 52 |
throw Context.reportRuntimeError1( |
| 53 |
"msg.no.function.interface.conversion", |
| 54 |
cl.getName()); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
adapter = new InterfaceAdapter(cf, cl); |
| 60 |
cache.cacheInterfaceAdapter(cl, adapter); |
| 61 |
} |
| 62 |
return VMBridge.instance.newInterfaceProxy( |
| 63 |
adapter.proxyHelper, cf, adapter, object, topScope); |
| 64 |
} |
| 65 |
|
| 66 |
private InterfaceAdapter(ContextFactory cf, Class<?> cl) |
| 67 |
{ |
| 68 |
this.proxyHelper |
| 69 |
= VMBridge.instance.getInterfaceProxyHelper( |
| 70 |
cf, new Class[] { cl }); |
| 71 |
} |
| 72 |
|
| 73 |
public Object invoke(ContextFactory cf, |
| 74 |
final Object target, |
| 75 |
final Scriptable topScope, |
| 76 |
final Object thisObject, |
| 77 |
final Method method, |
| 78 |
final Object[] args) |
| 79 |
{ |
| 80 |
ContextAction action = new ContextAction() { |
| 81 |
public Object run(Context cx) |
| 82 |
{ |
| 83 |
return invokeImpl(cx, target, topScope, thisObject, method, args); |
| 84 |
} |
| 85 |
}; |
| 86 |
return cf.call(action); |
| 87 |
} |
| 88 |
|
| 89 |
Object invokeImpl(Context cx, |
| 90 |
Object target, |
| 91 |
Scriptable topScope, |
| 92 |
Object thisObject, |
| 93 |
Method method, |
| 94 |
Object[] args) |
| 95 |
{ |
| 96 |
Callable function; |
| 97 |
if (target instanceof Callable) { |
| 98 |
function = (Callable)target; |
| 99 |
} else { |
| 100 |
Scriptable s = (Scriptable)target; |
| 101 |
String methodName = method.getName(); |
| 102 |
Object value = ScriptableObject.getProperty(s, methodName); |
| 103 |
if (value == ScriptableObject.NOT_FOUND) { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
Context.reportWarning(ScriptRuntime.getMessage1( |
| 108 |
"msg.undefined.function.interface", methodName)); |
| 109 |
Class<?> resultType = method.getReturnType(); |
| 110 |
if (resultType == Void.TYPE) { |
| 111 |
return null; |
| 112 |
} else { |
| 113 |
return Context.jsToJava(null, resultType); |
| 114 |
} |
| 115 |
} |
| 116 |
if (!(value instanceof Callable)) { |
| 117 |
throw Context.reportRuntimeError1( |
| 118 |
"msg.not.function.interface",methodName); |
| 119 |
} |
| 120 |
function = (Callable)value; |
| 121 |
} |
| 122 |
WrapFactory wf = cx.getWrapFactory(); |
| 123 |
if (args == null) { |
| 124 |
args = ScriptRuntime.emptyArgs; |
| 125 |
} else { |
| 126 |
for (int i = 0, N = args.length; i != N; ++i) { |
| 127 |
Object arg = args[i]; |
| 128 |
|
| 129 |
if (!(arg instanceof String || arg instanceof Number |
| 130 |
|| arg instanceof Boolean)) { |
| 131 |
args[i] = wf.wrap(cx, topScope, arg, null); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
Scriptable thisObj = wf.wrapAsJavaObject(cx, topScope, thisObject, null); |
| 136 |
|
| 137 |
Object result = function.call(cx, topScope, thisObj, args); |
| 138 |
Class<?> javaResultType = method.getReturnType(); |
| 139 |
if (javaResultType == Void.TYPE) { |
| 140 |
result = null; |
| 141 |
} else { |
| 142 |
result = Context.jsToJava(result, javaResultType); |
| 143 |
} |
| 144 |
return result; |
| 145 |
} |
| 146 |
} |