泛型和限定符
您好,我正在尝试实现基于焊接(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Q extends Qualifier
显然意味着您传递的类必须扩展Qualifier
。 :)但是,
TicketSuccessfulValidation
并未扩展Qualifier
,但被注释为之一。泛型不评估注释。Q extends Qualifier
obviously means that the class you pass has to extendQualifier
. :)However,
TicketSuccessfulValidation
doesn't extendQualifier
but is annotated to be one. Annotations are not evaluated by Generics.