Download presentation
Presentation is loading. Please wait.
Published byJairo Arber Modified over 9 years ago
1
Type Erasure: Breaking the Java Type System Suad Alagić, Brian Cabana, Jeff Faulkner and Mark Royer Department of Computer Science University of Southern Maine Boston University, Fall 2004
2
Controversy Years of research for correct solution Years of research for correct solution Solutions based on a wrong idiom Solutions based on a wrong idiom J2SE DK 5.0 based on a wrong solution J2SE DK 5.0 based on a wrong solution JDK 5.0: Violations of the type system JDK 5.0: Violations of the type system A correct solution in fact exists A correct solution in fact exists
3
Previous Work Pizza and GJ Pizza and GJ PolyJ PolyJ Heterogeneous Solutions Heterogeneous Solutions Reflective Solution Reflective Solution NextGen and MixGen NextGen and MixGen C# C# Java Specification Request – JSR14 Java Specification Request – JSR14
4
Bounded Parametric Polymorphism class SortedList { private T [] elements; public SortedList () { elements = new T[size]; elements = new T[size];} public int add(T t) public T remove(int index) public int size(); }
5
Type Erasure class SortedList { private Comparable [] elements; public SortedList() { elements = new Comparable[size]; } public int add(Comparable t); public int add(Comparable t); public Comparable remove(int i); public int size(); public int size();}
6
Implications of Type Erasure Consider SortedList and SortedList Consider SortedList and SortedList Clearly different types but Clearly different types but One and the same class object SortedList One and the same class object SortedList Incorrect run-time type information Incorrect run-time type information
7
Persistence Systems Implications Make SortedList employees persistent Make SortedList employees persistent Reachability: the class object becomes persistent Reachability: the class object becomes persistent But employees.getClass() is SortedList But employees.getClass() is SortedList Is there an object in the database of type SortedList ? Is there an object in the database of type SortedList ?
8
J2SE DK 5.0 Unforgivable Pitfalls: Violation of the Java type system Violation of the Java type system Incorrect run-time type information Incorrect run-time type information Java Core Reflection implications Java Core Reflection implications Overloading Overloading Serialization and type casts Serialization and type casts Reasons for failure: Generic Idiom is provably wrong Generic Idiom is provably wrong Java Virtual Machine must be extended Java Virtual Machine must be extended
9
Violation of Type System Vector v = new Vector (); myMethod(v); Integer i = v.lastElement(); public static void myMethod(Object collection) { // Vector instanceof and cast not allowed if (collection instanceof Vector) ((Vector)collection).add(“trouble”)); ((Vector)collection).add(“trouble”)); }}
10
Subtyping Rules Violated Vector a = new Vector (); Vector b = new Vector(); b.add(new Object()); a = b; … Integer i = a.remove(0);
11
Problems with Overloading public class MyClass { public void myMethod(Vector list) {} public void myMethod(Vector list) {} // Both methods arguments appear as type Vector } public class LinkedList { public void myMethod(G g) {} public void myMethod(G g) {} public void myMethod(Comparable c) {} public void myMethod(Comparable c) {} // Both method arguments appear as type Comparable }
12
Problems with Java Core Reflection public class SomeClass { public void myMethod(Vector value) { String s = value.lastElement(); String s = value.lastElement();}} public class MyClass { public static void main(String[] args) { SomeClass c = new SomeClass(); Vector v = new Vector (); Vector v = new Vector (); Method m = c.getClass().getMethod("myMethod", v.getClass()); Method m = c.getClass().getMethod("myMethod", v.getClass()); v.add(new Integer(123)); v.add(new Integer(123)); m.invoke(c, v); m.invoke(c, v);}}
13
Run-time Exceptions Covariant array subtyping: Covariant array subtyping: Employee [] subtype of Person[] Employee [] subtype of Person[] Static type checking rules violated Static type checking rules violated Dynamic type checks work Dynamic type checks work GJ: wrong run-time type info, no standard exceptions GJ: wrong run-time type info, no standard exceptions Failures at unpredictable places Failures at unpredictable places
14
Implementing Interfaces interface A{ Vector m(Vector x); } Vector m(Vector x); } interface B extends interface A { Vector m(Vector x);} Vector m(Vector x);} Class C implements B { Vector m(Vector x) {return null;} Vector m(Vector x) {return null;} // Vector m(Vector x) {return null;} }
15
Type Names class Ordered { boolean lessThan(T x) {…} boolean lessThan(T x) {…}} Ordered I= new Ordered ; Ordered S = new Ordered ; I.getClass().getName() == S.getClass().getName() == “Ordered” == “Ordered” I = (Ordered)S; S= (Ordered)I;
16
Serialization Class WriteObject { … FileOutputStream fileout = new FileOutputStream("myObject.obj"); FileOutputStream("myObject.obj"); ObjectOutputStream out = new ObjectOutputStream out = new ObjectOutputStream(fileout); ObjectOutputStream(fileout); Vector s = new Vector (); s.add(new Integer(5)); out.writeObject(s); out.writeObject(s);}
17
Serialization and Type Casting class ReadObject { class ReadObject { … FileInputStream filein = new FileInputStream("myObject.obj"); FileInputStream filein = new FileInputStream("myObject.obj"); ObjectInputStream in = new ObjectInputStream(filein); Vector s = null; Vector s = null; try { try { s = (Vector ) in.readObject(); s = (Vector ) in.readObject(); // Warning even for Vector // Warning even for Vector } catch (InvalidClassException e) {…} } catch (InvalidClassException e) {…} System.out.println("Read " + s.get(0)); System.out.println("Read " + s.get(0)); // Cast exception on s.get(0) // Cast exception on s.get(0)}
18
Components of Correct Solution Java Class File Structure Java Class File Structure Optional Attributes Optional Attributes Type Descriptors Type Descriptors Extended Class Loader Extended Class Loader JVM Modifications JVM Modifications Class Objects Class Objects Class Loading Class Loading Extended Java Core Reflection Extended Java Core Reflection
19
Class File Representation Class file for SortedList Class file for SortedList Flexibility in JVM: optional attributes Flexibility in JVM: optional attributes Optional class attributes: positions, names and bounds of type parameters Optional class attributes: positions, names and bounds of type parameters Distinction between type parameters and bound types Distinction between type parameters and bound types Likewise for methods and fields Likewise for methods and fields
20
Parametric Class Attribute Type parameter information Type parameter information Both for bound and actual type Both for bound and actual type Field descriptors Field descriptors Method descriptors Method descriptors Constant pool instructions Constant pool instructions
21
Type Descriptors T[] elements: [LJava/lang/Comparable; T[] elements: [LJava/lang/Comparable; int add(T x): (LJava/lang/Comparable;)I int add(T x): (LJava/lang/Comparable;)I Class object for SortedList : [LJava/lang/Integer; and (LJava/lang/Integer;)I Class object for SortedList : [LJava/lang/Integer; and (LJava/lang/Integer;)I Loader action Loader action
22
Constant Pool: SortedList Constant Pool: SortedList
24
Loading Class Objects Creates class objects with correct actual type information Creates class objects with correct actual type information Class methods and fields have correct descriptors Class methods and fields have correct descriptors Class loading model preserved Class loading model preserved Extended ClassLoader problems Extended ClassLoader problems URLClassLoader and native class loader must be extended URLClassLoader and native class loader must be extended
25
Class Loading
26
Java Core Reflection Extending final classes: Class, Method and Field Extending final classes: Class, Method and Field Additional methods Additional methods Is a class parametric, type parameters, bound types, actual type parameters Is a class parametric, type parameters, bound types, actual type parameters No negative effect on JCR No negative effect on JCR Recompilation of the whole platform Recompilation of the whole platform
27
Code Multiplicity C# solution avoids code multiplicity: C# solution avoids code multiplicity: multiple v-tables with actual types multiple v-tables with actual types NextGen uses multiple class objects: NextGen uses multiple class objects: elaborate representation elaborate representation JVM requires more serious changes JVM requires more serious changes Is this justified? Is this justified? Type safety versus some memory penalty Type safety versus some memory penalty
28
Conclusions JDK 5.0 is based on a wrong idiom (GJ type erasure) JDK 5.0 is based on a wrong idiom (GJ type erasure) No correct solution unless JVM is extended No correct solution unless JVM is extended It is possible to construct an extension which does not violate integrity of JVM It is possible to construct an extension which does not violate integrity of JVM Legacy code will not be affected Legacy code will not be affected
29
Conclusions Java is not type safe any more! Java is not type safe any more! There is a better way! There is a better way!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.