Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 357865 - Need IRequestor API for class being injected
Summary: Need IRequestor API for class being injected
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Runtime (show other bugs)
Version: 4.2   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 4.2 M3   Edit
Assignee: Oleg Besedin CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 357460
  Show dependency tree
 
Reported: 2011-09-15 15:13 EDT by Gunnar Wagenknecht CLA
Modified: 2011-09-28 16:32 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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
.