Class OwnedMetaClass

java.lang.Object
groovy.lang.DelegatingMetaClass
org.codehaus.groovy.runtime.metaclass.OwnedMetaClass
All Implemented Interfaces:
GroovyObject, MetaClass, MetaObjectProtocol, MutableMetaClass
Direct Known Subclasses:
MixedInMetaClass

public abstract class OwnedMetaClass extends DelegatingMetaClass
An abstract delegating MetaClass that delegates calls to an owner object's MetaClass. Subclasses must provide an owner object via the getOwner() method. This is used in Groovy for features like mixins that require delegation to an owner's metaclass for method and property access.

This class is for internal use by the Groovy runtime.

  • Constructor Details

    • OwnedMetaClass

      public OwnedMetaClass(MetaClass delegate)
      Constructs a new OwnedMetaClass.
      Parameters:
      delegate - the MetaClass to delegate to
  • Method Details

    • getAttribute

      public Object getAttribute(Object object, String attribute)
      Retrieves the value of an attribute on the owner's MetaClass.
      Specified by:
      getAttribute in interface MetaObjectProtocol
      Overrides:
      getAttribute in class DelegatingMetaClass
      Parameters:
      object - the object (ignored; the owner is used instead)
      attribute - the attribute name
      Returns:
      the attribute value
      See Also:
    • getOwner

      protected abstract Object getOwner()
      Returns the owner object whose MetaClass should be used for delegated operations.
      Returns:
      the owner object
    • getClassNode

      public ClassNode getClassNode()
      Returns the AST ClassNode for the owner's class.
      Specified by:
      getClassNode in interface MetaClass
      Overrides:
      getClassNode in class DelegatingMetaClass
      Returns:
      the ClassNode of the owner's metaclass
    • getMetaMethods

      public List<MetaMethod> getMetaMethods()
      Returns all metamethods from the owner's MetaClass.
      Specified by:
      getMetaMethods in interface MetaClass
      Overrides:
      getMetaMethods in class DelegatingMetaClass
      Returns:
      a list of all metamethods
    • getMethods

      public List<MetaMethod> getMethods()
      Returns all methods from the owner's MetaClass.
      Specified by:
      getMethods in interface MetaClass
      Specified by:
      getMethods in interface MetaObjectProtocol
      Overrides:
      getMethods in class DelegatingMetaClass
      Returns:
      a list of all methods
      See Also:
    • respondsTo

      public List<MetaMethod> respondsTo(Object obj, String name, Object[] argTypes)
      Returns all metamethods that respond to the given method name and argument types.
      Specified by:
      respondsTo in interface MetaObjectProtocol
      Overrides:
      respondsTo in class DelegatingMetaClass
      Parameters:
      obj - the object (ignored; the owner is used instead)
      name - the method name
      argTypes - the argument types
      Returns:
      a list of matching metamethods
    • respondsTo

      public List<MetaMethod> respondsTo(Object obj, String name)
      Returns all metamethods that respond to the given method name.
      Specified by:
      respondsTo in interface MetaObjectProtocol
      Overrides:
      respondsTo in class DelegatingMetaClass
      Parameters:
      obj - the object (ignored; the owner is used instead)
      name - the method name
      Returns:
      a list of matching metamethods
    • hasProperty

      public MetaProperty hasProperty(Object obj, String name)
      Retrieves the MetaProperty with the given name from the owner's MetaClass.
      Specified by:
      hasProperty in interface MetaObjectProtocol
      Overrides:
      hasProperty in class DelegatingMetaClass
      Parameters:
      obj - the object (ignored; the owner is used instead)
      name - the property name
      Returns:
      the MetaProperty or null if not found
    • getProperties

      public List<MetaProperty> getProperties()
      Returns all properties from the owner's MetaClass.
      Specified by:
      getProperties in interface MetaClass
      Specified by:
      getProperties in interface MetaObjectProtocol
      Overrides:
      getProperties in class DelegatingMetaClass
      Returns:
      a list of all properties
      See Also:
    • getProperty

      public Object getProperty(Object object, String property)
      Gets a property value from the owner's MetaClass.
      Specified by:
      getProperty in interface MetaObjectProtocol
      Overrides:
      getProperty in class DelegatingMetaClass
      Parameters:
      object - the object (ignored; the owner is used instead)
      property - the property name
      Returns:
      the property value
      See Also:
    • invokeConstructor

      public Object invokeConstructor(Object[] arguments)
      Invokes a constructor on the owner's MetaClass.
      Specified by:
      invokeConstructor in interface MetaObjectProtocol
      Overrides:
      invokeConstructor in class DelegatingMetaClass
      Parameters:
      arguments - the constructor arguments
      Returns:
      the newly constructed instance
    • invokeMethod

      public Object invokeMethod(Object object, String methodName, Object arguments)
      Invokes a method on the owner's MetaClass with the specified arguments.
      Specified by:
      invokeMethod in interface MetaObjectProtocol
      Overrides:
      invokeMethod in class DelegatingMetaClass
      Parameters:
      object - the object (ignored; the owner is used instead)
      methodName - the method name
      arguments - the method arguments (as a single object or varargs)
      Returns:
      the method result
      See Also:
    • invokeMethod

      public Object invokeMethod(Object object, String methodName, Object[] arguments)
      Invokes a method on the owner's MetaClass with the specified array of arguments.
      Specified by:
      invokeMethod in interface MetaObjectProtocol
      Overrides:
      invokeMethod in class DelegatingMetaClass
      Parameters:
      object - the object (ignored; the owner is used instead)
      methodName - the method name
      arguments - the method arguments as an array
      Returns:
      the method result
      See Also:
    • getOwnerMetaClass

      protected abstract MetaClass getOwnerMetaClass(Object owner)
      Returns the MetaClass of the owner object.
      Parameters:
      owner - the owner object
      Returns:
      the owner's MetaClass
    • invokeStaticMethod

      public Object invokeStaticMethod(Object object, String methodName, Object[] arguments)
      Invokes a static method on the owner's MetaClass.
      Specified by:
      invokeStaticMethod in interface MetaObjectProtocol
      Overrides:
      invokeStaticMethod in class DelegatingMetaClass
      Parameters:
      object - the class object
      methodName - the method name
      arguments - the method arguments
      Returns:
      the method result
      See Also:
    • setAttribute

      public void setAttribute(Object object, String attribute, Object newValue)
      Sets the value of an attribute on the owner's MetaClass.
      Specified by:
      setAttribute in interface MetaObjectProtocol
      Overrides:
      setAttribute in class DelegatingMetaClass
      Parameters:
      object - the object (ignored; the owner is used instead)
      attribute - the attribute name
      newValue - the new attribute value
      See Also:
    • setProperty

      public void setProperty(Object object, String property, Object newValue)
      Sets a property value on the owner's MetaClass.
      Specified by:
      setProperty in interface MetaObjectProtocol
      Overrides:
      setProperty in class DelegatingMetaClass
      Parameters:
      object - the object (ignored; the owner is used instead)
      property - the property name
      newValue - the new property value
      See Also:
    • equals

      public boolean equals(Object obj)
      Compares this OwnedMetaClass with another object using the delegate's equality.
      Overrides:
      equals in class DelegatingMetaClass
      Parameters:
      obj - the object to compare with
      Returns:
      true if equal to the delegate
    • hashCode

      public int hashCode()
      Returns the hash code of the delegate MetaClass.
      Overrides:
      hashCode in class DelegatingMetaClass
      Returns:
      the hash code
    • toString

      public String toString()
      Returns a string representation of this OwnedMetaClass.
      Overrides:
      toString in class DelegatingMetaClass
      Returns:
      a string describing this metaclass and its delegate
    • getAttribute

      public Object getAttribute(Class sender, Object receiver, String messageName, boolean useSuper)
      Gets an attribute from the owner's MetaClass with caller context.
      Specified by:
      getAttribute in interface MetaClass
      Overrides:
      getAttribute in class DelegatingMetaClass
      Parameters:
      sender - the class making the request
      receiver - the receiver object (ignored; the owner is used instead)
      messageName - the attribute name
      useSuper - whether to use the super method
      Returns:
      the attribute value
    • getProperty

      public Object getProperty(Class sender, Object receiver, String messageName, boolean useSuper, boolean fromInsideClass)
      Gets a property from the owner's MetaClass with caller context.
      Specified by:
      getProperty in interface MetaClass
      Overrides:
      getProperty in class DelegatingMetaClass
      Parameters:
      sender - the class making the request
      receiver - the receiver object (ignored; the owner is used instead)
      messageName - the property name
      useSuper - whether to use the super method
      fromInsideClass - whether this is called from inside the class
      Returns:
      the property value
    • getMetaProperty

      public MetaProperty getMetaProperty(String name)
      Gets the MetaProperty with the given name from the owner's MetaClass.
      Specified by:
      getMetaProperty in interface MetaObjectProtocol
      Overrides:
      getMetaProperty in class DelegatingMetaClass
      Parameters:
      name - the property name
      Returns:
      the MetaProperty or null if not found
    • getStaticMetaMethod

      public MetaMethod getStaticMetaMethod(String name, Object[] args)
      Gets a static metamethod from the owner's MetaClass by name and argument objects.
      Specified by:
      getStaticMetaMethod in interface MetaObjectProtocol
      Overrides:
      getStaticMetaMethod in class DelegatingMetaClass
      Parameters:
      name - the method name
      args - the argument objects
      Returns:
      the MetaMethod or null if not found
    • getStaticMetaMethod

      public MetaMethod getStaticMetaMethod(String name, Class[] argTypes)
      Gets a static metamethod from the owner's MetaClass by name and argument types.
      Overrides:
      getStaticMetaMethod in class DelegatingMetaClass
      Parameters:
      name - the method name
      argTypes - the argument types
      Returns:
      the MetaMethod or null if not found
    • getMetaMethod

      public MetaMethod getMetaMethod(String name, Object[] args)
      Gets a metamethod from the owner's MetaClass by name and argument objects.
      Specified by:
      getMetaMethod in interface MetaObjectProtocol
      Overrides:
      getMetaMethod in class DelegatingMetaClass
      Parameters:
      name - the method name
      args - the argument objects
      Returns:
      the MetaMethod or null if not found
    • getMetaMethod

      public MetaMethod getMetaMethod(String name, Class[] argTypes)
      Gets a metamethod from the owner's MetaClass by name and argument types.
      Parameters:
      name - the method name
      argTypes - the argument types
      Returns:
      the MetaMethod or null if not found
    • getTheClass

      public Class getTheClass()
      Returns the class that this MetaClass represents.
      Specified by:
      getTheClass in interface MetaObjectProtocol
      Overrides:
      getTheClass in class DelegatingMetaClass
      Returns:
      the owner's class
    • invokeMethod

      public Object invokeMethod(Class sender, Object receiver, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass)
      Invokes a method on the owner's MetaClass with caller context.
      Specified by:
      invokeMethod in interface MetaClass
      Overrides:
      invokeMethod in class DelegatingMetaClass
      Parameters:
      sender - the class making the request
      receiver - the receiver object (ignored; the owner is used instead)
      methodName - the method name
      arguments - the method arguments
      isCallToSuper - whether this is a super method call
      fromInsideClass - whether this is called from inside the class
      Returns:
      the method result
    • invokeMissingMethod

      public Object invokeMissingMethod(Object instance, String methodName, Object[] arguments)
      Invokes a missing method on the owner's MetaClass.
      Specified by:
      invokeMissingMethod in interface MetaClass
      Overrides:
      invokeMissingMethod in class DelegatingMetaClass
      Parameters:
      instance - the instance (ignored; the owner is used instead)
      methodName - the method name that was missing
      arguments - the method arguments
      Returns:
      the method result
      See Also:
    • invokeMissingProperty

      public Object invokeMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter)
      Invokes a missing property on the owner's MetaClass.
      Specified by:
      invokeMissingProperty in interface MetaClass
      Overrides:
      invokeMissingProperty in class DelegatingMetaClass
      Parameters:
      instance - the instance (ignored; the owner is used instead)
      propertyName - the property name that was missing
      optionalValue - the optional value (for setters)
      isGetter - whether this is a getter operation
      Returns:
      the property value (or null for setters)
    • isGroovyObject

      public boolean isGroovyObject()
      Checks if the owner's class is a GroovyObject.
      Overrides:
      isGroovyObject in class DelegatingMetaClass
      Returns:
      true if the owner's class implements GroovyObject
    • setAttribute

      public void setAttribute(Class sender, Object receiver, String messageName, Object messageValue, boolean useSuper, boolean fromInsideClass)
      Sets an attribute on the owner's MetaClass with caller context.
      Specified by:
      setAttribute in interface MetaClass
      Overrides:
      setAttribute in class DelegatingMetaClass
      Parameters:
      sender - the class making the request
      receiver - the receiver object (ignored; the owner is used instead)
      messageName - the attribute name
      messageValue - the new attribute value
      useSuper - whether to use the super method
      fromInsideClass - whether this is called from inside the class
    • setProperty

      public void setProperty(Class sender, Object receiver, String messageName, Object messageValue, boolean useSuper, boolean fromInsideClass)
      Sets a property on the owner's MetaClass with caller context.
      Specified by:
      setProperty in interface MetaClass
      Overrides:
      setProperty in class DelegatingMetaClass
      Parameters:
      sender - the class making the request
      receiver - the receiver object (ignored; the owner is used instead)
      messageName - the property name
      messageValue - the new property value
      useSuper - whether to use the super method
      fromInsideClass - whether this is called from inside the class
    • selectConstructorAndTransformArguments

      public int selectConstructorAndTransformArguments(int numberOfConstructors, Object[] arguments)
      Selects a constructor and transforms arguments for the owner's metaclass.
      Specified by:
      selectConstructorAndTransformArguments in interface MetaClass
      Overrides:
      selectConstructorAndTransformArguments in class DelegatingMetaClass
      Parameters:
      numberOfConstructors - the number of constructors available
      arguments - the constructor arguments to transform
      Returns:
      the selected constructor index