11package org .eclipsescript .core ;
22
33import java .net .URL ;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
46
57import org .eclipse .core .runtime .FileLocator ;
68import org .eclipse .core .runtime .ILog ;
2426import org .eclipsescript .util .EclipseUtils .DisplayThreadRunnable ;
2527import org .osgi .framework .Bundle ;
2628import org .osgi .framework .BundleContext ;
29+ import org .osgi .framework .FrameworkUtil ;
2730import org .osgi .framework .ServiceReference ;
2831
2932/**
@@ -36,27 +39,38 @@ public class Activator extends AbstractUIPlugin {
3639 private static Activator plugin ;
3740
3841 public static Bundle getBundleExportingClass (String className ) {
39- int lastIndexOfDot = className .lastIndexOf ('.' );
40- if (lastIndexOfDot > 0 && lastIndexOfDot < className .length () - 1 ) {
41- char firstCharOfClassName = className .charAt (lastIndexOfDot + 1 );
42- if (Character .isLowerCase (firstCharOfClassName )) {
43- // probably a package requested by rhino "org.eclipse" part of "org.eclipse.ui.xxx"
44- return null ;
45- }
46-
47- String packageName = className .substring (0 , lastIndexOfDot );
48-
42+ String packageName = getPackageNameFromClassName (className );
43+ if (packageName != null ) {
4944 ExportPackageDescription desc = plugin .resolver .resolveDynamicImport (plugin .bundleDescription , packageName );
5045 if (desc != null ) {
5146 BundleDescription exporter = desc .getExporter ();
5247 long exporterBundleId = exporter .getBundleId ();
5348 Bundle exportingBundle = plugin .context .getBundle (exporterBundleId );
5449 return exportingBundle ;
5550 }
51+ }
52+ return null ;
53+ }
5654
55+ public static Bundle [] getBundlesExportingPackage (String className ) {
56+ List <Bundle > bundles = new ArrayList <Bundle >();
57+
58+ String packageName = getPackageNameFromClassName (className );
59+ if (packageName != null ) {
60+ BundleContext bundleContext = FrameworkUtil .getBundle (Activator .class ).getBundleContext ();
61+ ExportPackageDescription [] exportedPackages = plugin .resolver .getState ().getExportedPackages ();
62+ for (ExportPackageDescription packageDescription : exportedPackages ) {
63+ boolean found = packageName .equals (packageDescription .getName ());
64+ if (found ) {
65+ BundleDescription exporter = packageDescription .getExporter ();
66+ long bundleId = exporter .getBundleId ();
67+ Bundle bundle = bundleContext .getBundle (bundleId );
68+ bundles .add (bundle );
69+ }
70+ }
5771 }
5872
59- return null ;
73+ return bundles . toArray ( new Bundle [ bundles . size ()]) ;
6074 }
6175
6276 /**
@@ -70,6 +84,21 @@ public static ImageDescriptor getImageDescriptor(String id) {
7084 return getDefault ().getImageRegistry ().getDescriptor (id );
7185 }
7286
87+ private static String getPackageNameFromClassName (String className ) {
88+ int lastIndexOfDot = className .lastIndexOf ('.' );
89+ if (lastIndexOfDot > 0 && lastIndexOfDot < className .length () - 1 ) {
90+ char firstCharOfClassName = className .charAt (lastIndexOfDot + 1 );
91+ if (Character .isLowerCase (firstCharOfClassName )) {
92+ // probably a package requested by rhino "org.eclipse" part of "org.eclipse.ui.xxx"
93+ return null ;
94+ }
95+ String packageName = className .substring (0 , lastIndexOfDot );
96+ return packageName ;
97+ }
98+
99+ return null ;
100+ }
101+
73102 public static void logError (final Throwable exception ) {
74103 ILog log = plugin .getLog ();
75104 log .log (new Status (IStatus .ERROR , plugin .getBundle ().getSymbolicName (), exception .getMessage (), exception ));
0 commit comments