001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.shell.moz;
017:
018: import org.eclipse.swt.internal.mozilla.XPCOM;
019:
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022:
023: /**
024: * A bag of static helper methods for mucking about with low-level SWT and Gecko
025: * constructs.
026: *
027: * TODO(jat): remove this class by replacing the nsISupports code with
028: * JsRootedValue references.
029: */
030: class SwtGeckoGlue {
031:
032: private static boolean areMethodsInitialized;
033: private static final String ERRMSG_CANNOT_ACCESS = "Unable to find or access necessary SWT XPCOM methods. Ensure that the correct version of swt.jar is being used.";
034: private static final String ERRMSG_CANNOT_INVOKE = "Unable to invoke a necessary SWT XPCOM method. Ensure that the correct version of swt.jar is being used.";
035: private static Method XPCOMVtblCall;
036:
037: /**
038: * Wrapper for XPCOM's nsISupports::AddRef().
039: */
040: public static int addRefInt(int nsISupports) {
041: ensureMethodsInitialized();
042: Throwable rethrow = null;
043: try {
044: Object retVal = XPCOMVtblCall.invoke(null, new Object[] {
045: new Integer(1), new Integer(nsISupports) });
046: return ((Integer) retVal).intValue();
047: } catch (IllegalArgumentException e) {
048: rethrow = e;
049: } catch (IllegalAccessException e) {
050: rethrow = e;
051: } catch (InvocationTargetException e) {
052: rethrow = e;
053: }
054: throw new RuntimeException(ERRMSG_CANNOT_INVOKE, rethrow);
055: }
056:
057: /**
058: * Wrapper for XPCOM's nsISupports::Release().
059: */
060: public static int releaseInt(int nsISupports) {
061: ensureMethodsInitialized();
062: Throwable rethrow = null;
063: try {
064: Object retVal = XPCOMVtblCall.invoke(null, new Object[] {
065: new Integer(2), new Integer(nsISupports) });
066: return ((Integer) retVal).intValue();
067: } catch (IllegalArgumentException e) {
068: rethrow = e;
069: } catch (IllegalAccessException e) {
070: rethrow = e;
071: } catch (InvocationTargetException e) {
072: rethrow = e;
073: }
074: throw new RuntimeException(ERRMSG_CANNOT_INVOKE, rethrow);
075: }
076:
077: private static void ensureMethodsInitialized() {
078: if (!areMethodsInitialized) {
079: Throwable rethrow = null;
080:
081: // Never try more than once to initialize, even if there's an exception.
082: areMethodsInitialized = true;
083: try {
084: XPCOMVtblCall = XPCOM.class.getDeclaredMethod(
085: "VtblCall",
086: new Class[] { int.class, int.class });
087: XPCOMVtblCall.setAccessible(true);
088: } catch (SecurityException e) {
089: rethrow = e;
090: } catch (NoSuchMethodException e) {
091: rethrow = e;
092: }
093:
094: if (rethrow != null) {
095: throw new RuntimeException(ERRMSG_CANNOT_ACCESS,
096: rethrow);
097: }
098: }
099: }
100: }
|