Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Reified Type Parameters Aggelos Biboudis University of Athens 2 Java Generics 101 l = new ArrayList(); l.add("bar"); String s = l.get(0); l = new ArrayList(); l.add("bar"); String s = (String)l.get(0); class ArrayList { X [] arr … } class ArrayList { Object [] arr … }

Slide 3

Slide 3 text

Reified Type Parameters Aggelos Biboudis University of Athens 3 Erasure examples (Technicalities.FAQ108 of Generics, Angelika Langer) Object Number > Comparable > Cloneable > Object Object,Object

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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 extends X - “mixins”

Slide 6

Slide 6 text

Reified Type Parameters Aggelos Biboudis University of Athens 6 We introduce a new annotation class C<@reify X,Y> { ... }

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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(); } }

Slide 9

Slide 9 text

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 > customer = new TimeStamped >(); sn = customer.getSerialNumber(); timeS = customer.getTimestamp();

Slide 10

Slide 10 text

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> String String newInstance()

Slide 11

Slide 11 text

Reified Type Parameters Aggelos Biboudis University of Athens 11 Code Generation: Sharing Code class Foo$Integer extends Foo$Shared { Integer new$X(){ return new Integer(); } } Foo foo = new Foo(); new class w/o reified generics overriding new$X subclassing Foo$Shared class Foo<@reify X> { void meth() { X local = new X(); } } class Foo$Shared implements iface$Foo { X new$X() { return null; } void meth() { X local = new$X(); } } interface iface$Foo { … } Generating an interface and a class One-time generation

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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