javaosgiinjectreflectionsipojo

In the OSGi/iPOJO world, how to get a list of instances of classes implementing an interface?


In the JavaEE/CDI world, I know how to have the list of instances of classes implementing a given interface : by using Instance<MyInterface> combined with reflections library.

But in the OSGI/iPOJO world, how to do so ?

I know I get one instance by using @Requires MyInterface anInstance. But how can I have a programmatic access to all those classes ?


Solution

  • Just use the BundleContext:

    @Context BundleContext context;
    
    public List<ServiceReference> getInstancesImplementingX() {
        return context.getServiceReferences(X.class);   
    }