Version 1.5 of the Java programming language will include generics, a language construct for associating type parameterswith classes and methods. Generics are particularly useful for creating statically type-safe, reusable container classes such that a store of an inappropriate type causes a compile-time error, and that no down-casting is needed when retrieving elements. The standard libraries released with Java 1.5 will include generic versions of popular container classes such as HashMap and ArrayList. This paper presents a method for refactoring Java programs that use current container classes into equivalent Java 1.5 programs that use their new generic counterparts. Our method uses a variation on an existing model of type constraints to infer the element types of container objects, and it is parameterized by how much, if any, context sensitivity to exploit when generating these type constraints. We present both a context-insensitive instantiation of the framework and one using a low-cost variation on Agesenfs Cartesian Product Algorithm. The method has been implemented in Eclipse, a popular open-source development environment for Java. We evaluated our approach on several small benchmark programs, and found that, in all but one case, between 40% and 100% of all casts can be removed.
