Abstract
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.
- Allen, E., Bannet, J., and Cartwright, R. 2003. A first-class approach to genericity. In Proceedings of the 18th Annual ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'03). ACM Press, New York, 96--114. Google Scholar
Digital Library
- Apache Software Foundation. Byte-code engineering library. http://jakarta.apache.org/bcel/manual.html. (6/09).Google Scholar
- Bachrach, J. and Playford, K. 2001. The Java syntactic extender (JSE). In Proceedings of the 16th ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'01). ACM Press, New York, 31--42. Google Scholar
Digital Library
- Baker, J. and Hsieh, W. C. 2002. Maya: Multiple-Dispatch syntax extension in Java. In Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI'02). ACM Press, New York, 270--281. Google Scholar
Digital Library
- Batory, D., Lofaso, B., and Smaragdakis, Y. 1998. JTS: Tools for implementing domain-specific languages. In Proceedings of the 5th International Conference on Software Reuse. Los Alamitos, CA, 143--153. Google Scholar
Digital Library
- Bracha, G. and Cook, W. 1990. Mixin-Based inheritance. In Proceedings of the European Conference on Object-Oriented Programming and Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA/ECOOP'90). ACM Press, New York, 303--311. Google Scholar
Digital Library
- Bruenton, E. et al. ASM Java Bytecode Engineering Library: http://asm.ow2.org/. (6/09).Google Scholar
- Calcagno, C., Taha, W., Huang, L., and Leroy, X. 2003. Implementing multi-stage languages using ASTs, gensym, and reflection. In Proceedings of the 2nd International Conference on Generative Programming and Component Engineering (GPCE'03). Springer, Berlin, 57--76. Google Scholar
Digital Library
- Cannon, H. I. 1982. Flavors: A non-hierarchical approach to object-oriented programming. Tech. rep.Google Scholar
- Chakravarty, M. M. T., Keller, G., and Jones, S. P. 2005a. Associated type synonyms. In Proceedings of the 10th ACM SIGPLAN International Conference on Functional Programming (ICFP'05). ACM, New York, 241--253. Google Scholar
Digital Library
- Chakravarty, M. M. T., Keller, G., Jones, S. P., and Marlow, S. 2005b. Associated types with class. In Proceedings of the 32nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'05). ACM Press, 1--13. Google Scholar
Digital Library
- Cook, W. R., Hill, W., and Canning, P. S. 1990. Inheritance is not subtyping. In Proceedings of the 17th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'90). ACM Press, New York, 125--135. Google Scholar
Digital Library
- Csallner, C. and Smaragdakis, Y. 2004. JCrasher: An automatic robustness tester for Java. Softw. Pract. Exper. 34, 11, 1025--1050. Google Scholar
Digital Library
- Danforth, S. and Forman, I. R. 1994. Reflections on metaclass programming in SOM. In Proceedings of the 9th ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'94). ACM Press, New York, 440--452. Google Scholar
Digital Library
- Draheim, D., Lutteroth, C., and Weber, G. 2005. A type system for reflective program generators. In Proceedings of the 4th International Conference on Generative Programming and Component Engineering. Lecture Notes in Computer Science, vol. 3676. Springer, Berlin, 327--341. Google Scholar
Digital Library
- Ducasse, S., Nierstrasz, O., Schärli, N., Wuyts, R., and Black, A. P. 2006. Traits: A mechanism for fine-grained reuse. ACM Trans. Program. Lang. Syst. 28, 2, 331--388. Google Scholar
Digital Library
- Ekman, T. and Hedin, G. 2007. The JastAdd extensible Java compiler. In Proceedings of the 22nd Annual ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'07). ACM, New York, 1--18. Google Scholar
Digital Library
- Fähndrich, M., Carbin, M., and Larus, J. R. 2006. Reflective program generation with patterns. In Proceedings of the 5th International Conference on Generative Programming and Component Engineering. ACM Press, New York, 275--284. Google Scholar
Digital Library
- Gamma, E., Helm, R., and Johnson, R. 1995. Design Patterns. Elements of Reusable Object-Oriented Software. Addison-Wesley Professional Computing Series. Addison-Wesley. Google Scholar
Digital Library
- Gibbons, J. 2007. In Spring School on Datatype-Generic Programming, R. Backhouse, J. Gibbons, R. Hinze, and J. Jeuring, Eds. Lecture Notes in Computer Science, vol. 4719. Springer, Berlin. Google Scholar
Digital Library
- Herlihy, M., Luchangco, V., and Moir, M. 2006. A flexible framework for implementing software transactional memory. In Proceedings of the 21st Annual ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'06). ACM, New York, 253--262. Google Scholar
Digital Library
- Hinze, R., Jeuring, J., and Löh, A. 2004. Type-Indexed data types. Sci. Comput. Program. 51, 1-2, 117--151. Google Scholar
Digital Library
- Huang, S. S. and Smaragdakis, Y. 2006. Easy language extension with Meta-AspectJ. In Proceedings of the International Conference on Software Engineering (ICSE'06). ACM, New York, 865--868. Google Scholar
Digital Library
- Huang, S. S. and Smaragdakis, Y. 2008. Expressive and safe static reflection with MorphJ. In Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI'08). Vol. 43. ACM, New York, 79--89. Google Scholar
Digital Library
- Huang, S. S., Zook, D., and Smaragdakis, Y. 2005. Statically safe program generation with SafeGen. In Proceedings of the 4th International Conference on Generative Programming and Component Engineering. Lecture Notes in Computer Science, vol. 3676. Springer, Berlin, 309--326. Google Scholar
Digital Library
- Huang, S. S., Zook, D., and Smaragdakis, Y. 2007a. cJ: Enhancing Java with safe type conditions. In Proceedings of the 6th International Conference on Aspect-Oriented Software Development. ACM Press, 185--198. Google Scholar
Digital Library
- Huang, S. S., Zook, D., and Smaragdakis, Y. 2007b. Morphing: Safely shaping a class in the image of others. In Proceedings of the 21st Annual ACMSIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (ECOOP'07), E. Ernst, Ed. Lecture Notes in Computer Science, vol. 4609. Springer, Berlin, 303--329. Google Scholar
Digital Library
- Igarashi, A., Pierce, B., and Wadler, P. 2001. Featherweight Java: A minimal core calculus for Java and GJ. ACM Trans. Program. Lang. Syst. 23, 3, 396--450. Google Scholar
Digital Library
- Igarashi, A. and Viroli, M. 2006. Variant parametric types: A flexible subtyping scheme for generics. ACM Trans. Program. Lang. Syst. 28, 5, 795--847. Google Scholar
Digital Library
- Jansson, P. and Jeuring, J. 1997. PolyP—a polytypic programming language extension. In Proceedings of the 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'97). ACM Press, New York, 470--482. Google Scholar
Digital Library
- Kiczales, G., des Rivieres, J., and Bobrow, D. G. 1991. The Art of the Metaobject Protocol. MIT Press. Google Scholar
Digital Library
- Kiczales, G., Hilsdale, E., Hugunin, J., Kersten, M., Palm, J., and Griswold, W. G. 2001. An overview of AspectJ. In Proceedings of the 15th Annual ACMSIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (ECOOP'01). Springer, Berlin, 327--353. Google Scholar
Digital Library
- Kiczales, G., Lamping, J., Menhdhekar, A., Maeda, C., Lopes, C., Loingtier, J.-M., and Irwin, J. 1997. Aspect-Oriented programming. In Proceedings of the 11th Annual ACMSIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (ECOOP'97), M. Akşit and S. Matsuoka, Eds. Vol. 1241. Springer, Berlin, 220--242.Google Scholar
- Lämmel, R. and Jones, S. P. 2003. Scrap your boilerplate: A practical design pattern for generic programming. In Proceedings of the ACM SIGPLAN International Workshop on Types in Languages Design and Implementation (TLDI'03). ACM, New York, 26--37. Google Scholar
Digital Library
- Lieberman, H. 1986. Using prototypical objects to implement shared behavior in object-oriented systems. SIGPLAN Not. 21, 11, 214--223. Google Scholar
Digital Library
- Mens, K., Michiels, I., and Wuyts, R. 2001. Supporting software development through declaratively codified programming patterns. In Proceedings of the 13th International Conference on Software Engineering and Knowledge Engineering. 136--143.Google Scholar
- Mohnen, M. 2002. Interfaces with default implementations in Java. In Proceedings of the Inaugural Conference on the Principles and Practice of Programming. 35--40. Google Scholar
Digital Library
- Reppy, J. and Turon, A. 2007. Metaprogramming with traits. In Proceedings of the Annual ACMSIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (ECOOP'07). Springer, Berlin, 373--398. Google Scholar
Digital Library
- Schrijvers, T., Peyton Jones, S., Chakravarty, M., and Sulzmann, M. 2008. Type checking with open type functions. In Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming (ICFP'08). ACM, New York, 51--62. Google Scholar
Digital Library
- Sheard, T. and Jones, S. P. 2002. Template meta-programming for Haskell. In Proceedings of the ACM SIGPLAN Workshop on Haskell. ACM Press, 1--16. Google Scholar
Digital Library
- Smaragdakis, Y. and Batory, D. 1998. Implementing layered designs with mixin layers. In Proceedings of the 12th Annual ACMSIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (ECOOP'98). Lecture Notes in Computer Science, vol. 1445. Springer, Berlin, 550--570. Google Scholar
Digital Library
- Stein, L. A. 1987. Delegation is inheritance. In Proceedings on ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'87). ACM, New York, 138--146. Google Scholar
Digital Library
- Taha, W. and Sheard, T. 1997. Multi-Stage programming with explicit annotations. In Proceedings of the ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation. ACM Press, 203--217. Google Scholar
Digital Library
- Torgersen, M., Hansen, C. P., Ernst, E., von der Ahe, P., Bracha, G., and Gafter, N. 2004. Adding wildcards to the java programming language. In Proceedings of the ACM Symposium on Applied Computing (SAC'04). ACM Press, 1289--1296. Google Scholar
Digital Library
- Visser, E. 2004. Program transformation with Stratego/XT: Rules, strategies, tools, and systems in Stratego/XT 0.9. In Domain-Specific Program Generation, C. Lengauer, D. Batory, C. Consel, and M. Odersky, Eds. Lecture Notes in Computer Science, vol. 3016. Springer, Berlin, 216--238.Google Scholar
- Weirich, S. and Huang, L. 2004. A design for type-directed Java. In Proceedings of the Workshop on Object-Oriented Developments (WOOD). V. Bono, Ed. ENTCS.Google Scholar
Index Terms
Morphing: Structurally shaping a class by reflecting on others
Recommendations
Domain-specific languages and program generation with meta-AspectJ
Meta-AspectJ (MAJ) is a language for generating AspectJ programs using code templates. MAJ itself is an extension of Java, so users can interleave arbitrary Java code with AspectJ code templates. MAJ is a structured metaprogramming tool: a well-typed ...
Sound type-dependent syntactic language extension
POPL '16: Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming LanguagesSyntactic language extensions can introduce new facilities into a programming language while requiring little implementation effort and modest changes to the compiler. It is typical to desugar language extensions in a distinguished compiler phase after ...
Expressive and safe static reflection with MorphJ
PLDI '08Recently, language extensions have been proposed for Java and C# to support pattern-based reflective declaration. These extensions introduce a disciplined form of meta-programming and aspect-oriented programming to mainstream languages: They allow ...








Comments