The Case for Runtime Types in Generic Java - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

The Case for Runtime Types in Generic Java

Description:

The Case for Run-time Types in Generic Java. Eric E. Allen. Robert 'Corky' Cartwright ... Limitations of other Run-time Development Tools ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 26
Provided by: tri5114
Category:
Tags: case | generic | java | runtime | types

less

Transcript and Presenter's Notes

Title: The Case for Runtime Types in Generic Java


1
The Case for Run-time Types in Generic Java
  • Eric E. Allen
  • Robert Corky Cartwright
  • Rice University

2
Generic Types What are they and why bother?
3
Generic Types What are they and why bother?
  • Stack s new Stack()
  • Integer i

s.pop()
4
Solution 1 Insert Casts
  • Stack s new Stack()
  • Integer i

(Integer)s.pop()
5
Solution 2 Duplicate Code
6
Solution 3 Extend the Type System
7
Generic Java
  • public class VectorltT extends Objectgt
  • public T elementAt(int i)
  • public class StackltTgt extends VectorltTgt
  • public T pop()

8
Prior Attempts
  • Hetereogeneous solution (copy code for each
    instantiation)
  • Modify the JVM
  • Type Erasure

9
Type Erasure
  • Generic types are used only for type checking
  • After type checking, all generic types are erased
    to their non-parametric upper bounds

10
Type Erasure
  • TreeltTgt ? Tree
  • TreeltIntegergt ? Tree
  • T (in class Tree) ? Object

11
Type Erasure Disadvantages
  • No support for run-time type operations on
    generic types
  • casting
  • new operations
  • instanceof operations

12
NextGen
  • 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

13
Type dependent operations are necessary for many
natural object-oriented designs in the context of
static type checking
14
Examples
  • The Singleton Design Pattern
  • Interface Cloneable
  • Functional mapping over arrays
  • Debuggers and run-time development tools

15
The Singleton Pattern
abstract class Tree class Leaf extends Tree
class Branch extends Tree Object value
Tree left Tree right
16
The Singleton Pattern
class Leaf extends Tree public static final
Leaf ONLY new Leaf() private Leaf()
17
The Singleton Pattern in NextGen
class LeafltTgt extends TreeltTgt public static
final LeafltTgt ONLY new LeafltTgt()
private Leaf()
18
The 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()
19
Interface Cloneable
  • Object clone()

20
Interface Cloneable
  • Vector v2 (Vector)v.clone()

21
Interface Cloneable
  • VectorltTgt v2 (VectorltTgt)v.clone()

22
Functional Mapping over Arrays
  • public interface MapperltA,Bgt
  • public B map(A element)

23
Functional 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)

24
Limitations 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

25
Limitations 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
Write a Comment
User Comments (0)
About PowerShow.com