[cdi-dev] How about documenting Alternatives on producers ?

Antonio Goncalves antonio.goncalves at gmail.com
Tue Oct 21 10:10:30 EDT 2014


Hi all,

I often use code such as the one below: one bean, two producers and one
producer is an alternative (not the bean) :

*public class *PrefixProducer
{
   @Produces
   *public *String *prefixNonAlt *= *"non-alternative"*;

   @Produces
*   @Alternative*
   *public *String *prefixAlt *= *"alternative"*;
}

I enable the alternative, as usual, by declaring the bean in the beans.xml :

<*alternatives*>
  <*class*>org.agoncal.cdi.PrefixProducer</*class*>
</*alternatives*>

This works fine, no problem. But I just realized that Intellij IDEA
displays the warning "Alternative class expected here". I've looked around,
read the spec, the Weld documentation, and didn't find any example of the
code above. I think people are unware of this. The only reference of
alternative producers I found is in the spec :

*5.1.1.2*
*An alternative is selected for the bean archive if either:*

** the alternative is a producer method, field or resource, and the bean
class that declares the method or field is listed*

I think for such easy use cases (i.e PrefixProducer) there is no need to
multiply beans, so the code is fine and easy to read. But the code you see
around, tend to multiply beans (people think that Alternatives are *only*
for beans, not for producers).

Shouldn't the spec/Weld documentation mention this case a bit more often,
by giving an example ?

WDYT ?

Antonio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/cdi-dev/attachments/20141021/625551dd/attachment.html 


More information about the cdi-dev mailing list