Title: The Case for Runtime Types in Generic Java
1The Case for Run-time Types in Generic Java
- Eric E. Allen
- Robert Corky Cartwright
- Rice University
2Generic Types What are they and why bother?
3Generic Types What are they and why bother?
- Stack s new Stack()
-
-
- Integer i
s.pop()
4Solution 1 Insert Casts
- Stack s new Stack()
-
-
- Integer i
(Integer)s.pop()
5Solution 2 Duplicate Code
6Solution 3 Extend the Type System
7Generic Java
- public class VectorltT extends Objectgt
- public T elementAt(int i)
-
-
- public class StackltTgt extends VectorltTgt
- public T pop()
8Prior Attempts
- Hetereogeneous solution (copy code for each
instantiation) - Modify the JVM
- Type Erasure
9Type Erasure
- Generic types are used only for type checking
- After type checking, all generic types are erased
to their non-parametric upper bounds
10Type Erasure
- TreeltTgt ? Tree
- TreeltIntegergt ? Tree
- T (in class Tree) ? Object
11Type Erasure Disadvantages
- No support for run-time type operations on
generic types - casting
- new operations
- instanceof operations
12NextGen
- Designed in tandem with GJ
- Extended from the GJ compiler
- No substantial overhead when compared to JSR-14
(Allen, Cartwright, Stoler 2002) - More expressiveness than GJ/JSR-14
13Type dependent operations are necessary for many
natural object-oriented designs in the context of
static type checking
14Examples
- The Singleton Design Pattern
- Interface Cloneable
- Functional mapping over arrays
- Debuggers and run-time development tools
15The Singleton Pattern
abstract class Tree class Leaf extends Tree
class Branch extends Tree Object value
Tree left Tree right
16The Singleton Pattern
class Leaf extends Tree public static final
Leaf ONLY new Leaf() private Leaf()
17The Singleton Pattern in NextGen
class LeafltTgt extends TreeltTgt public static
final LeafltTgt ONLY new LeafltTgt()
private Leaf()
18The Singleton Pattern in JSR-14
class LeafltTgt extends TreeltTgt public static
final LeafltIntegergt ONLY_INTEGER new
LeafltIntegergt() public static final
LeafltStringgt ONLY_STRING new
LeafltStringgt()
19Interface Cloneable
20Interface Cloneable
- Vector v2 (Vector)v.clone()
21Interface Cloneable
- VectorltTgt v2 (VectorltTgt)v.clone()
22Functional Mapping over Arrays
- public interface MapperltA,Bgt
- public B map(A element)
23Functional Mapping over Arrays
- public class ArrayMapper
- ltA,Bgt public static B mapArrayltA,Bgt aMapper)
- B out new Bin.length
- for (int i 0 i lt in.length i)
- outi aMapper.map(ini)
-
-
-
24Limitations of Debugging with JSR-14
- All information about the instantiations of type
variables is lost in JSR-14 - Debuggers cant report any information about the
instantiations
25Limitations of other Run-time Development Tools
- Other testing harnesses (e.g., JUnit) cant test
the values of these instantiations - Read-eval-print-loops will display values as
instances of erased type