Community
Participate
Working Groups
IRequestor#getRequestingObject() may return null (eg., in case of constructor injection). BTW this is not documented there. I'd like to have a method #getRequestingObjectClass that returns the Class of the requesting object. This allows to discover the bundle that loaded a class. It's then possible to get the bundle and the BundleContext which allows to implement a PrimaryObjectSupplier that acquired OSGi services for injection on behalf of the requesting bundle. The latter is better for applying proper OSGi contraints.
BTW, here is what I'm doing now: private BundleContext getBundleContext(final IRequestor requestor) { Bundle bundle = null; final Object requestingObject = requestor.getRequestingObject(); if (null != requestingObject) { bundle = FrameworkUtil.getBundle(requestingObject.getClass()); } if (null == bundle) { // ConstructorRequestor // FIXME: https://bugs.eclipse.org/357865 try { final Field field = requestor.getClass().getDeclaredField("constructor"); if (!field.isAccessible()) { field.setAccessible(true); } bundle = FrameworkUtil.getBundle( ((Constructor<?>) field.get(requestor)).getDeclaringClass()); } catch (final Exception e) { LOG.debug("exception accessing data in requestor {}", requestor, e); } } if (null != bundle) { return bundle.getBundleContext(); } // give up return null; }
Added IRequestor#getRequestingObjectClass() - as requested :-). http://git.eclipse.org/c/platform/eclipse.platform.runtime.git/commit/?id=b9f758ea70fc19eb822897008912fb8ec7b9e707
.