泛型和限定符

发布于 2024-11-25 04:28:22 字数 1584 浏览 0 评论 0原文

您好,我正在尝试实现基于焊接(CDI)事件的 EventBuilder。

我创建了以下方法来使用所选限定符构建我的事件(在参数 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>(){});
}

我的限定符具有以下代码:

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

}

然后我尝试使用如下方法:

@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 {
        throw new TicketNotFoundException();
    }
}

然后我的 eclipse 给我以下消息:

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>

如果我的 TicketSuccessfulValidation 是 说它扩展了 Qualifier 是不对的?为​​什么 TicketSuccessfulValidation 不能有效替代“Q extends Qualifier”?

用 @Qualifier 注释, 帮助。

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 {
        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2024-12-02 04:28:22

Q extends Qualifier 显然意味着您传递的类必须扩展 Qualifier。 :)

但是,TicketSuccessfulValidation 并未扩展 Qualifier,但被注释为之一。泛型不评估注释。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文