KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > ie > MethodDispatch


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.shell.ie;
17
18 import com.google.gwt.dev.shell.CompilingClassLoader;
19 import com.google.gwt.dev.shell.JsValueGlue;
20
21 import org.eclipse.swt.internal.ole.win32.COM;
22 import org.eclipse.swt.ole.win32.Variant;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 /**
28  * Wraps an arbitrary Java Method as an Automation-compatible server. The class
29  * was motivated by the need to expose Java objects into JavaScript.
30  *
31  * <p>
32  * <b>Features</b>
33  * </p>
34  * <ul>
35  * <li>Implements the <code>IDispatch</code> interface for you</li>
36  * <li>If the COM client keeps a reference to this object, this object is
37  * prevented from being garbage collected</li>
38  * </ul>
39  *
40  * <p>
41  * <b>Limitations</b>
42  * </p>
43  * <ul>
44  * <li>Only late-bound dispatch is supported</li>
45  * <li>Named arguments are not supported (see {@link #GetIDsOfNames)).</li>
46  * </ul>
47  */

48 class MethodDispatch extends IDispatchImpl {
49
50   private final CompilingClassLoader classLoader;
51
52   private final Method JavaDoc method;
53
54   public MethodDispatch(CompilingClassLoader classLoader, Method JavaDoc method) {
55     this.classLoader = classLoader;
56     this.method = method;
57   }
58
59   public String JavaDoc toString() {
60     return "\nfunction " + method.toString() + "(){\n [native code]\n}\n";
61   }
62
63   /**
64    * ID 0 is magic. It can either mean toString or invoke, depending on the
65    * flags. So we start with ID 1 for toString. {@link IDispatchProxy} and
66    * {@link BrowserWidgetIE6.External} should be fixed to do the same.
67    */

68   protected void getIDsOfNames(String JavaDoc[] names, int[] ids)
69       throws HResultException {
70     if (names[0].equalsIgnoreCase("toString")) {
71       ids[0] = 1;
72     } else if (names[0].equalsIgnoreCase("call")) {
73       ids[0] = 2;
74     } else {
75       throw new HResultException(IDispatchProxy.DISP_E_UNKNOWNNAME);
76     }
77   }
78
79   /*
80    * Handles all the things the browser can do to a function object.
81    */

82   protected Variant invoke(int id, int flags, Variant[] params)
83       throws HResultException, InvocationTargetException JavaDoc {
84     switch (id) {
85       case 0:
86         if ((flags & COM.DISPATCH_METHOD) != 0) {
87           // implicit call -- "m()"
88
return callMethod(classLoader, null, params, method);
89         } else if ((flags & COM.DISPATCH_PROPERTYGET) != 0) {
90           // implicit toString -- "'foo' + m"
91
return new Variant(toString());
92         }
93         break;
94       case 1:
95         // "m.toString()"
96
if ((flags & (COM.DISPATCH_METHOD | COM.DISPATCH_PROPERTYGET)) != 0) {
97           return new Variant(toString());
98         }
99         break;
100       case 2:
101         // "m.call(thisObj, arg)"
102
if ((flags & COM.DISPATCH_METHOD) != 0) {
103           /*
104            * First param must be a this object of the correct type (for instance
105            * methods). If method is static, it can be null.
106            */

107           Object JavaDoc jthis = JsValueGlue.get(new JsValueIE6(params[0]),
108               method.getDeclaringClass(), "this");
109           Variant[] otherParams = new Variant[params.length - 1];
110           System.arraycopy(params, 1, otherParams, 0, otherParams.length);
111           return callMethod(classLoader, jthis, otherParams, method);
112         }
113         break;
114       case IDispatchProxy.DISPID_MAGIC_GETGLOBALREF:
115         // We are NOT in fact a "wrapped Java Object", but we don't want to
116
// throw an exception for being asked.
117
return new Variant(0);
118       default:
119         // The specified member id is out of range.
120
throw new HResultException(COM.DISP_E_MEMBERNOTFOUND);
121     }
122     throw new HResultException(COM.E_NOTSUPPORTED);
123   }
124 }
125
Popular Tags