2 Replies Latest reply: Mar 26, 2012 7:23 PM by Andrew Hughes RSS

Factories with Optional @Qualifiers, @Stereotypes and @Produces, How?

Andrew Hughes Newbie

Hi Guys,

 

Can anyone tell me what is wrong with this approach below? I was hoping I could utilize CDI to easily implement a factory for instansiating 'Widget' objects. They have mixed sizes and styles e.t.c. which currently use qualifiers (and should possibly not?).

 

Example:

 

Without style or sizing...

 

@Inject

Widget w;

 

or, with implicit sizing

 

@Inject

@Size(height="100px",width="100px")

Widget someWidget;

 

or with full sizing

 

@Inject

@SizeFull

Widget someWidget;

 

or with style and full sizing

 

@Inject

@Style(name="pretty", border=true)

@SizeFull

Widget someWidget;

 

or with style only

 

@Inject

@Style(name="pretty", border=true)

Widget someWidget;

 

 

The only unique rule not shown above is that you can not have BOTH  @SizeFull and @Size qualifiers on an injection point.

 

The following should be ILLEGAL!

 

@Inject

@Size(height="100px",width="100px")

@SizeFull

Widget someWidget;

 

 

It doesn't seem feasible to create multiple @Produces to handle each unique injection point, there's just too many to be sensible.

 

I have looked at stereotypes, but I consistent get the error "Cannot declare qualifiers on stereotype interface" for which I have no explaination. I also don't see how a @Stereotype could be linked with a @Producer, can it? Also, I dont see examples where stereotypes have field/attribute values like qualifiers can, can they?

 

@Stereotype

@Target({ METHOD, FIELD, PARAMETER, TYPE })

@Retention(RUNTIME)

@Style

@Size

@SizeFull

public @interface WidgetConfig {

  String something();  //is this valid????? and can it be @Nonbinding???

}

 

 

 

Cheers

--AH

  • 1. Re: Factories with Optional @Qualifiers, @Stereotypes and @Produces, How?
    Marko Lukša Apprentice

    Just make the producer method like this:

     

    @Produces public Widget produceWidget(InjectionPoint injectionPoint);

     

    The injectionPoint argument will contain the metadata of the field that is being injected. The producer method can therefore inspect the annotations of that field and produce the widget according to the annotations.

     

     

    Look at the HttpParams example at http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html_single/#d0e1624

  • 2. Re: Factories with Optional @Qualifiers, @Stereotypes and @Produces, How?
    Andrew Hughes Newbie

    Hi Marko,

     

    Thanks for your reply. I had hoped for a different answer, I am dissapointed. The reason is this... there is nothing in CDI to ensure that the resolved @Produces will have any consideration for the Injection Point's unique combination of qualifiers. Perhaps this is actually seen as an advantage?

     

    So why am I saying all this...

     

    After experiencing both Guice and CDI I can see several pro's/con's between the two. Like a kid in a candy store, Guice's @Assisted injection is just too damn good - I want it, I want it.... It's capability to realize a super clean and type safe factory pattern with almost zero effort is brilliant. Not to mention the fact that it can take in supplied (non primitive) arguments.

     

    If there is a better way to realize this, or if someone can explain it better can I suggest we start an article (or some form of documentation) to help out as many people as we can to explain recommended solutions?

     

    CHEERS