Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reified Type Parameters Using Java Annotations

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
Tweet

More Decks by Aggelos Biboudis

Other Decks in Research

Transcript

  1. 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
  2. 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 … }
  3. 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
  4. 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
  5. 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”
  6. Reified Type Parameters Aggelos Biboudis University of Athens 6 We

    introduce a new annotation class C<@reify X,Y> { ... }
  7. 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
  8. 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(); } }
  9. 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();
  10. 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()
  11. 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
  12. 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
  13. 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
  14. 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