I have the following definition:
public interface MessageResponseHandler<T extends MessageBody> {
public void responsed(Message<T> msg);
}
public class DeviceClientHelper {
public MessageResponseHandler<? extends MessageBody> messageHandler;
setHandler(MessageResponseHandler<? extends MessageBody> h){
this.messageHandler = h;
}
public someMethod(Object message){
Message<? extends MessageBody> msg = (Message<? extends MessageBody>) message;
if (this.messageHandler != null) {
this.messageHandler.responsed(msg);
}
}
}
I couldn’t figure out why in the someMethod() method, the invocation of
this.messageHandler.responsed(msg);
would give me a wired compiler error in eclipse. something like:
the method responsed(Message<capture#3-of ? extends MessageBody>) in
the type MessageResponseHandler<capture#3-of ? extends MessageBody> is
not applicable for the arguments (Message<capture#4-of ? extends
MessageBody>)
what is the “catpure” means in the error message anyway?
You’re saying
DeviceClientHelperhas amessageHandlerfor some subclass ofMessageBody. AndsomeMethodhas a message also for some subclass ofMessageBody. However, there’s nothing requiring them to be the same subclass ofMessageBody, so the call toresponsedis not valid.To make them use the same subclass, make the
DeviceClientHelpergeneric on a specific subclass ofMessageBodylike so:However, your
MessageResponseHandlerinterface probably doesn’t need to care about theMessageBodyclass. This depends on how it’s actually used, but something like this might work better:And then you can remove the generic type from the
messageHandlerfield and keep the originalsomeMethodimplementation.