Introduce Generics into legacy code and iteration over parameterized instances

186 views Asked by At

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...
        }
    }

}
2

There are 2 answers

0
Philipp Wendler On BEST ANSWER

In code dealing with generics, try to avoid using raw types at all. When you use these types, you will often loose more type-information than you expect, as is happening in this case.

The call proxyContainer.getProxies() in the last loop returns actually a List (because proxyContainer is of a raw type, all generics relating to its type are removed, including generics in method signatures). And if you iterate over a raw List, you will only get Objects out of it, so there is a compile error. You can resolve this by writing (List<ClientObjectProxy>)proxyContainer.getProxies() in the for loop (you will get a warning of course).

Thus it is usually clearer to use wildcards instead of raw types. In code which is already "genericified", never use the raw type ClientObjectProxyContainer, but always ClientObjectProxyContainer<?>, if you don't have a concrete type. This type has basically the same meaning, but it will not lead to all generic types being ignored when it is used.

When using the wildcard for the type of proxyContainer, the result type of getProxies() will be List<ClientProxyObject<?>> instead of just List, so you can take ClientProxyObjects out of it (but here, too, prefer to use ClientProxyObject<?>!).

0
UVM On

I prefer Loop 1 approach if you introduce the Generics.Then you do not have worry about the type checking.