Class ClosureMetaClass

  • All Implemented Interfaces:
    MetaClass, MetaObjectProtocol, MutableMetaClass

    public final class ClosureMetaClass
    extends MetaClassImpl
    A meta class for closures generated by the Groovy compiler. These classes have special characteristics this MetaClass uses. One of these is that a generated Closure has only additional doCall methods, all other methods are in the Closure class as well. To use this fact this MetaClass uses a MetaClass for Closure as static field And delegates calls to this MetaClass if needed. This allows a lean implementation for this MetaClass. Multiple generated closures will then use the same MetaClass for Closure. For static dispatching this class uses the MetaClass of Class, again all instances of this class will share that MetaClass. The Class MetaClass is initialized lazy, because most operations do not need this MetaClass.

    The Closure and Class MetaClasses are not replaceable.

    This MetaClass is for internal usage only!

    Since:
    1.5
    • Method Detail

      • resetCachedMetaClasses

        public static void resetCachedMetaClasses()
      • invokeMethod

        public Object invokeMethod​(Class sender,
                                   Object object,
                                   String methodName,
                                   Object[] originalArguments,
                                   boolean isCallToSuper,
                                   boolean fromInsideClass)
        Description copied from class: MetaClassImpl

        Invokes a method on the given receiver for the specified arguments. The sender is the class that invoked the method on the object. The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.

        The isCallToSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly to the super class if necessary

        Specified by:
        invokeMethod in interface MetaClass
        Overrides:
        invokeMethod in class MetaClassImpl
        Parameters:
        sender - The java.lang.Class instance that invoked the method
        object - The object which the method was invoked on
        methodName - The name of the method
        originalArguments - The arguments to the method
        isCallToSuper - Whether the method is a call to a super class method
        fromInsideClass - Whether the call was invoked from the inside or the outside of the class
        Returns:
        The return value of the method
        See Also:
        MetaClass.invokeMethod(Class, Object, String, Object[], boolean, boolean)
      • initialize

        public void initialize()
        Description copied from class: MetaClassImpl
        Complete the initialisation process. After this method is called no methods should be added to the meta class. 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 MetaClassImpl
      • pickMethod

        public MetaMethod pickMethod​(String name,
                                     Class[] argTypes)
        Description copied from class: MetaClassImpl
        Selects a method by name and argument classes. This method does not search for an exact match, it searches for a compatible method. For this the method selection mechanism is used as provided by the implementation of this MetaClass. pickMethod may or may not be used during the method selection process when invoking a method. There is no warranty for that.
        Specified by:
        pickMethod in interface MetaClass
        Overrides:
        pickMethod in class MetaClassImpl
        Parameters:
        name - the name of the method to pick
        argTypes - the method arguments
        Returns:
        a matching MetaMethod or null
      • retrieveStaticMethod

        public MetaMethod retrieveStaticMethod​(String methodName,
                                               Class[] arguments)
      • getProperty

        public Object getProperty​(Class sender,
                                  Object object,
                                  String name,
                                  boolean useSuper,
                                  boolean fromInsideClass)
        Description copied from interface: MetaClass

        Retrieves a property on the given receiver for the specified arguments. The sender is the class that is requesting the property from the object. The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.

        The isCallToSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly to the super class if necessary

        Specified by:
        getProperty in interface MetaClass
        Overrides:
        getProperty in class MetaClassImpl
        Parameters:
        sender - The java.lang.Class instance that requested the property
        object - The Object which the property is being retrieved from
        name - The name of the property
        useSuper - Whether the call is to a super class property
        fromInsideClass - ??
        Returns:
        the given property's value on the object
      • getAttribute

        public Object getAttribute​(Class sender,
                                   Object object,
                                   String attribute,
                                   boolean useSuper,
                                   boolean fromInsideClass)
        Description copied from class: MetaClassImpl
        Retrieves the value of an attribute (field). This method is to support the Groovy runtime and not for general client API usage.
        Overrides:
        getAttribute in class MetaClassImpl
        Parameters:
        sender - The class of the object that requested the attribute
        object - The instance the attribute is to be retrieved from
        attribute - The name of the attribute
        useSuper - Whether to look-up on the super class or not
        fromInsideClass - Whether the call was invoked from the inside or the outside of the class.
        Returns:
        The attribute value
      • setAttribute

        public void setAttribute​(Class sender,
                                 Object object,
                                 String attribute,
                                 Object newValue,
                                 boolean useSuper,
                                 boolean fromInsideClass)
        Description copied from class: MetaClassImpl

        Sets an attribute on the given receiver for the specified arguments. The sender is the class that is setting the attribute from the object. The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.

        The isCallToSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly to the super class if necessary

        Specified by:
        setAttribute in interface MetaClass
        Overrides:
        setAttribute in class MetaClassImpl
        Parameters:
        sender - The java.lang.Class instance that is mutating the property
        object - The Object which the property is being set on
        attribute - The name of the attribute,
        newValue - The new value of the attribute to set
        useSuper - Whether the call is to a super class property
        fromInsideClass - Whether the call was invoked from the inside or the outside of the class
      • invokeStaticMethod

        public Object invokeStaticMethod​(Object object,
                                         String methodName,
                                         Object[] arguments)
        Description copied from interface: MetaObjectProtocol
        Invokes a static method on the given Object with the given name and arguments.

        The Object can either be an instance of the class that this MetaObjectProtocol instance applies to or the java.lang.Class instance itself. If a method cannot be invoked a MissingMethodException is will be thrown

        Specified by:
        invokeStaticMethod in interface MetaObjectProtocol
        Overrides:
        invokeStaticMethod in class MetaClassImpl
        Parameters:
        object - An instance of the class returned by the getTheClass() method or the class itself
        methodName - The name of the method
        arguments - The arguments to the method
        Returns:
        The return value of the method which is null if the return type is void
        See Also:
        MissingMethodException
      • setProperty

        public void setProperty​(Class sender,
                                Object object,
                                String name,
                                Object newValue,
                                boolean useSuper,
                                boolean fromInsideClass)
        Description copied from class: MetaClassImpl

        Retrieves a property on the given receiver for the specified arguments. The sender is the class that is requesting the property from the object. The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.

        The useSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly to the super class if necessary

        Specified by:
        setProperty in interface MetaClass
        Overrides:
        setProperty in class MetaClassImpl
        Parameters:
        sender - The java.lang.Class instance that is mutating the property
        object - The Object which the property is being set on
        name - The name of the property
        newValue - The new value of the property to set
        useSuper - Whether the call is to a super class property
        fromInsideClass - Whether the call was invoked from the inside or the outside of the class.
      • getMethodWithoutCaching

        public MetaMethod getMethodWithoutCaching​(int index,
                                                  Class sender,
                                                  String methodName,
                                                  Class[] arguments,
                                                  boolean isCallToSuper)
      • setProperties

        public void setProperties​(Object bean,
                                  Map map)
        Description copied from class: MetaClassImpl
        Sets a number of bean properties from the given Map where the keys are the String names of properties and the values are the values of the properties to set
        Overrides:
        setProperties in class MetaClassImpl
      • addMetaMethod

        public void addMetaMethod​(MetaMethod method)
        Description copied from class: MetaClassImpl
        adds a MetaMethod to this class. WARNING: this method will not do the neccessary steps for multimethod logic and using this method doesn't mean, that a method added here is replacing another method from a parent class completely. These steps are usually done by initialize, which means if you need these steps, you have to add the method before running initialize the first time.
        Specified by:
        addMetaMethod in interface MutableMetaClass
        Overrides:
        addMetaMethod in class MetaClassImpl
        Parameters:
        method - the MetaMethod
        See Also:
        MetaClassImpl.initialize()
      • respondsTo

        public List respondsTo​(Object obj,
                               String name,
                               Object[] argTypes)
        Description copied from interface: MetaObjectProtocol

        Returns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name and arguments types.

        Note that this method's return value is based on realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing

        This method is "safe" in that it will always return a value and never throw an exception

        Specified by:
        respondsTo in interface MetaObjectProtocol
        Overrides:
        respondsTo in class MetaClassImpl
        Parameters:
        obj - The object to inspect
        name - The name of the method of interest
        argTypes - The argument types to match against
        Returns:
        A List of MetaMethods matching the argument types which will be empty if no matching methods exist
        See Also:
        MetaObjectProtocol.respondsTo(Object, String, Object[])
      • respondsTo

        public List respondsTo​(Object obj,
                               String name)
        Description copied from interface: MetaObjectProtocol

        Returns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name regardless of arguments. In other words this method will return for foo() and foo(String).

        Note that this method's return value is based on realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing

        This method is "safe" in that it will always return a value and never throw an exception

        Specified by:
        respondsTo in interface MetaObjectProtocol
        Overrides:
        respondsTo in class MetaClassImpl
        Parameters:
        obj - The object to inspect
        name - The name of the method of interest
        Returns:
        A List of MetaMethods which will be empty if no methods with the given name exist
        See Also:
        MetaObjectProtocol.respondsTo(Object, String)