Java 重写泛型类中的方法

Java 重写泛型类中的方法,java,generics,refactoring,Java,Generics,Refactoring,我正在重构一些单元测试。基本上,我发现不同客户机的单元测试实现了一组方法,例如:createClientWithNullResponse、createClientWithSuccessResponse等 我想知道在Java中是否有可能实现一个通用的解决方案,因为这种方法在数百个单元类中反复出现,只改变了方法签名 但是,有一个棘手的问题。请参见方法示例: /** * configures the client to return a succesful response * @return a

我正在重构一些单元测试。基本上,我发现不同客户机的单元测试实现了一组方法,例如:createClientWithNullResponse、createClientWithSuccessResponse等

我想知道在Java中是否有可能实现一个通用的解决方案,因为这种方法在数百个单元类中反复出现,只改变了方法签名

但是,有一个棘手的问题。请参见方法示例:

/**
 * configures the client to return a succesful response
 * @return a client configured to return a succesful response
 */
private Client1 configureClientWithSuccesfulResponse()
{
    client = new Client1()
    {
        public CommonClientResponse processRequest( CommonsClientRequest commonsClientRequest )
        {
            CommonClientResponse commonClientResponse = new CommonClientResponse();
            commonClientResponse.setResponse( new Client1Response() );
            return commonClientResponse;
        }
    };
    return client;
}
因此,client2将具有完全相同的方法,除了signature具有client2之外,重写的方法将创建一个新的Client2Response,对于几十个客户端也是如此

附加信息:processRequest被重写为模拟,设置我希望每个方法的响应。 Client1扩展了CommonWS,它扩展了AbstractCommons,AbstractCommons是一个抽象类,但包含processRequest方法的实现

总而言之,我的想法是为所有单元测试创建一个基类,使用一组通用方法传递类类型,然后为每个类重写processRequest。我试过:

public class base <T extends AbstractCommonClient>{

    private T configureClientWithNullResponse(Class <? extends AbstractCommonClient> clazz, Class< ? extends ClientResponse> clazz1)
    {

        try
        {
            return clazz.newInstance()
            {
                CommonClientResponse processRequest( CommonsClientRequest commonsClientRequest )
                {
                    CommonClientResponse commonClientResponse = new CommonClientResponse();
                    commonClientResponse.setResponse( clazz1.newInstance() );
                    return commonClientResponse;
                };
            };
        }
    }

}
公共类基{

private T configureClientWithNullResponse(Class这是一个棘手的问题。我建议创建一个Factory类,返回每种类型的客户端,然后提供响应并将其传递给Factory。类似于:

public class ClientFactory {

    public static createResponse(ClientResponse response) {
        CommonClientResponse commonClientResponse = new CommonClientResponse();
        commonClientResponse.setResponse(response);
        return commonClientResponse;
    }

    public static Client1 createClient1(final ClientResponse response) {
       return new Client1() {
            public CommonClientResponse processRequest(CommonsClientRequest unused) {
                return createResponse(response)
            };
        }
    };

    public static Client2 createClient2(final ClientResponse response) {
       return new Client2() {
            public CommonClientResponse processRequest(CommonsClientRequest unused) {
                return createResponse(response)
            };
        }
    };

    ..... // same for every type of Client
你可以用以下方式来称呼它:

    factory.createClient1(new Client1Response());
仍然有一些重复,但它有帮助。有一点。
您觉得怎么样?

当您正在有效地尝试创建一个在运行时类型未知的匿名类时,您是否考虑过在运行时调用编译器?我自己并没有太多使用它,但它可能值得研究。您可以使用

JavaCompiler compiler = javax.tools.ToolProvider.getSystemJavaCompiler();

请注意,只有当应用程序在安装了JDK的系统(如JRE)上运行时,这才有效。

我认为没有任何简单的方法可以创建编译时未知的类的匿名子类–新类(){…}语法在编译过程中生成一个实际的完整类文件。您能发布错误吗?mkro2,不是一个特定的错误,但我不知道如何在使用newInstance创建的实例中重写a方法。谢谢!我可能可以帮助您,但我需要查看所有相关的接口和类定义,以及理想情况下您想要的方法调用希望看到效果。嗨,Guillaume,谢谢你的回答。不,按照你建议的方式,情况不会有太大改善。问题是我需要以通用方式创建客户机,而不是指定每个客户机。例如,请参阅:我需要一个createClientWithNullResponse方法。使用工厂,我需要为每个客户机创建一个方法at需要一个空的响应。我清楚吗?是的,这很清楚,我理解这个问题。我会试着多想想,这是一个有趣的问题。:O我来看看这个问题,但作为一个重构,它似乎过于复杂。医学甚至可能是最糟糕的痛苦…:s