我有以下Java代码:
public abstract class BaseObject implements Serializable {
...
}
public interface MessageConverter {
<T extends BaseObject> T getMessage(String message, Class<T> requiredType);
}
public abstract class AbstractMessageConverter implements MessageConverter {
...
}
public class WebMessageConverter extends AbstractMessageConverter {
public <T extends BaseObject> T getMessage(String message, Class<T> requiredType) {
final T data;
...
return data;
}
}
public class Utils {
private static <T> T getItemType(String value, Class<T> requiredType, MessageConverter messageConverter) throws Exception
{
if (BaseObject.class.isAssignableFrom(requiredType))
{
return (T) messageConverter.getMessage(value, requiredType);
}
return (T) toRequiredType(value, requiredType);
}
public static <T> T toRequiredType(Object value, Class<T> requiredType)
{
final T data;
...
return data;
}
}
以下行:
return (T) messageConverter.getMessage(value, requiredType);
原因:
The method getMessage(String, Class<T extends BaseObject>) in the type MessageConverter is not applicable for the arguments (String, Class<T>)
如何解决此错误?
答案 0 :(得分:1)
shutdown
上的签名在声明WebMessageConverter
时期望为BaseObject
的子类型。
<T extends BaseObject>
上的签名没有限制,因为它使用普通的Utils
,例如允许使用<T>
。这就是编译器抱怨的原因。
要解决,请在Class<String>
中声明方法,如下所示:
Utils