Morphing: Structurally shaping a class by reflecting on others

We present MorphJ: a language for specifying general classes whose members are produced by iterating over members of other classes. We call this technique “class morphing” or just “morphing.” Morphing extends the notion of genericity so that not only types of methods and fields, but also the structure of a class can vary according to type variables. This adds a disciplined form of metaprogramming to mainstream languages and allows expressing common programming patterns in a highly generic way that is otherwise not supported by conventional techniques. For instance, morphing lets us write generic proxies (i.e., classes that can be parameterized with another class and export the same public methods as that class); default implementations (e.g., a generic do-nothing type, configurable for any interface); semantic extensions (e.g., specialized behavior for methods that declare a certain annotation); and more. MorphJ's hallmark feature is that, despite its emphasis on generality, it allows modular type-checking: a MorphJ class can be checked independently of its uses. Thus, the possibility of supplying a type parameter that will lead to invalid code is detected early, an invaluable feature for highly general components that will be statically instantiated by other programmers. We demonstrate the benefits of morphing with several examples, including a MorphJ reimplementation of DSTM2, a software transactional memory library which reduces 1,484 lines of Java reflection and bytecode engineering library calls to just 586 lines of MorphJ code.

[1]  Cyril S. Ku,et al.  Design Patterns , 2008, Wiley Encyclopedia of Computer Science and Engineering.

[2]  William G. Griswold,et al.  An Overview of AspectJ , 2001, ECOOP.

[3]  Kim Mens,et al.  Supporting software development through declaratively codified programming patterns , 2002, Expert Syst. Appl..

[4]  Gilad Bracha,et al.  Adding wildcards to the Java programming language , 2004, SAC '04.

[5]  Daniel G. Bobrow,et al.  Book review: The Art of the MetaObject Protocol By Gregor Kiczales, Jim des Rivieres, Daniel G. and Bobrow(MIT Press, 1991) , 1991, SGAR.

[6]  Simon Peyton Jones,et al.  Type checking with open type functions , 2008, ICFP 2008.

[7]  Eelco Visser,et al.  Program Transformation with Stratego/XT: Rules, Strategies, Tools, and Systems in Stratego/XT 0.9 , 2003, Domain-Specific Program Generation.

[8]  Walid Taha,et al.  Implementing Multi-stage Languages Using ASTs, Gensym, and Reflection , 2003, GPCE.

[9]  Henry Lieberman,et al.  Using Prototypical Objects to Implement Shared Behavior in Object Oriented Systems , 1986, OOPSLA.

[10]  Oscar Nierstrasz,et al.  Traits: A mechanism for fine-grained reuse , 2006, TOPL.

[11]  Walid Taha,et al.  Multi-stage programming with explicit annotations , 1997, PEPM.

[12]  Philip Wadler,et al.  Featherweight Java: a minimal core calculus for Java and GJ , 2001, TOPL.

[13]  Simon L. Peyton Jones,et al.  Associated types with class , 2005, POPL '05.

[14]  Ralph Johnson,et al.  design patterns elements of reusable object oriented software , 2019 .

[15]  Lynn Andrea Stein,et al.  Delegation is inheritance , 1987, OOPSLA '87.

[16]  Simon L. Peyton Jones,et al.  Template meta-programming for Haskell , 2002, Haskell '02.

[17]  Yannis Smaragdakis,et al.  Implementing Layered Designs with Mixin Layers , 1998, ECOOP.

[18]  Jonathan Bachrach,et al.  The Java syntactic extender (JSE) , 2001, OOPSLA '01.

[19]  Markus Mohnen Interfaces with default implementations in Java , 2002, PPPJ/IRE.

[20]  Mirko Viroli,et al.  Variant parametric types: A flexible subtyping scheme for generics , 2006, TOPL.

[21]  Yannis Smaragdakis,et al.  JTS: tools for implementing domain-specific languages , 1998, Proceedings. Fifth International Conference on Software Reuse (Cat. No.98TB100203).

[22]  Wilson C. Hsieh,et al.  Maya: multiple-dispatch syntax extension in Java , 2002, PLDI '02.

[23]  B. E. Eckbo,et al.  Appendix , 1826, Epilepsy Research.

[24]  Torbjörn Ekman,et al.  The jastadd extensible java compiler , 2007, OOPSLA.

[25]  Simon L. Peyton Jones,et al.  Associated type synonyms , 2005, ICFP '05.

[26]  William R. Cook,et al.  Inheritance is not subtyping , 1989, POPL '90.

[27]  Howard I. Cannon Flavors : A non-hierarchical approach to object-oriented programming , 2007 .

[28]  Jeremy Gibbons Datatype-Generic Programming , 2006, SSDGP.

[29]  Robert Cartwright,et al.  A first-class approach to genericity , 2003, OOPSLA 2003.

[30]  Johan Jeuring,et al.  PolyP—a polytypic programming language extension , 1997, POPL '97.

[31]  Ralf Hinze,et al.  Type-indexed data types , 2004, Sci. Comput. Program..

[32]  Gregor Kiczales,et al.  Aspect-oriented programming , 2001, ESEC/FSE-9.

[33]  Shan Shan Huang,et al.  cJ: enhancing java with safe type conditions , 2007, AOSD.

[34]  Dirk Draheim,et al.  A type system for reflective program generators , 2005, GPCE'05.

[35]  Shan Shan Huang,et al.  Easy language extension with meta-aspectJ , 2006, ICSE.

[36]  James R. Larus,et al.  Reflective program generation with patterns , 2006, GPCE '06.

[37]  Shan Shan Huang,et al.  Statically safe program generation with safegen , 2005, GPCE'05.

[38]  Shan Shan Huang,et al.  Expressive and safe static reflection with MorphJ , 2008, PLDI '08.

[39]  Walid Taha,et al.  Multi-stage programming with explicit annotations , 1997 .

[40]  Simon L. Peyton Jones,et al.  Type checking with open type functions , 2008, ICFP.

[41]  Gregor Kiczales,et al.  Aspect-oriented programming , 1996, CSUR.

[42]  William R. Cook,et al.  Mixin-based inheritance , 1990, OOPSLA/ECOOP '90.

[43]  Henry Lieberman,et al.  Using prototypical objects to implement shared behavior in object-oriented systems , 1986, OOPLSA '86.

[44]  Robert Cartwright,et al.  A first-class approach to genericity , 2003, OOPSLA '03.

[45]  Simon L. Peyton Jones,et al.  Scrap your boilerplate: a practical design pattern for generic programming , 2003, TLDI '03.

[46]  Aaron Turon,et al.  Metaprogramming with Traits , 2007, ECOOP.

[47]  Ira R. Forman,et al.  Reflections on Metaclass Rorgramming in SOM , 1994, OOPSLA.

[48]  Maurice Herlihy,et al.  A flexible framework for implementing software transactional memory , 2006, OOPSLA '06.

[49]  Shan Shan Huang,et al.  Morphing: Safely Shaping a Class in the Image of Others , 2007, ECOOP.

[50]  Yannis Smaragdakis,et al.  JCrasher: an automatic robustness tester for Java , 2004, Softw. Pract. Exp..