-
1. Re: @Produces with 2 lists causes error
jfuerth Aug 7, 2013 3:02 PM (in response to magick93)1 of 1 people found this helpfulThe problem is that the producers both produce the same raw type (java.util.List) and this is confusing Errai's IOC generator. This might be a bug; I'm not sure how it's supposed to work in the presence of generic type arguments.
Whether or not this is a bug, here are two different workarounds you can try:
1. Subclass ArrayList for each of the list types, for example public class SymbolList extends ArrayList<Symbol>. Modify your producers and injection points to use the new types.
2. Use CDI qualifiers to differentiate between the two lists. So your producer methods/fields would be @Produces @Symbol List<Symbol> symbols and @Produces @Indicator List<Indicator> symbols. The injection points would also use the qualifiers: @Inject @Symbol List<Symbol> symbols.
-Jonathan
-
2. Re: @Produces with 2 lists causes error
magick93 Aug 7, 2013 3:46 PM (in response to jfuerth)Hi Jonathan, I tried #2, but that also did not work. I still get the same error.
-
3. Re: @Produces with 2 lists causes error
jfuerth Aug 7, 2013 4:00 PM (in response to magick93)That's surprising. We have a mountain of test cases for qualified injection points. Are you sure you qualified all of the injection points as well as all producers? You'll still get the "ambiguous injection type" error if even one injection point lacks a qualifier.
-Jonathan
-
4. Re: @Produces with 2 lists causes error
magick93 Aug 7, 2013 5:11 PM (in response to jfuerth)Hi Jonathan
Yes, am sure.
Howerver, I was able to fix this issue by following your first suggestion.
1. Subclass ArrayList for each of the list types, for example public class SymbolList extends ArrayList<Symbol>. Modify your producers and injection points to use the new types.
-
5. Re: @Produces with 2 lists causes error
jfuerth Aug 7, 2013 5:30 PM (in response to magick93)I think suggestion 1 was the better one anyway, because it allows the Java compiler to ensure type safety at your injection points. With option 2, there is no compile time safety against a mistake like @Inject @Symbol List<Indicator> indicators. This would happily inject the list of Symbol objects into the Indicator list injection point. You'll get mysterious errors when you try to use the items in the list.
However, I'm still concerned that option 2 didn't work for you. I've just added the following to an Errai project, and GWT compilation completed without error:
Two List producers:
package org.jboss.errai.cdiwb.client.local; import java.util.ArrayList; import java.util.List; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; import org.jboss.errai.cdiwb.shared.Qual1; import org.jboss.errai.cdiwb.shared.Qual2; @ApplicationScoped public class ListProducers { @Produces @Qual1 private List<String> stringList = new ArrayList<String>(); @Produces @Qual2 private List<Integer> intList = new ArrayList<Integer>(); }
Two List injection points:
package org.jboss.errai.cdiwb.client.local; import java.util.List; import javax.enterprise.context.Dependent; import javax.inject.Inject; import org.jboss.errai.cdiwb.shared.Qual1; import org.jboss.errai.cdiwb.shared.Qual2; @Dependent public class ListUsers { @Inject @Qual1 List<String> stringList; @Inject @Qual2 List<Integer> intList; }
And finally the two qualifiers:
package org.jboss.errai.cdiwb.shared; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import javax.inject.Qualifier; @Qualifier @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Qual1 { }
package org.jboss.errai.cdiwb.shared; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import javax.inject.Qualifier; @Qualifier @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Qual2 { }
Is this structurally identical to what you did? Which version of Errai were you compiling against?
-Jonathan