MessageConverter类型的方法fromMessage(String,Class <t extended =“” baseobject =“”>)不适用于参数(String,Class <t>)

时间:2018-07-12 04:01:27

标签: java

我有以下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>)

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

shutdown上的签名在声明WebMessageConverter时期望为BaseObject的子类型。

<T extends BaseObject>上的签名没有限制,因为它使用普通的Utils,例如允许使用<T>。这就是编译器抱怨的原因。

要解决,请在Class<String>中声明方法,如下所示:

Utils