Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 357865

Summary: Need IRequestor API for class being injected
Product: [Eclipse Project] Platform Reporter: Gunnar Wagenknecht <gunnar>
Component: RuntimeAssignee: Oleg Besedin <ob1.eclipse>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: ob1.eclipse, pwebster
Version: 4.2   
Target Milestone: 4.2 M3   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on:    
Bug Blocks: 357460    

Description Gunnar Wagenknecht CLA 2011-09-15 15:13:49 EDT
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.
Comment 1 Gunnar Wagenknecht CLA 2011-09-22 06:53:03 EDT
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;
}
Comment 2 Oleg Besedin CLA 2011-09-28 16:31:50 EDT
Added IRequestor#getRequestingObjectClass() - as requested :-). 

http://git.eclipse.org/c/platform/eclipse.platform.runtime.git/commit/?id=b9f758ea70fc19eb822897008912fb8ec7b9e707
Comment 3 Oleg Besedin CLA 2011-09-28 16:32:52 EDT
.