title: Metaobject Protocol (MOP) in Groovy
body: <p>I am new to the Groovy programming language and I am trying to fully understand the dynamic nature and capabilities it has. What I do know is that every class created in Groovy in its most basic form looks like this (implements GroovyObject and extending java Object).</p>
  
  <pre><code>public class Foo implements groovy.lang.GroovyObject extends java.lang.Object { }
  </code></pre>
  
  <p>Groovy object also contains a <a href="http://groovy.codehaus.org/api/groovy/lang/MetaClass.html" rel="noreferrer">MetaClass</a> that extends <a href="http://groovy.codehaus.org/api/groovy/lang/MetaObjectProtocol.html" rel="noreferrer">MetaObjectProtocol</a>. It is this class hierarchy that provides some of Groovy's dynamic capabilities. This includes the ability to introspect itself (getProperties,getMethods) or intercept methods (invokeMethod,missingMethod).</p>
  
  <p>I also understand the different types of meta programming available in Groovy. These give you the ability to add or override functionality at runtime  or compile time.</p>
  
  <ul>
  <li>Runtime
  
  <ul>
  <li>Categories</li>
  <li>Expando / MetaClass / ExpandoMetaClass</li>
  </ul></li>
  <li>Compile Time
  
  <ul>
  <li>AST Transformations</li>
  <li>Extension Module</li>
  </ul></li>
  </ul>
  
  <p>Now that have some of that out of the way we can get to the meat of this question. When someone or a book refers to the "Metaobject Protocol" in Groovy are we talking about a specific class or a collection of things. I have hard time grasping something that isn't defined or set in stone. One of my books defined it as </p>
  
  <blockquote>
    <p>A protocol is a collection of rules and formats. The
    Meta-Object-Protocol (MOP) is the collection of rules of how a request
    for a method call is handled by the Groovy runtime system and how to
    control the intermediate layer. The "format" of the protocol is
    defined by the respective APIs,</p>
  </blockquote>
  
  <p>I also have Venkat's Programming Groovy 2 book and in it there is a diagram that defines this method lookup process. So I am guessing this is the rules of how we request a method (at least a POGO, I understand a POJO is different). </p>
  
  <p><img src="https://i.stack.imgur.com/gVoUQ.png" alt="enter image description here"></p>
  
  <p>Anyways I think I am going down the right path but I feel like I am still missing that "ahhhaa" moment. Can anyone fill me in on what I am missing? Or at the very least tell me my ramblings here made some sort of sense :) Thank you!!</p>