In computer science, a metaobject is an object that manipulates, creates, describes, or implements other objects (including itself). The object that the metaobject is about is called the base object. Some information that a metaobject might store is the base object's type, interface, class, methods, attributes, parse tree, etc. Metaobjects are examples of the computer science concept of reflection, where a system has access (usually at run time) to its internal structure. Reflection enables a system to essentially rewrite itself on the fly, to change the actual structure of the system as it executes.
A metaobject protocol (MOP) provides the vocabulary (protocol) to access and manipulate the structure and behavior of systems of objects. Typical functions of a metaobject protocol include:
- Creating and deleting new classes
- Creating new methods and properties
- Changing the class structure so that classes inherit from different classes
- Generating or modifying the code that defines the methods for the class
Metaobject protocol is contrary to the "closed" aspect of Bertrand Meyer's open/closed principle, which holds that object-software systems should be open to extension but closed to modification. Metaobject protocol transparently exposes the internal structure of objects and the entire object system, opening the possibility for radical redesign. For this reason it is usually used sparingly and for special circumstances such as software that transforms other software, for example for reverse engineering.
Runtime and compile time
When compilation is not available at run-time there are additional complications for the implementation of metaobject protocol. For example, it is possible to change the type hierarchy with such a protocol but doing so may cause problems for code compiled with an alternative class model definition. Some environments have found innovative solutions for this, e.g., by handling metaobject issues at compile time. A good example of this is OpenC++.
It is noteworthy that the Semantic web object-oriented model is more dynamic than most standard object systems, and is consistent with runtime metaobject protocols. For example, in the Semantic web model classes are expected to change their relations to each other and there is a special inference engine known as a classifier that can validate and analyze evolving class models.
The first metaobject protocol was in the Smalltalk object-oriented programming language developed at Xerox PARC. The Common Lisp Object System (CLOS) came later and was influenced by the Smalltalk protocol. The CLOS model, unlike the Smalltalk model, allows a class to have more than one superclass. This raises additional complexity in issues such as resolving the lineage of the class hierarchy on some object instance, which is, unlike in Smalltalk, handled via generic functions rather than message passing. The most influential book describing the semantics and implementation of the metaobject protocol in Common Lisp is The Art of the Metaobject Protocol by Gregor Kiczales et al.
Metaobject protocols are also extensively used in software engineering applications. In virtually all commercial CASE, re-engineering, and Integrated Development Environments there is some form of metaobject protocol to represent and manipulate the design artifacts.
A metaobject protocol is one way to implement aspect-oriented programming. Many of the early founders of MOPs, including Gregor Kiczales, have since moved on to be the primary advocates for aspect-oriented programming. Kiczales et al. of PARC were hired to design AspectJ for Java, a language which does not possess a native metaobject protocol.
- Kind (type theory)
- Moose Perl meta object system
- Unified Modeling Language: UML
- Smith, Brian C (1982-01-01). "PROCEDURAL REFLECTION IN PROGRAMMING LANGUAGES". MIT Technical Report (MIT-LCS-TR-272). Retrieved 16 December 2013.
- Foote, Brian; Ralph Johnson (1–6 October 1989). "Reflective Facilities in Smalltalk-80". OOPSLA '89. Retrieved 16 December 2013.
- Favre, Lilliana; Liliana Martinez; Claudia Pereira (2009). MDA-Based Reverse Engineering of Object Oriented Code. Springer. ISBN 978-3-642-01861-9.
- Chiba, Shigeru (1995). "A Metaobject Protocol for C++". OOPSLA '95. Retrieved 27 December 2013.
- Knublauch, Holger; Oberle, Daniel; Tetlow, Phil; Wallace, Evan (2006-03-09). "A Semantic Web Primer for Object-Oriented Software Developers". W3C. Retrieved 2008-07-30.
- "Integrating Object-Oriented and Functional Programming" (PDF). Retrieved 7 July 2016.
- Kiczales, Gregor; Jim des Rivieres; Daniel G. Bobrow (July 30, 1991). The Art of the Metaobject Protocol. The MIT Press. ISBN 978-0262610742.
- Johnson, Lewis; David R. Harris; Kevin M. Benner; Martin S. Feather (October 1992). "Aries: The Requirements/Specification Facet for KBSA". Rome Laboratory Final Technical Report. RL-TR-92-248.
- "The Origin of Refine" (PDF). www.metaware.fr. Metaware White Paper. Retrieved 6 January 2014.
- "OMG's MetaObject Facility". omg.org. Object Management Group. Retrieved 7 January 2014.
- The Guile MOP specification (GOOPS, based on Tiny CLOS)
- Metaobjects and the Metaobject Protocol
- The Common Lisp Object System MetaObject Protocol (contains two chapters from The Art of the Metaobject Protocol)
- Python 3 Metaprogramming