osgi-dev-boun...@mail.osgi.org wrote on 2015/03/18 09:04:08:

> From: Frank Langel <fr...@frankjlangel.com>
> Hi,
> 
> I did some research, but didn¹t find a way to register a generic 
service.
> I would like to do sth like this
> 
>    context.registerService(Map<K,V>.class, service, properties), I.e.
>    context.registerService(Map<Integer,String>.class, new
> HashMap<Integer,String>(), null)

'Map<Integer,String>.class' does not exist at runtime. Only 'Map.class' 
exists at runtime due to generics erasure. So there is no way to look up a 
class using generics since they are erased at runtime.

> 
> The injection should then only work if the registered and the injected K
> and V are the same. Assuming I only have one service of type Map
> registered,
> 
>    @Reference
>    public void setMapService(Map<Integer,String> mymap) {
>        This.map = my map;
>    }
> 
>    Would succeed.
> 
>    The following would fail as V (registered) != V(referenced service)
> 
>    @Reference
>    public void setMapService(Map<Integer,Integer> mymap) {
>      this.map = mymap;
>    }
> 
> As a workaround, I could define K and V as service properties and filter
> for them, but that¹s not very elegant and very error prone
> 
> Any feedback would be appreciated
> Frank


-- 

BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
hargr...@us.ibm.com

office: +1 386 848 1781
mobile: +1 386 848 3788
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to