Share
Embed
Reified Type Parameters Using Java Annotations
This is the slide deck of my talk at GPCE '13 about adding reified generics to Java using a (JSR 308) generic type annotation called @reify.
http://www.di.uoa.gr/~biboudis/reified-annot-gpce13.pdf
Aggelos Biboudis
October 27, 2013
Transcript
14 slides
-
Reified Type Parameters Using Java Annotations Prodromos Gerakios Aggelos Biboudis
Yannis Smaragdakis Department of Informatics University of Athens 12th International Conference on Generative Programming: Concepts & Experiences (GPCE'13) October 27, 2013 Indianapolis, IN, USA -
Reified Type Parameters Aggelos Biboudis University of Athens 2 Java
Generics 101 l = new ArrayList<String>(); l.add("bar"); String s = l.get(0); l = new ArrayList(); l.add("bar"); String s = (String)l.get(0); class ArrayList<X> { X [] arr … } class ArrayList { Object [] arr … } -
Reified Type Parameters Aggelos Biboudis University of Athens 3 Erasure
examples (Technicalities.FAQ108 of Generics, Angelika Langer) <T> Object <T extends Number> Number <T extends Comparable<T>> Comparable <T extends Cloneable & Comparable<T>> Cloneable <T extends Object & Comparable<T>> Object <S, T extends S> Object,Object -
Reified Type Parameters Aggelos Biboudis University of Athens 4 What
type erasure implies (the good) ✔ Code sharing in the bytecode level ✔ Compatibility with the “unaware of genericity” JVM ✔ Fewer source code type casts ✔ Type safety preservation -
Reified Type Parameters Aggelos Biboudis University of Athens 5 What
type erasure implies (the bad) ✗ Type casts required ✗ By default autoboxing occurs ✗ No reflection support for generic parameters ✗ Expressiveness limitations (for X type param) ✗ new X ✗ X.class ✗ class C<X> extends X - “mixins” -
Reified Type Parameters Aggelos Biboudis University of Athens 6 We
introduce a new annotation class C<@reify X,Y> { ... } -
Reified Type Parameters Aggelos Biboudis University of Athens 7 Contributions
• Add reified generics for Java without a custom compiler! • Translate by expansion while sharing generated code • Control expansion or erasure of generics • Support new patterns in Java new T() and extends T, where T type parameter • Design as pluggable type checker -
Reified Type Parameters Aggelos Biboudis University of Athens 8 @reify
will enable selective reification class ReifiedGeneric <@reify X,Y> { Class classOfX = X.class; Y id(Y y) { return y; } X newInstance() { return new X(); } } -
Reified Type Parameters Aggelos Biboudis University of Athens 9 @reify
will introduce mixin support class Serial <@reify T> extends T { public long getSerialNumber() { … }} class TimeStamped <@reify T> extends T { public long getTimestamp() { … }} TimeStamped <Serial <Customer>> customer = new TimeStamped <Serial <Customer>>(); sn = customer.getSerialNumber(); timeS = customer.getTimestamp(); -
Reified Type Parameters Aggelos Biboudis University of Athens 10 Is
this a well-formed mixed-in definition? No. Two overloaded methods with distinct return types. X in ObjectFactory needs constraint. class GenericFactory<@reify X> { X newInstance() { return new X(); }} class ObjectFactory<@reify X> extends X { Object newInstance(){ return new Object(); } } ObjectFactory<GenericFactory<String>> String String newInstance() -
Reified Type Parameters Aggelos Biboudis University of Athens 11 Code
Generation: Sharing Code class Foo$Integer extends Foo$Shared<Integer> { Integer new$X(){ return new Integer(); } } Foo<Integer> foo = new Foo<Integer>(); new class w/o reified generics overriding new$X subclassing Foo$Shared<X> class Foo<@reify X> { void meth() { X local = new X(); } } class Foo$Shared<X> implements iface$Foo<X> { X new$X() { return null; } void meth() { X local = new$X(); } } interface iface$Foo<X> { … } Generating an interface and a class One-time generation -
Reified Type Parameters Aggelos Biboudis University of Athens 12 JSR
308 & Checker • JSR 308 introduced annotations on types • JSR 308 will be part of Java 8 • Can be processed as in JSR 269 (annotation processing) but also … • Can be processed with Checker Framework to write a checker plugin for type checking and generation -
Reified Type Parameters Aggelos Biboudis University of Athens 13 Conclusions
• Java generics with selective reification • Translation scheme by-expansion • Mixins and allocation expressions with generic parameters • A solution that complies with Java 8 -
Reified Type Parameters Aggelos Biboudis University of Athens 14 Questions?
Contact biboudis @ di.uoa.gr www.di.uoa.gr/~biboudis/ @biboudis See you tomorrow at 6:00-9:00 pm in Splash Poster Reception - Cosmopolitan Foyer