Thanks a lo for the insights. Type erasure spoils all the fun.

On 3/18/15, 5:00 PM, "osgi-dev-requ...@mail.osgi.org"
<osgi-dev-requ...@mail.osgi.org> wrote:

>Send osgi-dev mailing list submissions to
>       osgi-dev@mail.osgi.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>       https://mail.osgi.org/mailman/listinfo/osgi-dev
>or, via email, send a message with subject or body 'help' to
>       osgi-dev-requ...@mail.osgi.org
>
>You can reach the person managing the list at
>       osgi-dev-ow...@mail.osgi.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of osgi-dev digest..."
>
>
>Today's Topics:
>
>   1. Register generic service -
>      bundleContext.registerService(Map<Integer, Object>.class, .., ..)
>      (Frank Langel)
>   2. Re: Register generic service -
>      bundleContext.registerService(Map<Integer, Object>.class, .., ..)
>      (BJ Hargrave)
>   3. Re: Register generic service -
>      bundleContext.registerService(Map<Integer, Object>.class, .., ..)
>      (Raymond Auge)
>
>
>----------------------------------------------------------------------
>
>Message: 1
>Date: Wed, 18 Mar 2015 14:04:08 +0100
>From: Frank Langel <fr...@frankjlangel.com>
>To: "osgi-dev@mail.osgi.org" <osgi-dev@mail.osgi.org>
>Subject: [osgi-dev] Register generic service -
>       bundleContext.registerService(Map<Integer, Object>.class, .., ..)
>Message-ID: <d12f30ed.1ec14%fr...@frankjlangel.com>
>Content-Type: text/plain;      charset="ISO-8859-1"
>
>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)
>
>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
>
>
>
>
>------------------------------
>
>Message: 2
>Date: Wed, 18 Mar 2015 09:11:31 -0400
>From: BJ Hargrave <hargr...@us.ibm.com>
>To: OSGi Developer Mail List <osgi-dev@mail.osgi.org>
>Subject: Re: [osgi-dev] Register generic service -
>       bundleContext.registerService(Map<Integer, Object>.class, .., ..)
>Message-ID:
>       <of00c34a5d.d8c66ce9-on85257e0c.004829a4-85257e0c.00487...@us.ibm.com>
>Content-Type: text/plain; charset="iso-8859-1"
>
>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
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: 
><http://mail.osgi.org/pipermail/osgi-dev/attachments/20150318/4d43f1f0/att
>achment-0001.html>
>
>------------------------------
>
>Message: 3
>Date: Wed, 18 Mar 2015 10:00:33 -0400
>From: Raymond Auge <raymond.a...@liferay.com>
>To: OSGi Developer Mail List <osgi-dev@mail.osgi.org>
>Subject: Re: [osgi-dev] Register generic service -
>       bundleContext.registerService(Map<Integer, Object>.class, .., ..)
>Message-ID:
>       <CAMm6HcDszC_ze8hryL6pBseM=apugcpyvx_qycoug-4vhz+...@mail.gmail.com>
>Content-Type: text/plain; charset="utf-8"
>
>The only way to retrain generic information at runtime is with a class
>that
>extends from a generic one without declaring a generic signature itself.
>
>For instance:
>
>         class MyMap extends HashMap<Integer, String> {}
>
>The compiler retains the generic information directly in the class. At
>that
>point you can, at runtime, check if the type has generic arguments and
>obtain what those argument types were.
>
>        Type[] genericInterfaces = clazz.getGenericInterfaces();
>
>        for (Type genericInterface : genericInterfaces) {
>            if (!(genericInterface instanceof ParameterizedType)) {
>                continue;
>            }
>
>            ParameterizedType parameterizedType =
>                (ParameterizedType)genericInterface;
>
>            Type rawType = parameterizedType.getRawType();
>
>            ...
>         }
>
>
>However, you still can't register the service using generics:
>
>         context.registerService(Map.class, new MyMap(), null)
>
>But if you need you could technically impose the restriction from the
>other
>side and ignore any services which don't have retrained generic
>information.
>
>That might make things a little harder for developers to reason about but
>if this is for "personal" use only, then it may do what you want.
>
>- Ray
>
>
>On Wed, Mar 18, 2015 at 9:11 AM, BJ Hargrave <hargr...@us.ibm.com> wrote:
>
>> 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* <http://www.osgi.org/>
>> *hargr...@us.ibm.com* <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
>>
>
>
>
>-- 
>*Raymond Aug?* <http://www.liferay.com/web/raymond.auge/profile>
> (@rotty3000)
>Senior Software Architect *Liferay, Inc.* <http://www.liferay.com>
> (@Liferay)
>Board Member & EEG Co-Chair, OSGi Alliance <http://osgi.org>
>(@OSGiAlliance)
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: 
><http://mail.osgi.org/pipermail/osgi-dev/attachments/20150318/0512cacf/att
>achment-0001.html>
>
>------------------------------
>
>_______________________________________________
>OSGi Developer Mail List
>osgi-dev@mail.osgi.org
>https://mail.osgi.org/mailman/listinfo/osgi-dev
>
>End of osgi-dev Digest, Vol 101, Issue 25
>*****************************************


_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to