Class HandleMetaClass

java.lang.Object
groovy.lang.DelegatingMetaClass
org.codehaus.groovy.runtime.HandleMetaClass
All Implemented Interfaces:
GroovyObject, MetaClass, MetaObjectProtocol, MutableMetaClass

public class HandleMetaClass extends DelegatingMetaClass
A delegating meta-class that can replace the meta-class of one instance on demand.
  • Constructor Details

    • HandleMetaClass

      public HandleMetaClass(MetaClass mc)
      Delegates to mc with no instance attached. replaceDelegate() then installs an ExpandoMetaClass on the class. Equivalent to HandleMetaClass(mc, null).
      Parameters:
      mc - the meta class to delegate to
    • HandleMetaClass

      public HandleMetaClass(MetaClass mc, Object obj)
      Delegates to mc and records whether obj still needs a per-instance meta class. A null obj leaves replacement to the class, as HandleMetaClass(MetaClass) does. A non-null obj is remembered when the meta class of obj's class is mc, or when mc is not an ExpandoMetaClass. Otherwise the instance is treated as already having its own meta class and replaceDelegate() leaves it alone.
      Parameters:
      mc - the meta class to delegate to
      obj - the instance whose meta class may be replaced, or null for the class
  • Method Details

    • initialize

      public void initialize()
      Description copied from class: DelegatingMetaClass
      Complete the initialisation process. After this method is called no methods should be added to the metaclass. Invocation of methods or access to fields/properties is forbidden unless this method is called. This method should contain any initialisation code, taking a longer time to complete. An example is the creation of the Reflector. It is suggested to synchronize this method.
      Specified by:
      initialize in interface MetaClass
      Overrides:
      initialize in class DelegatingMetaClass
    • replaceDelegate

      public GroovyObject replaceDelegate()
      Ensures the delegate is an ExpandoMetaClass when a replacement is still pending, then returns that delegate. With no instance, a delegate that is not already an expando meta class is replaced by new ExpandoMetaClass(theClass, true, true), initialized, and registered on the class; an existing expando delegate is registered again. With a pending instance, a new ExpandoMetaClass(theClass, false, true) is created, expando methods are copied when the previous delegate is an ExpandoMetaClass, and the new meta class is set on that instance. After an instance replacement, further calls do not replace again. The delegate is cast to GroovyObject.
      Returns:
      the delegate after any replacement
    • invokeMethod

      public Object invokeMethod(String name, Object args)
      Description copied from class: DelegatingMetaClass
      Invokes the given method.
      Specified by:
      invokeMethod in interface GroovyObject
      Overrides:
      invokeMethod in class DelegatingMetaClass
      Parameters:
      name - the name of the method to call
      args - the arguments to use for the method call
      Returns:
      the result of invoking the method
    • getProperty

      public Object getProperty(String property)
      Description copied from class: DelegatingMetaClass
      Retrieves a property value.
      Specified by:
      getProperty in interface GroovyObject
      Overrides:
      getProperty in class DelegatingMetaClass
      Parameters:
      property - the name of the property of interest
      Returns:
      the given property
    • setProperty

      public void setProperty(String property, Object newValue)
      Description copied from class: DelegatingMetaClass
      Sets the given property to the new value.
      Specified by:
      setProperty in interface GroovyObject
      Overrides:
      setProperty in class DelegatingMetaClass
      Parameters:
      property - the name of the property of interest
      newValue - the new value for the property
    • addNewInstanceMethod

      public void addNewInstanceMethod(Method method)
      Description copied from class: DelegatingMetaClass
      adds a new instance method to this MetaClass. Instance methods are able to overwrite the original methods of the class. Calling this method should not be done after initialise was called.
      Specified by:
      addNewInstanceMethod in interface MutableMetaClass
      Overrides:
      addNewInstanceMethod in class DelegatingMetaClass
      Parameters:
      method - the method to be added
    • addNewStaticMethod

      public void addNewStaticMethod(Method method)
      Description copied from class: DelegatingMetaClass
      adds a new static method to this MetaClass. This is only possible as long as initialise was not called.
      Specified by:
      addNewStaticMethod in interface MutableMetaClass
      Overrides:
      addNewStaticMethod in class DelegatingMetaClass
      Parameters:
      method - the method to be added
    • addMetaMethod

      public void addMetaMethod(MetaMethod metaMethod)
      Description copied from class: DelegatingMetaClass
      Adds a new MetaMethod to the MetaClass. This is only possible as long as initialise was not called.
      Specified by:
      addMetaMethod in interface MutableMetaClass
      Overrides:
      addMetaMethod in class DelegatingMetaClass
      Parameters:
      metaMethod - The MetaMethod to add
    • addMetaBeanProperty

      public void addMetaBeanProperty(MetaBeanProperty metaBeanProperty)
      Description copied from class: DelegatingMetaClass
      Adds a new MetaBeanProperty to the MetaClass. This is only possible as long as initialise was not called.
      Specified by:
      addMetaBeanProperty in interface MutableMetaClass
      Overrides:
      addMetaBeanProperty in class DelegatingMetaClass
      Parameters:
      metaBeanProperty - The MetaBeanProperty instance
    • equals

      public boolean equals(Object obj)
      Description copied from class: DelegatingMetaClass
      Overrides:
      equals in class DelegatingMetaClass