Ejemplo n.º 1
0
 /**
  * Retuns an URL based on a plugin and file path
  *
  * @param plugin Object The plugin containing the file path
  * @param name String The file path
  * @return URL The URL representing the file at the specified path
  * @throws Exception
  */
 @SuppressWarnings("unchecked") // $NON-NLS-1$
 private static URL getPluginImageURL(Object plugin, String name) throws Exception {
   // try to work with 'plugin' as with OSGI BundleContext
   try {
     Class bundleClass = Class.forName("org.osgi.framework.Bundle"); // $NON-NLS-1$
     Class bundleContextClass = Class.forName("org.osgi.framework.BundleContext"); // $NON-NLS-1$
     if (bundleContextClass.isAssignableFrom(plugin.getClass())) {
       Method getBundleMethod =
           bundleContextClass.getMethod("getBundle", new Class[] {}); // $NON-NLS-1$
       Object bundle = getBundleMethod.invoke(plugin, new Object[] {});
       //
       Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath"); // $NON-NLS-1$
       Class pathClass = Class.forName("org.eclipse.core.runtime.Path"); // $NON-NLS-1$
       Constructor pathConstructor = pathClass.getConstructor(new Class[] {String.class});
       Object path = pathConstructor.newInstance(new Object[] {name});
       //
       Class platformClass = Class.forName("org.eclipse.core.runtime.Platform"); // $NON-NLS-1$
       Method findMethod =
           platformClass.getMethod("find", new Class[] {bundleClass, ipathClass}); // $NON-NLS-1$
       return (URL) findMethod.invoke(null, new Object[] {bundle, path});
     }
   } catch (Throwable e) {
     // Ignore any exceptions
   }
   // else work with 'plugin' as with usual Eclipse plugin
   {
     Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); // $NON-NLS-1$
     if (pluginClass.isAssignableFrom(plugin.getClass())) {
       //
       Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath"); // $NON-NLS-1$
       Class pathClass = Class.forName("org.eclipse.core.runtime.Path"); // $NON-NLS-1$
       Constructor pathConstructor = pathClass.getConstructor(new Class[] {String.class});
       Object path = pathConstructor.newInstance(new Object[] {name});
       //
       Method findMethod = pluginClass.getMethod("find", new Class[] {ipathClass}); // $NON-NLS-1$
       return (URL) findMethod.invoke(plugin, new Object[] {path});
     }
   }
   return null;
 }