Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Where it all starts

Say you have a Groovy code like

...

Code Block
...
Object aobj[] = { "hi" };
ScriptBytecodeAdapter.invokeMethod(this, "println", ((Object) (aobj)));
...

Invokation scheme

There is a delegation scheme like

  • ScriptBytecodeAdapter.invokeMethod(...) (static method)
    • InvokerHelper.invokeMethod(...) (static method)
      • Invoker.invokeMethod(...) (instance method called on InvokerHelper's single instance)

MetaClass and MetaClassRegistry

Invoker calls invokeMethod(...) on the MetaClass of our class (with exceptions,
see below). It finds this MetaClass by looking it up in the MetaClassRegistry.
The Invoker holds a single instance of this registry.

Note

When working with the MetaClassRegistry, InvokerHelper.getInstance().getMetaRegistry() is the only instance, you should ever use.

Exceptions (when MetaClass.invokeMethod(...) is not used):

  • for Closures, Closure.invoke(...) is used
  • for GroovyObjects obj of type GroovyInterceptable, obj.invokeMethod(methodName,asArray(arguments)) is called
  • for any other GroovyObject obj when method invokation through its MetaClass fails, obj.invokeMethod(methodName,asArray(arguments)) is called

MetaClass.invokeMethod(...) finally cares for the invokation, either by reflection or by (question).dynamic bytecode generation.
Dynamic bytecode generation is supposed to be faster. For a class MyClass it generates
gjdk.groovy.lang.MyClass_GroovyReflector with an invoke method.
Does MyClass_GroovyReflector contain methods according to MyClass.groovy that can be called directly (question)

The cool thing about MetaClass is that you can dynamically add or remove methods to it.
One can even replace the whole MetaClass in the MetaClassRegistry. See ProxyMetaClass
for an example.

...