Program Analysis Group Laboratory of Computer Science Massachusetts Institute of Technology


Inference of Generic Types in Java Source Code

Alan Donovan
Adam Kiezun
Matthew Tschantz
Michael D. Ernst

Note: Jiggetai is now superceded by our current work on generics-related refactorings.


Version 1.5 of the Java programming language (beta-released in Jan 2004) will include support for generic types. Generic types (or parametric polymorphism) make it possible to write a class or procedure abstracted over the types of its arguments.

In the absence of generic types, Java programmers have been writing and using pseudo-generic classes, such as the collection classes in java.util: with such classes, it is up to the programmer to remember (or document) what kind of elements each container holds. This is burdensome, and causes programs to be littered with casts. Worse, a programmer's first indication of an error in such code is typically a runtime exception due to a failing cast.

In contrast, parametric types provide compile-time checking: forbidden operations, such as inserting an Integer into a Vector<String>, are discovered earlier. Not only do parametric types prevent this kind of error, but they also provide more precise documentation of the code.

We present Jiggetai, a tool that reads Java source code and pre-compiled class libraries, infers parametric types for the declarations in the source code, and emits a syntactically correct and semantically equivalent generic Java program (but otherwise preserving the features of the input program).

The implementation of Jiggetai is nearly complete, and it supports all features of both the source and target languages. We will be making the beta-release version of the tool available for download in Summer 2004.


For example, assuming the following generic library class Box:

class Box<T extends Object>
    public  void set(T v) { this.v = v; }
    public  T    get()    { return v;   }

    private T v;

Jiggetai translates the input program on the left into the generic program on the right:

Box bs = new Box();
Box bi = new Box();
bi.set(new Integer(3));
Object s = bs.get();
Object i = bi.get();
Box<String>  bs = new Box<String>();
Box<Integer> bi = new Box<Integer>();
bi.set(new Integer(3));
String s = bs.get();
Integer i = bi.get();



Alan Donovan, Adam Kieżun, Matthew S. Tschantz and Michael Ernst: Converting Java Programs to use Generic Libraries In proceedings of OOPSLA 2004, Vancover, BC, Canada. [ PDF | PS ]


Gilad Bracha, Martin Odersky, David Stoutamire, Philip Wadler: Making the Future Safe for the Past: Adding Genericity to the Java Programming Language, In proceedings of OOPSLA 1998, Vancouver, BC. Canada. [ ACM Portal ]


This webpage has been translated into Belorussian.
Last updated: 29 June 2011