Linked Questions

Popular Questions

my questions relates to introducing Generics into legacy Java classes. The legacy classes from the code below that I want to generify are ClientObjectProxy and ClientObjectContainer. ClientObject remains unchanged. For simplicity I put all the classes into one outer class.

Now in the legacy code there are many calls of the method ClientObjectProxyContainer.getProxies() on ClientObjectProxyContainer instances that are not parameterized.

By introducing Generics existing code like in Loop 4 won't compile anymore, the call needs to be extracted to a local variable like in Loop 1 or a declaration with a question mark needs to be used similar to Loop 2. New code should use parameterized variables like in Loop 3.

My question is, is this is the way to go for me, change the existing code to either Loop 1 or Loop 2, or is there's a way of not modifying legacy code?

Thanks

Jan

import java.util.ArrayList;
import java.util.List;

public class GenericForumsExample {
    /**
     * This class is unchanged
     */
    private class ClientObject {
    }

    /**
     * @param <T> Type of the ClientObject behind this proxy
     */
    private class ClientObjectProxy<T extends ClientObject> {
    }

    /**
     * @param <T> Type of the ClientObject contained in this collection
     */
    private class ClientObjectProxyContainer<T extends ClientObject> {
        //Previous signature was:
        //  public List<ClientObjectProxy> getProxies(){
        // New signature is the following
        public List<ClientObjectProxy<T>> getProxies(){
            return new ArrayList<ClientObjectProxy<T>>();
        }
    }

    public void testScenario() {
        ClientObjectProxyContainer proxyContainer = new ClientObjectProxyContainer();
        List<ClientObjectProxy> proxies = proxyContainer.getProxies(); // Just a compiler warning

        // Loop 1
        for (ClientObjectProxy proxy : proxies) {                     // Compiler OK
            //Do something...
        }

        // Loop 2
        ClientObjectProxyContainer<?> clientObjectProxyContainer = new ClientObjectProxyContainer();
        for (ClientObjectProxy<?> proxy : clientObjectProxyContainer.getProxies()) {
            //Do something...
        }

        // Loop 3
        for (ClientObjectProxy<ClientObject> proxy : new ClientObjectProxyContainer<ClientObject>().getProxies()) {
            //Do something...
        }

        // Loop 4
        // Compiler fails on next code line
        // incompatible types
        // found   : java.lang.Object
        // required: GenericForumsExample.ClientObjectProxy
        for (ClientObjectProxy proxy : proxyContainer.getProxies()) {
            //Do something...
        }
    }

}

Related Questions