Title: PL Seminar Spring 2002
1Making the future safe for the past (time
paradox)Adding Genericity to the Java
Programming Language
Gilad Bracha, Martin Odersky, David Stoutamire,
Philip Wadler
Appeared in OOPSLA 98
Presented by Anand Krishnan
Center for Distributed Object Computing Department
of Computer Science Washington University
February 13, 2002
2 Javas current support for genercity
- Generics by the idiom
- replace variable types by the top of type
hieararchy - Requires type casts
- Observer pattern introduced JDK 1.1
- Collection Classes introduced JDK 1.2
- JSR-000014 Adding Generics to the JavaTM
Programming Language - http//jcp.org/jsr/detail/14.jsp
-
3Example uses of type parameterization
- Stack
- stacks of T objects
- Hashtable
- the T object must be hashable
- Priority Queue
- the T object must provide a consistent ordering
relation - Mix-in
- Deriving from a parameterized Base Class
-
4 Generic Legacy Problem
- Impact on legacy code due to addition of direct
support for generics - Pizza - backward compatible
- Pizza - Incompatibility between legacy type and
parametric type - rewrite legacy code or write adapter code
- problems include
- code bloat
- managing upgrade of dispersed legacy code
- legacy code available only as binaries
-
5 Styles of implementing Generics
- Syntactic instance - Stack Stack
- Semantic instance - data structure and compiled
code associated with a syntactic instance -
- Homogeneous
- the generic idiom - Instances of parameterized
construct share same implementation - possibilty of security breach
- Heterogeneous
- Data structure and compiled code associated with
a syntactic instance - different run time representations for methods
- support type parameterization in languages where
all types cannot be referred to in a uniform way. - Choice depends on language semantics
- eg. Parameterized class with a static variable
6 A GJ Example
interface ConvertibleTo A
convert()
interface ConvertibleTo B
convert()
class ReprChange,
B implements
ConvertibleTo
A a void set(B x) a x.convert() B
get() return a.convert()
7 Translating GJ
- erasure
- parametric type delete the parameter
- non-parametric type - do nothing
- type parameter erase the bound
- erase all arg types and return types
- insert type casts where required
- insert bridge methods to ensure proper overriding
- bridging done so that bridge and original method
share the same name -
8GJ - the bytecode meddler
class Interval implements Iterator
private int i, n public Interval (int l,
int u) i l n u public boolean
hasNext () return (i Integer next () return new Integer(i)
UponTranslation
interface Iterator public boolean hasNext
() public Object next ()
class Interval implements Iterator private
int i, n public Interval (int l, int u) i
l n u public boolean hasNext ()
return (i / () return new Integer(i) public
Object next / 2 / () return next / 1 /()
9Covariant Overriding
class C implements Cloneable public C copy
() return (C)this.clone () class D extends
C implements Cloneable public D copy ()
return (D)this.clone ()
10Invariant Subtyping
List ! Subtype( List )
String Subtype (Object )
11Polymorphic Methods and Type Inference
public LinkedList singleton (A x)
public LinkedList doublet (A x, A y)
- Inference - choose the smallest type parameter
that yields a valid call - call to doublet with Integer and Float - A
inferred as Number - call to doublet with Integer and String -
Inference Fails (common supertypes - Comparable
and Serializable) - Special Bottom Type - null type
- call to doublet with Integer and - A inferred
as Integer
12Security Implications
Problem
class SecureChannel extends Channel public
String read () public void add
(SecureChannel x) . class C
public LinkedList cs
...
13Security Implications
Solution
Problem
class SecureChannelList extends
LinkedList
SecureChannelList () super()
public void add (SecureChannel x)
super.add(x) class C
public LinkedList cs
...
class SecureChannel extends Channel public
String read () public void add
(SecureChannel x) . class C
public LinkedList cs
...
14Security Implications
Solution on Translation
Problem
class SecureChannelList extends LinkedList
SecureChannelList ()
super() public void add (Object
x) super.add((SecureChannel)x)
class C public
LinkedList cs ...
class SecureChannel extends Channel public
String read () public void add
(SecureChannel x) . class C
public LinkedList cs
...
15Heterogeneous Translation -divorced from the JVM
security model
- JVM Security Model
- visibilty for top-level classes package wide
- public visibility
-
p.C
16Heterogeneous Translation -divorced from the JVM
security model
- JVM Security Model
- visibilty for top-level classes package wide
- public visibility
-
p.C
- if D is public, place instantiation in p
17Heterogeneous Translation -divorced from the JVM
security model
- JVM Security Model
- visibilty for top-level classes package wide
- public visibility
-
p.C
- if D is public, place instantiation in p
- else if body of C refers only to public
classes - place instantiation in q
- else cannot instantiate type
18GJ Restrictions - Object and Array Creation
- For a type variable A
- new A () - illegal
- new An
- generates an unchecked warning
- indicates possible violation of GJs type
soundness - rather use Vector or ArrayList
- Use an array as a factory for arrays of similar
type
19GJ Restrictions - Object and Array Creation
class BadArray public static A
singleton (A x) return new A x
// unchecked warning public static
void main (String args) String a
singleton("zero")
20GJ Restrictions - Casts Instance tests
class Convert public static
Collection up (LinkedList xs)
return (Collection)xs
Collection
LinkedList
public static LinkedList down
(Collection xs) if (xs instanceof
LinkedList) return
(LinkedList)xs else
throw new ConvertException()
21GJ Restrictions - Casts Instance tests
class BadConvert public static Object
up (LinkedList xs) return
(Object)xs
Object
LinkedList
public static LinkedList down
(Object o) if (o instanceof
LinkedList) return
(LinkedList)o else
throw new ConvertException() // red
indicates compile time error
Solution - LinkedListString class extending
LinkedList - Wrapper class
LinkedListStringWrapper
22Raw Types
- Parameterized Types stripped of its parameters
- Maintain consistency with legacy code
- Useful in cast and instance tests
Signature of member of object of Raw Type
Erasure of signature of member of object of
Parameterized Type
Value of parameterized Type is a Value of
Corrseponding RawType For eg Iterator
Param_Instance Iterator Raw_Instance
Param_Instance
Value of Raw Type is a Value of Corrseponding
Parameterized Type For eg Iterator
Raw_Instance Iterator Param_Instance
Raw_Instance //unchecked warning
23Raw Types
- Method calls/Field assignments to Raw Types
generate unchecked warning if erasure changes
argument/field type
class Loophole public static String
loophole (Byte y) LinkedList xs
new LinkedList() LinkedList ys
xs ys.add(y) // unchecked warning
return xs.iterator().next()
24Retrofitting Mode
- Retro-actively parameterize classes
- Attach generic types to prexisting class files,
even if binaries - Add signature attributes to Code
class LinkedList implements Collection
public LinkedList () public void add (A
elt) public Iterator iterator ()
- Rewriting of code can be done conveniently
- May involve recoding in certain cases
- Map method returning Key/Value has to be
split
25Other Flavors (thanks to Martin Linenweber)
- Poor mans genericity for java
- wrapper script for Java interpreter
- works during class loading
- MIT proposal
- homogeneous translation scheme
- change to JVM
- maintain runtime representation of parameteric
types - avoid casts
- backward compatible through raw types
- Stanford Proposal
- heterogeneous translation
- generic code compiled to special byte code format
- GJ expands on this with reflection
26GJ Conclusions
- Easy to interface with legacy code
- backward compatible through raw types
- straightforward use of reflection on GJ programs
- forward compatible through retrofitting
- made forward compatible with NextGen
- maintains parameters at run time
- relaxes GJs restrictions
27Questions ?
The Asimov Gang of Four
Making Java easier to type / 1 /and easier to
type / 2 /