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

ChromaKey: Towards Extensible Reflective Architectures, Pablo Inostroza

Meta Workshop
October 30, 2016

ChromaKey: Towards Extensible Reflective Architectures, Pablo Inostroza

Meta Workshop

October 30, 2016
Tweet

More Decks by Meta Workshop

Other Decks in Research

Transcript

  1. ChromaKey • Reflection is a powerful way to inspect and

    modify object behavior • But: • What is reflected onto (the “canvas”) is tied to language and/or VM • No support for language extensions (e.g., new kinds of class members, new kinds of “classes”) • ChromaKey: extend mirror-based reflection with additional indirection through generic factories (Object Algebras)
  2. Mirror based ObjMirror mirror = Reflect.reflect(obj); ClassMirror clazz = mirror.getClass();

    interface ↓ encapsulation Object is oblivious to reflection ↓ stratification
  3. ChromaKey ObjMirror<MyC> mirror = Reflect.reflect(obj, classAlg); MyC clazz = mirror.getClass();

    NB: MyC is not the type of the reflected object, but the type of the meta class
  4. class EvArith implements Arith<Integer> { Integer lit(Integer n){ return n;

    } Integer add(Integer l, Integer r){ return l + r; } class PrintArith implements Arith<String>{ String lit(Integer n){ return n.toString(); } String add(String l, String r){ return "(" + l + " + " + r + “)”; } } New Carrier Type 6 Object Algebras interface Arith<E>{ E lit(Integer n); E add(E l, E r); }
  5. Object Algebras class EvArith implements Arith<Integer> { Integer lit(Integer n){

    return n; } Integer add(Integer l, Integer r){ return l + r; } interface Arith<E>{ E lit(Integer n); E add(E l, E r); } class EvMul extends EvArith implements Mul<Integer> { Integer mul(Integer l, Integer r){ return l * r; } } interface Mul<E> extends Arith<E>{ E mul(E l, E r); }
  6. ChromaKey ObjMirror<C> reflect(Object o, ClassAlg<C,M,F,B> sem) interface ObjMirror { <C,M,F,B>

    C getClass(); ... } Implementation of this is out of scope of this talk (hint: requires some base level reflection, or VM support…) Additional argument to represent the “semantics” of classes: an Object Algebra
  7. The green screen: Object Algebras interface ClassAlg<C, M, F, B>

    { C Class(String name, List<M> members); M Method(F formal, List<F> params, B body); M Field(F formal); F Formal(String name, Class<?> type); B Code(java.lang.reflect.Method m); }
  8. What does it get you? • Chromakey: further decoupling from

    meta level and object level • The meta level becomes a parameter under control of the user • Benefits • Decoupling: replace the mirror type hierarchy with your own. • Extensible: Reflect upon “entities” not anticipated by the language/runtime (i.e. for user extensible languages).
  9. For now: let’s start from classes <C,M,F,B> C reflect(Class<?> clazz,

    ClassAlg<C, M, F, B> sem) C clazz = reflect(Foo.class, semantics);
  10. Reflecting onto Strings class ToString implements ClassAlg<String,String,String,String> { public String

    Class(String name, List<String> members) { return "class " + name + "{" + ... + "}"; } public String Method(String f, List<String> fs, String b) { ... } public String Field(String f) { ... } public String Formal(String name, java.lang.Class<?> type) { ... } public String Code(java.lang.reflect.Method m) { ... } }
  11. Language extension: primary keys class Person { key String email;

    String name; } New kind of class member class Person { @Key String email; String name; } Current implementation WARNING! Leaving Standard Java (related to Recaf, see GPCE’16)
  12. Reflecting extensions interface KeyAlg<C,M,F,B> extends ClassAlg<C,M,F,B> { M Key(F formal);

    } Constructor method for the new language construct (like Field)
  13. Application: custom class semantics • DIY reflection semantics! • Not

    only for base Java, but for extensions too! • Related to “managed data” (see Onward!’12, GPCE’16)
  14. Simulating classes and objects interface MyClass { Object New(Object ...args);

    } class MyObj implements InvocationHandler { … } Classes are object factories. Objects are invocation handlers for dynamic proxies
  15. The “class” builder class MySemantics implements ClassAlg<MyClass, …> { public

    MyClass Class(String name, List members) { Class clazz = Class.forName(name); return new MyClass() { public Object New(Object... args) { return Proxy.newProxyInstance(…, new Class<?>[] { clazz } , new MyObj(name, members)); } }; } … }
  16. Using MySemantics… MyClass pointClass = reflect(Point.class, new MySemantics()); Point p

    = (Point) pointClass.New(1, 2); System.out.println(p.x() + p.y()); // prints out 3 interface Point { int x(); int y(); } Need to use interfaces (“technical detail”; see Eugster, OOPSLA’06)
  17. Why custom semantics? • Aspects: logging, persistence, access policies, contracts,

    etc. • Custom data layout: store two ints in one long, off heap data structures, etc. • Maintain data invariants: immutability, maximal sharing, transactions, etc.
  18. Discussion • Requirements on base-level reflection / VM support 


    (implementation of reflect) • Typing? Needs type constructor polymorphism
 
 <T,C<_>,M,F,B> C<T> reflect(Class<T> c, ClassAlg<C<T>, M, F, B> sem)
  19. Conclusion • ChromaKey: next step in decoupling meta level from

    object level • Brings Object Algebra extensibility to mirror-based reflection • Choose your own reflection semantics • Reflect upon user level language extensions • Ongoing work: implementation, evaluation (managed data).