openwebbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Manni-Bucau <rmannibu...@gmail.com>
Subject Re: CDI and ParameterizedTypes
Date Tue, 19 Mar 2013 16:37:24 GMT
done

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*



2013/3/19 Xavier Dury <kalgon@hotmail.com>

> Thanks.
>
> While you're correcting things, could you fix the typo in the message
> "Ambigious resolution".
>
> Another thing that bothers me is that the parameterized types information
> is "stripped" in the AbstractOwbBean.toString().
>
> If you look at my example, you will see:
>
> API Types:[java.lang.Object,test.StringToLongConverter,test.TypeConverter]
>
> I would like to see:
>
> API Types:[java.lang.Object,test.StringToLongConverter,test.TypeConverter<String,
> Long>]
>
> At first, I thought Bean.getTypes() only returned raw classes instead of
> more general Types. But that wasn't the case, it's just the toString()
> method that strips the parameterized information.
>
> Xavier
>
> ________________________________
> > From: rmannibucau@gmail.com
> > Date: Tue, 19 Mar 2013 11:34:16 +0100
> > Subject: Re: CDI and ParameterizedTypes
> > To: user@openwebbeans.apache.org
> >
> > FYI: https://issues.apache.org/jira/browse/OWB-791
> >
> > i'll backport it over 1.1.x branch pretty soon
> >
> > Romain Manni-Bucau
> > Twitter: @rmannibucau<https://twitter.com/rmannibucau>
> > Blog: http://rmannibucau.wordpress.com/
> > LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > Github: https://github.com/rmannibucau
> >
> >
> >
> > 2013/3/19 Romain Manni-Bucau
> > <rmannibucau@gmail.com<mailto:rmannibucau@gmail.com>>
> > Thanks,
> >
> > that's an issue: OWB only tolerates one generic
> >
> > patching
> org.apache.webbeans.util.ClassUtil#isAssignableForParametrizedCheckArguments
> > this way should make it work:
> >
> > private static boolean isAssignableForParametrizedCheckArguments(Type[]
> > beanTypeArgs, Type[] requiredTypeArgs)
> > {
> > Type requiredTypeArg = null;
> > Type beanTypeArg = null;
> > int ok = 0;
> > for(int i = 0; i< requiredTypeArgs.length;i++)
> > {
> > requiredTypeArg = requiredTypeArgs[i];
> > beanTypeArg = beanTypeArgs[i];
> >
> > //Required type is parametrized and bean type is parametrized
> > if(ClassUtil.isParametrizedType(requiredTypeArg) &&
> > ClassUtil.isParametrizedType(beanTypeArg))
> > {
> > if (checkBeanAndRequiredTypeIsParametrized(beanTypeArg,
> > requiredTypeArg))
> > {
> > ok++;
> > }
> > }
> > //Required type is wildcard
> > else if(ClassUtil.isWildCardType(requiredTypeArg))
> > {
> > if (checkRequiredTypeIsWildCard(beanTypeArg,
> > requiredTypeArg))
> > {
> > ok++;
> > }
> > }
> > //Required type is actual type and bean type is type variable
> > else if(requiredTypeArg instanceof Class &&
> > ClassUtil.isTypeVariable(beanTypeArg))
> > {
> > if
> > (checkRequiredTypeIsClassAndBeanTypeIsVariable(beanTypeArg,
> > requiredTypeArg))
> > {
> > ok++;
> > }
> > }
> > //Required type is Type variable and bean type is type variable
> > else if(ClassUtil.isTypeVariable(requiredTypeArg) &&
> > ClassUtil.isTypeVariable(beanTypeArg))
> > {
> > if (
> > checkBeanTypeAndRequiredIsTypeVariable(beanTypeArg, requiredTypeArg))
> > {
> > ok++;
> > }
> > }
> >
> > //Both type is actual type
> > else if((beanTypeArg instanceof Class) && (requiredTypeArg
> > instanceof Class))
> > {
> >
> > if(isClassAssignable((Class<?>)requiredTypeArg,(Class<?>)beanTypeArg))
> > {
> > ok++;
> > }
> > }
> > //Bean type is actual type and required type is type variable
> > else if((beanTypeArg instanceof Class) &&
> > (ClassUtil.isTypeVariable(requiredTypeArg)))
> > {
> > if
> > (checkRequiredTypeIsTypeVariableAndBeanTypeIsClass(beanTypeArg,
> > requiredTypeArg))
> > {
> > ok++;
> > }
> > }
> > }
> >
> > return ok == requiredTypeArgs.length;
> > }
> >
> > Romain Manni-Bucau
> > Twitter: @rmannibucau<https://twitter.com/rmannibucau>
> > Blog: http://rmannibucau.wordpress.com/
> > LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > Github: https://github.com/rmannibucau
> >
> >
> >
> > 2013/3/19 Xavier Dury <kalgon@hotmail.com<mailto:kalgon@hotmail.com>>
> > Sure, here it is:
> https://dl.dropbox.com/u/908875/openwebbeans/converters.zip
> >
> > Xavier
> >
> > ________________________________
> > > From: rmannibucau@gmail.com<mailto:rmannibucau@gmail.com>
> > > Date: Tue, 19 Mar 2013 09:53:43 +0100
> > > Subject: Re: CDI and ParameterizedTypes
> > > To: user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org>
> > >
> > > do you have a sample reproducing it?
> > >
> > > Romain Manni-Bucau
> > > Twitter: @rmannibucau<https://twitter.com/rmannibucau>
> > > Blog: http://rmannibucau.wordpress.com/
> > > LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > > Github: https://github.com/rmannibucau
> > >
> > >
> > >
> > > 2013/3/19 Xavier Dury
> > <kalgon@hotmail.com<mailto:kalgon@hotmail.com><mailto:kalgon@hotmail.com
> <mailto:kalgon@hotmail.com>>>
> > > Hi,
> > >
> > > I still got the same error with 1.1.8-SNAPSHOT:
> > >
> > > javax.enterprise.inject.AmbiguousResolutionException: Ambigious
> resolution
> > > found beans:
> > > StringToLongConverter, Name:null, WebBeans Type:MANAGED, API
> > > Types:[java.lang.Object,test.StringToLongConverter,test.TypeConverter],
> > >
> Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]
> > > StringToIntegerConverter, Name:null, WebBeans Type:MANAGED, API
> > >
> Types:[test.TypeConverter,java.lang.Object,test.StringToIntegerConverter],
> > >
> Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]
> > > at
> > >
> >
> org.apache.webbeans.util.InjectionExceptionUtils.throwAmbiguousResolutionExceptionForBeans(InjectionExceptionUtils.java:121)
> > > at
> > >
> >
> org.apache.webbeans.util.InjectionExceptionUtils.throwAmbiguousResolutionException(InjectionExceptionUtils.java:111)
> > > at
> > >
> >
> org.apache.webbeans.util.InjectionExceptionUtils.throwAmbiguousResolutionException(InjectionExceptionUtils.java:88)
> > > at
> > >
> >
> org.apache.webbeans.container.InjectionResolver.resolve(InjectionResolver.java:669)
> > > at
> > >
> >
> org.apache.webbeans.container.InjectionResolver.checkInjectionPoints(InjectionResolver.java:189)
> > > at
> > >
> >
> org.apache.webbeans.container.BeanManagerImpl.validate(BeanManagerImpl.java:1034)
> > > at
> > > org.apache.openejb.cdi.BeansDeployer.validate(BeansDeployer.java:269)
> > > at
> > >
> >
> org.apache.openejb.cdi.BeansDeployer.validateInjectionPoints(BeansDeployer.java:222)
> > > at
> > >
> >
> org.apache.openejb.cdi.OpenEJBLifecycle.startApplication(OpenEJBLifecycle.java:280)
> > > ... 46 more
> > >
> > > Xavier
> > > ________________________________
> > > > Date: Fri, 15 Mar 2013 15:54:37 +0000
> > > > From:
> > struberg@yahoo.de<mailto:struberg@yahoo.de><mailto:struberg@yahoo.de
> <mailto:struberg@yahoo.de>>
> > > > Subject: Re: CDI and ParameterizedTypes
> > > > To:
> > user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> ><mailto:user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> >>
> > > >
> > > > romain, Xavier, can you please test with the latest 1.1.8-SNAPSHOT?
> > > >
> > > > I'll roll a 1.1.8 release this evening.
> > > >
> > > > LieGrue,
> > > > strub
> > > >
> > > >
> > > > ________________________________
> > > > From: Romain Manni-Bucau
> > >
> > <rmannibucau@gmail.com<mailto:rmannibucau@gmail.com><mailto:
> rmannibucau@gmail.com<mailto:rmannibucau@gmail.com>>>
> > > > To:
> > user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> ><mailto:user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> >>
> > > > Sent: Friday, March 15, 2013 4:17 PM
> > > > Subject: Re: CDI and ParameterizedTypes
> > > >
> > > > Ok
> > > >
> > > > guess the fix was not in
> > > >
> > > >
> > > > Romain Manni-Bucau
> > > > Twitter: @rmannibucau<https://twitter.com/rmannibucau>
> > > > Blog: http://rmannibucau.wordpress.com/
> > > > LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > > > Github: https://github.com/rmannibucau
> > > >
> > > >
> > > >
> > > > 2013/3/15 Xavier Dury
> > >
> > <kalgon@hotmail.com<mailto:kalgon@hotmail.com><mailto:kalgon@hotmail.com
> <mailto:kalgon@hotmail.com>><mailto:kalgon@hotmail.com<mailto:
> kalgon@hotmail.com><mailto:kalgon@hotmail.com<mailto:kalgon@hotmail.com
> >>>>
> > > > Hi,
> > > >
> > > > I'm using the last version available on maven central repo which is
> > 1.1.7.
> > > >
> > > > Xavier
> > > >
> > > > ________________________________
> > > > > From:
> > >
> > rmannibucau@gmail.com<mailto:rmannibucau@gmail.com><mailto:
> rmannibucau@gmail.com<mailto:rmannibucau@gmail.com>><mailto:
> rmannibucau@gmail.com<mailto:rmannibucau@gmail.com><mailto:
> rmannibucau@gmail.com<mailto:rmannibucau@gmail.com>>>
> > > > > Date: Fri, 15 Mar 2013 15:29:00 +0100
> > > > > Subject: Re: CDI and ParameterizedTypes
> > > > > To:
> > >
> > user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> ><mailto:user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> >><mailto:user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> ><mailto:user@openwebbeans.apache.org<mailto:user@openwebbeans.apache.org
> >>>
> > > > >
> > > > > Hi,
> > > > >
> > > > > if i didn't miss it you didn't mention your version
> > > > >
> > > > > think it should work with 1.1.8
> > > > >
> > > > > https://github.com/rmannibucau/cdi-converters was used to work
> and was
> > > > > using something
> > > > > close
> > > >
> > >
> >
> https://github.com/rmannibucau/cdi-converters/blob/master/src/test/java/com/github/rmannibucau/converter/ConverterTest.java
> > > > >
> > > > > Romain Manni-Bucau
> > > > > Twitter: @rmannibucau<https://twitter.com/rmannibucau>
> > > > > Blog: http://rmannibucau.wordpress.com/
> > > > > LinkedIn: http://fr.linkedin.com/in/rmannibucau
> > > > > Github: https://github.com/rmannibucau
> > > > >
> > > > >
> > > > >
> > > > > 2013/3/15 Xavier Dury
> > > >
> > >
> > <kalgon@hotmail.com<mailto:kalgon@hotmail.com><mailto:kalgon@hotmail.com
> <mailto:kalgon@hotmail.com>><mailto:kalgon@hotmail.com<mailto:
> kalgon@hotmail.com><mailto:kalgon@hotmail.com<mailto:kalgon@hotmail.com
> >>><mailto:kalgon@hotmail.com<mailto:kalgon@hotmail.com><mailto:
> kalgon@hotmail.com<mailto:kalgon@hotmail.com>><mailto:kalgon@hotmail.com
> <mailto:kalgon@hotmail.com><mailto:kalgon@hotmail.com<mailto:
> kalgon@hotmail.com>>>>>
> > > > > Hi,
> > > > >
> > > > > I'm trying to implement a simple type conversion framework in CDI.
> > > > > Typically, you can either implement the TypeConverter interface or
> > > > > annotate some methods in your beans:
> > > > >
> > > > > public interface TypeConverter<S, T> {
> > > > >
> > > > > T convert(S source);
> > > > > }
> > > > >
> > > > > public class StringToIntegerTypeConverter implements
> > > > > TypeConverter<String, Integer> { ... }
> > > > >
> > > > > public class StringToLongTypeConverter implements
> TypeConverter<String,
> > > > > Long> { ... }
> > > > >
> > > > > public MyConverters {
> > > > >
> > > > > @Converts public Integer stringToInteger(String value) { ... }
> > > > > @Converts public Long stringToLong(String value) { ... }
> > > > > }
> > > > >
> > > > > A specific Extension "bridges" the annotated methods to a full
> > > > > TypeConverter (a custom Bean<?> is registered with an API types
of
> > > > > ParameterizedTypeImpl(rawType = TypeConverter.class, ownerType =
> null,
> > > > > actualTypeArguments = [method.getGenericParameterType[0],
> > > > > method.getGenericReturnType])).
> > > > >
> > > > > Then a TypeConversionService is responsible to find the adequate
> > > > > Converter through the BeanManager and call it with the provided
> object
> > > > > to be converted.
> > > > >
> > > > > public class TypeConversionService {
> > > > >
> > > > > public <T> T convert(Class<T> targetType, Object source)
{ ... }
> > > > > public <T> T convert(TypeLiteral<T> targetTypeLiteral,
Object
> source)
> > > > { ... }
> > > > > }
> > > > >
> > > > > My problem is the following: as soon as 2 or more TypeConverters
> are
> > > > > present in the module (let's say StringToIntegerTypeConverter and
> > > > > StringToLongTypeConverter), an ambiguous dependency exception is
> > > > > thrown.
> > > > >
> > > > > For example:
> > > > >
> > > > > public class MyBean {
> > > > >
> > > > > @Inject TypeConverter<String, Integer>
> stringToIntegerTypeConverter;
> > > > > @Inject TypeConverter<String, Long> stringToLongTypeConverter;
> > > > >
> > > > > // won't work
> > > > > }
> > > > >
> > > > > If I delete one of the TypeConverter classes
> (StringToLongTypeConverter
> > > > > for example), the remaining TypeConverter
> > > > > (StringToIntegerTypeConverter) is injected at the 2 injection
> points
> > > > > (stringToIntegerTypeConverter=ok and
> stringToLongTypeConverter=!ok).
> > > > >
> > > > > Is it possible with CDI to have such a scenario without using
> > > > > additional qualifiers to qualify each converter and only rely on
> the
> > > > > (parameterized) type information to tell them apart?
> > > > >
> > > > > I've seen some discussions on StackOverflow saying the
> TypeConverter
> > > > > should belong to the Dependent scope but it didn't work for me.
> > > > >
> > > > > Thanks,
> > > > >
> > > > > Xavier
> > > > >
> > > >
> > > >
> > > >
> > >
> >
> >
>

Mime
View raw message