skip to main content
research-article
Free Access

Morphing: Structurally shaping a class by reflecting on others

Published:07 February 2011Publication History
Skip Abstract Section

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.

References

  1. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  2. Apache Software Foundation. Byte-code engineering library. http://jakarta.apache.org/bcel/manual.html. (6/09).Google ScholarGoogle Scholar
  3. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  4. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  5. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  6. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  7. Bruenton, E. et al. ASM Java Bytecode Engineering Library: http://asm.ow2.org/. (6/09).Google ScholarGoogle Scholar
  8. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  9. Cannon, H. I. 1982. Flavors: A non-hierarchical approach to object-oriented programming. Tech. rep.Google ScholarGoogle Scholar
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. Csallner, C. and Smaragdakis, Y. 2004. JCrasher: An automatic robustness tester for Java. Softw. Pract. Exper. 34, 11, 1025--1050. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  15. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  16. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  19. Gamma, E., Helm, R., and Johnson, R. 1995. Design Patterns. Elements of Reusable Object-Oriented Software. Addison-Wesley Professional Computing Series. Addison-Wesley. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  21. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  22. Hinze, R., Jeuring, J., and Löh, A. 2004. Type-Indexed data types. Sci. Comput. Program. 51, 1-2, 117--151. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  24. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  25. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  26. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  27. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  28. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  29. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  30. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  31. Kiczales, G., des Rivieres, J., and Bobrow, D. G. 1991. The Art of the Metaobject Protocol. MIT Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  33. 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 ScholarGoogle Scholar
  34. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  35. Lieberman, H. 1986. Using prototypical objects to implement shared behavior in object-oriented systems. SIGPLAN Not. 21, 11, 214--223. Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. 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 ScholarGoogle Scholar
  37. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  38. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  39. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  40. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  41. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  42. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  43. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  44. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  45. 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 ScholarGoogle Scholar
  46. 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 ScholarGoogle Scholar

Index Terms

  1. Morphing: Structurally shaping a class by reflecting on others

        Recommendations

        Reviews

        Scott Arthur Moody

        Language designers have continuously explored various mechanisms to adapt code for reuse with a different purpose. Morphing is the latest concept to extend the notion of genericity to where the actual structure of a class can vary according to type variables. The authors introduce the MorphJ language, providing new mechanisms for Java beyond reflection and generics. The paper describes interesting, detailed, real-world examples to justify the MorphJ concept. For example, the authors show how one can add a default implementation class so that all nonimplemented methods provide a default behavior (such as returning null or 0). In addition, they can provide advanced dynamic type checking for what they refer to as negative or positive nested patterns to morph code only when it is type safe and doesn't invalidate certain methods with, for example, the same name. Nested patterns introduce iteration over the code; existing language constructs cannot do this without a preprocessor code generation approach. The paper provides a very detailed formal proof section to show the correctness of the main features of MorphJ. The MorphJ approach is a very interesting extension to the meta-programming class of language design, and should be explored for future mainstream languages. Online Computing Reviews Service

        Access critical reviews of Computing literature here

        Become a reviewer for Computing Reviews.

        Comments

        Login options

        Check if you have access through your login credentials or your institution to get full access on this article.

        Sign in

        Full Access

        • Published in

          cover image ACM Transactions on Programming Languages and Systems
          ACM Transactions on Programming Languages and Systems  Volume 33, Issue 2
          January 2011
          128 pages
          ISSN:0164-0925
          EISSN:1558-4593
          DOI:10.1145/1890028
          Issue’s Table of Contents

          Copyright © 2011 ACM

          Publisher

          Association for Computing Machinery

          New York, NY, United States

          Publication History

          • Published: 7 February 2011
          • Accepted: 1 March 2010
          • Revised: 1 February 2010
          • Received: 1 November 2009
          Published in toplas Volume 33, Issue 2

          Permissions

          Request permissions about this article.

          Request Permissions

          Check for updates

          Qualifiers

          • research-article
          • Research
          • Refereed

        PDF Format

        View or Download as a PDF file.

        PDF

        eReader

        View online with eReader.

        eReader
        About Cookies On This Site

        We use cookies to ensure that we give you the best experience on our website.

        Learn more

        Got it!