在泛型方法中创建返回类型的实例

时间:2013-05-31 14:44:42

标签: c# java generics

我希望在java中创建一个通用的辅助方法,它模仿以下C#方法的功能。

private T Execute<T>(Request request) where T : Response, new()
{
     .... Code
}

在以下情况下调用上述方法

    public AddLogicalDeviceResponse AddLogicalDevice(AddLogicalDeviceRequest request)
    {
        return Execute<AddLogicalDeviceResponse>(request);
    }


    public GetConfigurationResponse GetConfiguration(GetConfigurationRequest request)
    {
        return Execute<GetConfigurationResponse>(request);
    }


    public GetDeviceDefinitionsResponse GetDeviceDefinitions(GetDeviceDefinitionsRequest request)
    {
        return Execute<GetDeviceDefinitionsResponse>(request);
    }


    public RemoveLogicalDeviceResponse RemoveLogicalDevice(RemoveLogicalDeviceRequest request)
    {
        return Execute<RemoveLogicalDeviceResponse>(request);
    }


    public GetEPCListCommandResponse GetEPCListCommand(GetEPCListCommandRequest request)
    {
        return Execute<GetEPCListCommandResponse>(request);
    }


    public DeviceSpecificCommandResponse DeviceSpecificCommand(DeviceSpecificCommandRequest request)
    {
        return Execute<DeviceSpecificCommandResponse>(request);
    }


    public GetConfigValuesCommandResponse GetConfigValuesCommand(GetConfigValuesCommandRequest request)
    {
        return Execute<GetConfigValuesCommandResponse>(request);
    }


    public SetConfigValuesCommandResponse SetConfigValuesCommand(SetConfigValuesCommandRequest request)
    {
        return Execute<SetConfigValuesCommandResponse>(request);
    }


    public UpdateFirmwareCommandResponse UpdateFirmwareCommand(UpdateFirmwareCommandRequest request)
    {
        return Execute<UpdateFirmwareCommandResponse>(request);
    }

上述每个案例都在各自的情况下扩展了一个类响应或请求。

我尝试使用Java泛型创建与上述C#代码相同的实现,但我没有取得任何成功。是否可以仅使用一个辅助方法实现相同的功能,或者我被迫为每种类型的调用创建单独的帮助方法。

编辑:

这是我当前的Java实现

private <T extends Response> execute(Request request)
{
    String url = request.BuildUri(_baseUri, _accessKey);
    Object[] response = Send(url, "");

    if(((Integer)response[1] == 200))
    {
        T jsResponse = new T();
        jsResponse.Body = (String)response[0];
        jsResponse.Request = url;
        return jsResponse;
    }
    else
    {
        throw new ResponseException(response[1], response[2], url, response[0]);
    }
}

我目前的麻烦源于无法将退货识别为有效对象的功能。无法将对象T初始化为响应,并且T对象无法识别在Response中实现的方法。

2 个答案:

答案 0 :(得分:3)

在Java中,如果不将T作为参数发送,则无法创建Class<T>类的实例。以下是将此用作当前选项的示例:

interface Request {}

interface Response {
    public String sayHi();
}

class DeviceRequest implements Request {}
class DeviceResponse implements Response{
    public String sayHi() { return "DeviceResponse"; }
}

class WebRequest implements Request {}
class WebResponse implements Response{
    public String sayHi() { return "WebResponse"; }
}

class Helper {
    private <T extends Response> T execute(Request request, Class<T> clazz) throws InstantiationException, IllegalAccessException {
        return clazz.newInstance();
    }
    public DeviceResponse foo(DeviceRequest request) throws InstantiationException, IllegalAccessException {
        return execute(request, DeviceResponse.class);
    }
    public WebResponse bar(WebRequest request) throws InstantiationException, IllegalAccessException {
        return execute(request, WebResponse.class);
    }
}

class Consumer {
    public void baz() throws InstantiationException, IllegalAccessException {
        Helper helper = new Helper();
        DeviceResponse dr = helper.foo(new DeviceRequest());
        WebResponse wr = helper.bar(new WebRequest());
        System.out.println(dr.sayHi());
        System.out.println(wr.sayHi());
    }
    public static void main(String[] args) throws Exception {
        new Consumer().baz();
    }
}

请注意,由于这只是一个示例,我更倾向于抛出异常而不处理它们。对于真实世界的应用程序,您应该处理异常而不是仅仅抛出它们。

答案 1 :(得分:1)

在Java中,您可以在签名的其他部分之前定义泛型参数。

private <T extends Response> T execute(Request request)
{
     .... Code
}