Debian Codegraph @

rhino/src/org/mozilla/javascript/InterfaceAdapter.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.lang.reflect.Method;
10
11 /**
12 * Adapter to use JS function as implementation of Java interfaces with
13 * single method or multiple methods with the same signature.
14 */
15 public class InterfaceAdapter
16 {
17 private final Object proxyHelper;
18
19 /**
20 * Make glue object implementing interface cl that will
21 * call the supplied JS function when called.
22 * Only interfaces were all methods have the same signature is supported.
23 *
24 * @return The glue object or null if <tt>cl</tt> is not interface or
25 * has methods with different signatures.
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 // Check if interface can be implemented by a single function.
40 // We allow this if the interface has only one method or multiple
41 // methods with the same name (in which case they'd result in
42 // the same function to be invoked anyway).
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 // We really should throw an error here, but for the sake of
105 // compatibility with JavaAdapter we silently ignore undefined
106 // methods.
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 // neutralize wrap factory java primitive wrap feature
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 }