8

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).

public class Foo implements groovy.lang.GroovyObject extends java.lang.Object { }

Groovy object also contains a MetaClass that extends MetaObjectProtocol. 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).

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.

  • Runtime
    • Categories
    • Expando / MetaClass / ExpandoMetaClass
  • Compile Time
    • AST Transformations
    • Extension Module

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

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,

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).

enter image description here

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!!

Dan Vega
  • 1,097
  • 1
  • 14
  • 23
  • I think extensions should be added to the "compile time" meta stuff – Will Oct 26 '13 at 16:20
  • I didn't even know about extension modules, I added it to the list. Thank you! – Dan Vega Oct 26 '13 at 20:10
  • I'm not sure about what to answer here. Your question is about what /is/ the MOP? Like, which classes in the groovy source code implement it? – Will Oct 27 '13 at 00:23
  • What is the Metaobject Protocol (MOP) – Dan Vega Oct 28 '13 at 01:02
  • I think its the collection of runtime and compile time operations available. – Sam Farmer Oct 28 '13 at 16:34
  • Those are metaprogramming features. Here is an excerpt from Programming Groovy 2. "Metaprogramming means writing programs that manipulate programs, including themselves. Dynamic languages such as Groovy provide this functionality through the Metaobject protocol (MOP). Creating classes, writing unit tests, and introducing mock objects are all easy with Grooyv's MOP." When I read this all I want to ask is "What is Grooyv's MOP" – Dan Vega Oct 28 '13 at 19:39

1 Answers1

6

This is the answer. "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." Once you understand the process a method call goes through and the API that comes with it I think it all makes sense. I was just over thinking it all. Thanks!!

Dan Vega
  • 1,097
  • 1
  • 14
  • 23