Versions Compared


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

In MetaClass Redesign (by blackdrag) I tried to show some ideas of how such a redisgn might look like and what the advantages and disadvantages might be.


In this part I want to show a more detailed version of one idea. that is a implementation based MetaClass is in front and the user can add custom behaviors. This limits the basic MetaClass interface to mere adminisatrative methods

Code Block
package groovy.lang;

public interface MetaClass {
  // MOP
  void addMOP(MetaClassObjectProtocoll mop);
  MetaClassObjectProtokoll removeMOP(MetaClassObjectProtokoll mop);

  // add methods and  properties
  void addMethod(MetaMethod mm);
  void addProperty(MetaProperty mp);
  void addConstructor(MetaMethod mc);

  // listener for MetaClass changes
  void addMetClassListener(MetaClassListener mcl);
  void removeMetaClassListener(MetaClassListener mcl);
  MetaClassListener[] getMetClassListeners();

So the central point is addMOP, which allows to customize the method invocation for example. Besides this there are some methods for mutating the MetaClass (adding properties and such), a standard listener interface that allows to react to additions of properties for example. The implementation of this is not relevant for the user as it is thought as specification. The implementation depends on the Grovy runtime and might differ from other runtimes if there are ever such runtimes.

Usage of groovy.lang.MetaClass

If the user wants to influence themethod invocation process for example, then he has to implement the MetaClassObjectProtokoll interface, which I will show later. In this section I want to show how to create a per instance MetaClass and how to use it.

 changing a MetaClass in the registry

Code Block
// new code
metaClass = registry.getMetaClass(theClass)metaClass.addBehavior(myMOP)
// or in one line

So the one-line codebecomes a more complex two-line code, unless we decide to change MetaClassRegistry to make that by itself.

 creating and changing a per instance MetaClass

 The old code was like this:

Code Block
groovyObject.metaClass = myMetaClass

The new code will look like this:

Code Block

So the change here is minimal. The code to create a per instance MetaClass is hidden by the implementation and might differ. This of course means that the class GroovyObjectSupport, which is part of groovy.lang has implementation knowledge inside. So a implementator must change this class to his version of the runtime.

 Using the Listener to MetaClass changes

(add interface)