Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Next »





Groovy 3 semantics and new MOP










Jochen "blackdrag" Theodorou



Last modification:


Last update by:


For quite long we are thinking about a new MOP for Groovy, without finding the right way  to do it better this time. It is difficult to find the right path between being user-friendly and efficient. Thus the new MOP will contain some features the old did not, but will also remove some old features for the sake of a more straight and more easy to understand MOP.


Removing default null argument

The default null argument is used for method calls that have one parameter, but the call is done without an argument for the parameter. Groovy will here use null, as long as the type of the parameter is not a primitive. In case of a primitive the call fails. This feature was mainly added in the early Groovy pre 1.0 years before Groovy supported default values for parameters. Another limitation of this logic is that it works only with single parameter methods. There have been multiple cases of confused users by this logic. Performance wise this logic doesn't cost much, and certainly not more than a call with a real argument. But since this is a confusing feature of limited use, it should be removed.

Removing automatic list expansion

A method call done with a list that finds no matching method for that list (a method with one parameter of type List, Collection, Object, etc), will cause a second method selection iteration. This time the list is "unpacked" and all elements of the list are taken as if the method call had been done with the elements rather than the list. Groovy also supports spreading of lists by an syntax element, making this automatic feature not needed. In fact this can be quite surprising for users and is a problem for performance. A spread version might be still not good in performance, but at least the user will have to use an extra symbol and thus have the visual indicator. As of why this feature was originally added is unclear. Looking at user code you will find barely intended usages of this. Thus it should be removed.

Changing Safe Navigation to stop evaluation

Currently an expression like a?.b.c will fail if a is null. It will not evaluate b, but it will try to evaluate c on null. This defies the intend of safe navigation to avoid a NullPointerException. Thus this should be changed to stop the evaluation of the path expression.

User Wish List

  • Instance based Categories by Wujek Srujek

    Instead of having to provide a class with static methods it would be good to be able to feed an instance to the use-construct and that then we will use this instance along with its instance methods. This allows instance state to be used.

    tracking issue: missing

  • Replacing Introspector by blackdrag

    Not only because of bugs like GROOVY-5019 there should be areplacement of the Introspector

    tracking issue: missing

Some of the leading design ideas of this MOP

The plan is to orientate us a lot on the open call site caching the JVM provides with invokedynamic. For this to work all parts of the MOP should no longer be seen as places that do invoke something, but as places that return something, that then will be invoked. An invokeMethod then will for example return instead an object that acts as a kind of handler, which can be invoked. Groovy will then store it and avoid the reselection unless you invalidate it. In the old MOP such caching often did not happen once you interact using meta programming. The tasks to be solved in this are to provide an "extension point" for intercepting methods and to react to missing methods, as well as being able to invalidate a cached version and/or to make an uncached version possible.

MOP2 outline

in meta class of x in pseudo code:

as an explanation to the methods:

  • getMethodInterceptor
    Extension point for the user to register an interceptor, that will be used for all method calls. This is equal what GroovyInterceptable does using invokeMethod, only that in MOP1 the method is directly called. In MOP2 we will instead return a handler that then will do whatever is needed. This can for example be a method handle to a invokeMethod method if wanted. TODO: define guards
  • getFittingMethod
    Predefined way to select a method through meta class. This method will return an object which then can be used to invoke the selected method.
  • getMethodMissingHandler
    Extension point for the user to register a handler for missing methods. The then invoked method is comparable to what methodMissing in MOP1 would be doing.
  • MethodMissingException
    thrown if none of the above methods is bearing a non null result
  • storeInCallSite
    Predefined method to store the cachable result of the above process in the call site. As long as the cached object is valid the above methods will not be called again for this call site.
  • invoke
    Predefined method to do the Initial invocation of the cachable. Subsequent invocation may be done directly by invokedynamic.

Context Meta Class

For the MOP2 Groovy will use a system of meta classes with a context element and an always existing default. At each call site only one such view will be valid and it will be constant. Those views can be used to defined "sealed" meta class, which won't get influenced by outside meta classes or to allow for example calling private methods and not allowing them in other cases. This makes 3 possible meta class variations currently:

  1. Default meta class
    Behaves pretty much like what Java allows in regards of method calls (same class private methods are available) and all meta programming changes are visible
  2. Sealed meta class
    Like the default meta class, but meta programming changes from outside the class are not visible. This can be especially useful to library writers.
  3. Test meta class (un)selaed
    Like the two before but with the addition that private members will always be available.

Getting a Meta Class

Because of the concept of context a class does not have one direct meta class that can be generated without its context. The call site defines the place of the context. How the context itself is defined is a TODO. As an implementation strategy it is possible to for example use ClassValue to store a table with the context being a key. The key would probably have to be available as static information, or as easily computable information. Since the resulting meta class could be stored later in the call site object context changes are to be avoided, since it implies the invalidation of the call sites using that context.

Mailing-list discussions

JIRA issues


Work Items

This part is to guide the implementors with the course of action and planning of the subtasks.

  • make indy the only compilation target in the build
  • move all non-indy bytecode interfacing code to a module, which may be removed later. This includes ScriptBytecodeAdpater as well as all the custom call site caching classes
  • make a new module for MOP2
  • turn meta class into an immutable
  • implement meta class views

Breaking changes trace

groovy.lang.MetaObjectProtocol (currently in groovy.mop.MetaObjectProtocol):

  • getProperties() renamed to getMetaProperties()
  • getMethods() renamed to getMetaMethods()
  • respondsTo(Object, String, Object[]) changed to  respondsTo(String, Object... argTypes)
  • respondsTo(Object, String) replaced by getMetaMethods(String, Class...) with the class argument being null
  • hasProperty(Object,String) replaced by getMetaProperty(String) being null or not
  • getStaticMetaMethod(String, Object[]) replaced by respondsTo(String, Object...) and inspecting the list for static methods
  • getMetaMethod(name, Object[]) replaced by respondsTo(String, Object...) in case the arguments are no classes and getMetaMethods(String,Class...) in case of the arguments being classes
  • invokeConstructor(Object[]) removed
  • invokeMethod(Object, String, Object[]) removed
  • invokeMethod(Object, String, Object) removed
  • invokeStaticMethod(Object, String, Object[]) removed
  • getProperty(Object, String) replaced by MetaProperty#invoke
  • setProperty(Object, String, Object) replaced by MetaProperty#invoke
  • getAttribute(Object, String) replaced by MetaProperty#getField#invoke
  • setAttribute(Object, String, Object) replaced by MetaProperty#getField#invoke

  • getMetaProperty(String), getTheClass() unchanged

The work branch for this:

  • No labels