From 86bfd8b2b180985d4b89753ac3b6d3c8f3e5ce9b Mon Sep 17 00:00:00 2001 From: "pedemont%us.ibm.com" Date: Fri, 1 Oct 2004 16:29:53 +0000 Subject: [PATCH] Add helper function for getting an interfaces IID. Used by Java impl of QI. Not part of default build. git-svn-id: svn://10.0.0.236/trunk@163086 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/extensions/java/xpcom/XPCOM.java | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/mozilla/extensions/java/xpcom/XPCOM.java b/mozilla/extensions/java/xpcom/XPCOM.java index 2902b51ac5f..1fa47b75129 100644 --- a/mozilla/extensions/java/xpcom/XPCOM.java +++ b/mozilla/extensions/java/xpcom/XPCOM.java @@ -66,4 +66,44 @@ public final class XPCOM { public static native void FinalizeStub(Object thisObj); public static native int nsWriteSegmentFun(int ptr, Object aInStream, int aClosure, byte[] aFromSegment, int aToOffset, int aCount); + + + /* Utility functions */ + + // Given an interface, this will construct the name of the IID field (such as + // NS_ISUPPORTS_IID) and return its value. + public static String getInterfaceIID(Class aInterface) + { + // Get short class name (i.e. "bar", not "org.blah.foo.bar") + StringBuffer iidName = new StringBuffer(); + String fullClassName = aInterface.getName(); + int index = fullClassName.lastIndexOf("."); + String className = index > 0 ? fullClassName.substring(index + 1) : + fullClassName; + + // Create iid field name + if (className.startsWith("ns")) { + iidName.append("NS_"); + iidName.append(className.substring(2).toUpperCase()); + } else { + iidName.append(className.toUpperCase()); + } + iidName.append("_IID"); + + String iid; + try { + Field iidField = aInterface.getDeclaredField(iidName.toString()); + iid = (String) iidField.get(null); + } catch (NoSuchFieldException e) { + // Class may implement non-Mozilla interfaces, which would not have an + // IID method. In that case, just return an emptry string. + iid = ""; + } catch (IllegalAccessException e) { + // XXX Should be using a logging service, such as java.util.logging + System.err.println("ERROR: Could not get field " + iidName.toString()); + iid = ""; + } + + return iid; + } }