Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

Morphing: Structurally shaping a class by reflecting on others

Published: 07 February 2011 Publication History
  • Get Citation Alerts
  • 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.
    [2]
    Apache Software Foundation. Byte-code engineering library. http://jakarta.apache.org/bcel/manual.html. (6/09).
    [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.
    [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.
    [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.
    [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.
    [7]
    Bruenton, E. et al. ASM Java Bytecode Engineering Library: http://asm.ow2.org/. (6/09).
    [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.
    [9]
    Cannon, H. I. 1982. Flavors: A non-hierarchical approach to object-oriented programming. Tech. rep.
    [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.
    [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.
    [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.
    [13]
    Csallner, C. and Smaragdakis, Y. 2004. JCrasher: An automatic robustness tester for Java. Softw. Pract. Exper. 34, 11, 1025--1050.
    [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.
    [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.
    [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.
    [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.
    [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.
    [19]
    Gamma, E., Helm, R., and Johnson, R. 1995. Design Patterns. Elements of Reusable Object-Oriented Software. Addison-Wesley Professional Computing Series. Addison-Wesley.
    [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.
    [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.
    [22]
    Hinze, R., Jeuring, J., and Löh, A. 2004. Type-Indexed data types. Sci. Comput. Program. 51, 1-2, 117--151.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [31]
    Kiczales, G., des Rivieres, J., and Bobrow, D. G. 1991. The Art of the Metaobject Protocol. MIT Press.
    [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.
    [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.
    [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.
    [35]
    Lieberman, H. 1986. Using prototypical objects to implement shared behavior in object-oriented systems. SIGPLAN Not. 21, 11, 214--223.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.
    [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.

    Cited By

    View all

    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

    Information & Contributors

    Information

    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
    Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

    Publisher

    Association for Computing Machinery

    New York, NY, United States

    Publication History

    Published: 07 February 2011
    Accepted: 01 March 2010
    Revised: 01 February 2010
    Received: 01 November 2009
    Published in TOPLAS Volume 33, Issue 2

    Permissions

    Request permissions for this article.

    Check for updates

    Author Tags

    1. Metaprogramming
    2. language extensions
    3. morphing

    Qualifiers

    • Research-article
    • Research
    • Refereed

    Funding Sources

    Contributors

    Other Metrics

    Bibliometrics & Citations

    Bibliometrics

    Article Metrics

    • Downloads (Last 12 months)31
    • Downloads (Last 6 weeks)5

    Other Metrics

    Citations

    Cited By

    View all
    • (2019)A Survey of Metaprogramming LanguagesACM Computing Surveys10.1145/335458452:6(1-39)Online publication date: 16-Oct-2019
    • (2018)Efficient Reusable CollectionsIEICE Transactions on Information and Systems10.1587/transinf.2018EDP7105E101.D:11(2710-2719)Online publication date: 1-Nov-2018
    • (2017)Unifying analytic and statically-typed quasiquotesProceedings of the ACM on Programming Languages10.1145/31581012:POPL(1-33)Online publication date: 27-Dec-2017
    • (2017)A Comparative Study of Visualizations with Different Granularities of Behavior for Communicating about AutismProceedings of the ACM on Human-Computer Interaction10.1145/31346941:CSCW(1-16)Online publication date: 6-Dec-2017
    • (2017)Privacy Leakage in Event-based Social NetworksProceedings of the ACM on Human-Computer Interaction10.1145/31346701:CSCW(1-22)Online publication date: 6-Dec-2017
    • (2017)Structured Program Generation TechniquesGrand Timely Topics in Software Engineering10.1007/978-3-319-60074-1_7(154-178)Online publication date: 29-Jun-2017
    • (2015)FᴏᴏProceedings of the 17th Workshop on Formal Techniques for Java-like Programs10.1145/2786536.2786540(1-4)Online publication date: 7-Jul-2015
    • (2014)MorphScalaProceedings of the Fifth Annual Scala Workshop10.1145/2637647.2637650(18-22)Online publication date: 28-Jul-2014
    • (2014)Building call graphs for embedded client-side code in dynamic web applicationsProceedings of the 22nd ACM SIGSOFT International Symposium on Foundations of Software Engineering10.1145/2635868.2635928(518-529)Online publication date: 11-Nov-2014
    • (2014)Early detection of type errors in C++ templatesProceedings of the ACM SIGPLAN 2014 Workshop on Partial Evaluation and Program Manipulation10.1145/2543728.2543731(133-144)Online publication date: 11-Jan-2014
    • Show More Cited By

    View Options

    View options

    PDF

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader

    Get Access

    Login options

    Full Access

    Media

    Figures

    Other

    Tables

    Share

    Share

    Share this Publication link

    Share on social media