Adapter to use JS function as implementation of Java interfaces with single method or multiple methods with the same signature.
Adapter to use JS function as implementation of Java interfaces with single method or multiple methods with the same signature.
Exemple #1
0
		/// <summary>
		/// Make glue object implementing interface cl that will
		/// call the supplied JS function when called.
		/// </summary>
		/// <remarks>
		/// Make glue object implementing interface cl that will
		/// call the supplied JS function when called.
		/// Only interfaces were all methods have the same signature is supported.
		/// </remarks>
		/// <returns>
		/// The glue object or null if <tt>cl</tt> is not interface or
		/// has methods with different signatures.
		/// </returns>
		internal static object Create(Context cx, Type cl, ScriptableObject @object)
		{
			if (!cl.IsInterface)
			{
				throw new ArgumentException();
			}
			Scriptable topScope = ScriptRuntime.GetTopCallScope(cx);
			ClassCache cache = ClassCache.Get(topScope);
			Rhino.InterfaceAdapter adapter;
			adapter = (Rhino.InterfaceAdapter)cache.GetInterfaceAdapter(cl);
			ContextFactory cf = cx.GetFactory();
			if (adapter == null)
			{
				MethodInfo[] methods = cl.GetMethods();
				if (@object is Callable)
				{
					// Check if interface can be implemented by a single function.
					// We allow this if the interface has only one method or multiple 
					// methods with the same name (in which case they'd result in 
					// the same function to be invoked anyway).
					int length = methods.Length;
					if (length == 0)
					{
						throw Context.ReportRuntimeError1("msg.no.empty.interface.conversion", cl.FullName);
					}
					if (length > 1)
					{
						string methodName = methods[0].Name;
						for (int i = 1; i < length; i++)
						{
							if (!methodName.Equals(methods[i].Name))
							{
								throw Context.ReportRuntimeError1("msg.no.function.interface.conversion", cl.FullName);
							}
						}
					}
				}
				adapter = new Rhino.InterfaceAdapter(cf, cl);
				cache.CacheInterfaceAdapter(cl, adapter);
			}
			return VMBridge.instance.NewInterfaceProxy(adapter.proxyHelper, cf, adapter, @object, topScope);
		}
Exemple #2
0
			public _ContextAction_80(InterfaceAdapter _enclosing, object target, Scriptable topScope, object thisObject, MethodInfo method, object[] args)
			{
				this._enclosing = _enclosing;
				this.target = target;
				this.topScope = topScope;
				this.thisObject = thisObject;
				this.method = method;
				this.args = args;
			}
Exemple #3
0
		protected internal override object NewInterfaceProxy(object proxyHelper, ContextFactory cf, InterfaceAdapter adapter, object target, Scriptable topScope)
		{
			ConstructorInfo<object> c = (ConstructorInfo<object>)proxyHelper;
			InvocationHandler handler = new _InvocationHandler_107(target, adapter, cf, topScope);
			// In addition to methods declared in the interface, proxies
			// also route some java.lang.Object methods through the
			// invocation handler.
			// Note: we could compare a proxy and its wrapped function
			// as equal here but that would break symmetry of equal().
			// The reason == suffices here is that proxies are cached
			// in ScriptableObject (see NativeJavaObject.coerceType())
			object proxy;
			try
			{
				proxy = c.NewInstance(handler);
			}
			catch (TargetInvocationException ex)
			{
				throw Context.ThrowAsScriptRuntimeEx(ex);
			}
			catch (MemberAccessException ex)
			{
				// Should not happen
				throw Kit.InitCause(new InvalidOperationException(), ex);
			}
			catch (InstantiationException ex)
			{
				// Should not happen
				throw Kit.InitCause(new InvalidOperationException(), ex);
			}
			return proxy;
		}
Exemple #4
0
		/// <summary>
		/// Create proxy object for
		/// <see cref="InterfaceAdapter">InterfaceAdapter</see>
		/// . The proxy should call
		/// <see cref="InterfaceAdapter.Invoke(ContextFactory, object, Scriptable, object, System.Reflection.MethodInfo, object[])">InterfaceAdapter.Invoke(ContextFactory, object, Scriptable, object, System.Reflection.MethodInfo, object[])</see>
		/// as implementation of interface methods associated with
		/// <tt>proxyHelper</tt>.
		/// </summary>
		/// <param name="proxyHelper">
		/// The result of the previous call to
		/// <see cref="GetInterfaceProxyHelper(ContextFactory, System.Type{T}[])">GetInterfaceProxyHelper(ContextFactory, System.Type&lt;T&gt;[])</see>
		/// .
		/// </param>
		protected internal virtual object NewInterfaceProxy(object proxyHelper, ContextFactory cf, InterfaceAdapter adapter, object target, Scriptable topScope)
		{
			throw Context.ReportRuntimeError("VMBridge.newInterfaceProxy is not supported");
		}
Exemple #5
0
			public _InvocationHandler_107(object target, InterfaceAdapter adapter, ContextFactory cf, Scriptable topScope)
			{
				this.target = target;
				this.adapter = adapter;
				this.cf = cf;
				this.topScope = topScope;
			}