开发者

Generics and Qualifiers

Hi im trying to implement an EventBuilder based in weld (CDI) Events.

I created the following method to build my event with the chosen qualifier (especified inf the parameter qualifierClass.

@SuppressWarnings("serial")
public static <T extends custom.Event, Q extends Qualifier> 
    Event<T> buildEvent(Event<T> event, Class<Q> qualifierClass) {

    return event.select(new AnnotationLiteral<Q>(){});
}

My qualifier has the following code:

@Qualifier 
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TicketSuccessfulValidation {

}

Then i try to use the methodo like this:

@Inject Event<TicketEvent> event;

private Map<String, User> loggedUsers = new HashMap<String, User>(0);

public User retrieveUserByTicket(String ticket) {
    User user = this.loggedUsers.get(ticket);
    if (user != null) {
        buildEvent(event, TicketSuccessfulValidation.class).fire(new TicketEvent("12345"));
        return user;

    } else {
       开发者_如何学C throw new TicketNotFoundException();
    }
}

My eclipse then gives me the following message:

Bound mismatch: The generic method buildEvent(Event<T>, Class<Q>) of type AbstractEventBuilder is not applicable for the arguments (Event<TicketEvent>, Class<TicketSuccessfulValidation>). The inferred type TicketSuccessfulValidation is not a valid substitute for the bounded parameter <Q extends Qualifier>

If my TicketSuccessfulValidation is annotated with @Qualifier its not right to say that is extends Qualifier? Why TicketSuccessfulValidation is not a valid substitute for "Q extends Qualifier" ?

Thanks in advance for any help.


Q extends Qualifier obviously means that the class you pass has to extend Qualifier. :)

However, TicketSuccessfulValidation doesn't extend Qualifier but is annotated to be one. Annotations are not evaluated by Generics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜