有一堂课
public class Action<R extends CommonResponse>
在其中调用方法并通过函数循环运行
R response = null;
for (PreDecoder preDecoder : stateMachineContext.getPreDecoders()) {
R preDecodedResponse = preDecoder.preDecode(request, preDecodedData.data, preDecodedData);
if (preDecodedResponse != null) {
response = preDecodedResponse;
break;
}
}
PreDecode - 带有方法的功能接口
<R extends CommonResponse> R preDecode(Message rqMessage, ByteBuffer data, ByteBufferRef preDecodedData);
PreDecode 接口的示例实现
@Override
public CommonRS preDecode(Message rqMessage, ByteBuffer data, ByteBufferRef preDecodedData) {
int sourceAddress = data.get() & 0xFF;
int destinationAddress = data.get() & 0xFF;
if (destinationAddress != (localAddress & 0xFF) || sourceAddress != (remoteAddress & 0xFF)) {
CommonRS response = new CommonRS(rqMessage.getFunctionType());
response.setSuccess(false);
return response;
}
preDecodedData.data = data;
return null;
}
在接口的实现中,由于maven不想构建 Unchecked overriding: return type requires unchecked转换项目,出现Warning。找到'...CommonRS',需要'R'
有了这个,问题就出现了:如何真正解决这个问题?
UPD
如果 preDecoder 方法有 CommonResponse 返回类型,那么Action类方法中会发生Unchecked cast