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 2 Next »

Say you have a Groovy code like

As explained in Groovy Backstage, there is bytecode generated to achieve the
desired behaviour of printing to stdout.

The easiest way of looking at the generated bytecode is to groovyc your Groovy
source to a class file and process it with a Java Decompiler (e.g. JAD).
see also: From source code to bytecode

The resulting code looks as follows (only the relevant snippet):

There is a delegation scheme like

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

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.

Exceptions (MetaClass.invokeMethod(...) 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).

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.

back to Groovy Backstage

  • No labels