org.codehaus.groovy.grails.commons.metaclass
Class DynamicMethodsExpandoMetaClass

java.lang.Object
  extended by groovy.lang.MetaClassImpl
      extended by groovy.lang.ExpandoMetaClass
          extended by org.codehaus.groovy.grails.commons.metaclass.DynamicMethodsExpandoMetaClass
All Implemented Interfaces:
groovy.lang.GroovyObject, groovy.lang.MetaClass, groovy.lang.MetaObjectProtocol, groovy.lang.MutableMetaClass

public class DynamicMethodsExpandoMetaClass
extends groovy.lang.ExpandoMetaClass

This MetaClass extends ExpandoMetaClass and adds the ability to use regex to specified method matches that then get passed to the invocable closure. Example: metaClass./^findBy(\w+)$/ = { matcher, args -> } The first argument to the closure is the Regex Matcher. The second is the arguments to the method. This MetaClass allows you to implement thigns like dynamic finders in a trivial manner. The regular expression MUST start with a ^ and end with a $ otherwise it won't be regarded as a valid regex expression and an error will be thrown. Otherwise the mechanism is similar to that provided by ExpandoMetaClass WARNING: Unlike ExpandoMetaClass this MetaClass uses method proxying, hence there is a an overhead attached to its use. This makes it less suitable for use on commonly used objects like java.lang.Object or java.lang.String if performance is important to your application consider other options like the regular ExpandoMetaClass If usage can be isolated to a small set of use cases (such as dynamic finders in Grails) then there is no problem as proxying is not occuring for every method call WARNING: This MetaClass does not support inheritance heirarchies. In other words a child class will not be able to invoke a dynamically added method that exists on a super class

Since:
0.5

Created: Feb 2, 2007 Time: 6:31:31 PM

Author:
Graeme Rocher

Nested Class Summary
(package private)  class DynamicMethodsExpandoMetaClass.DynamicExpandoMetaProperty
          Wraps an existing ExpandoMetaBeanProperty and interceptors methods registration to check if the specified method addition is a regex method
 
Nested classes/interfaces inherited from class groovy.lang.ExpandoMetaClass
groovy.lang.ExpandoMetaClass.ExpandoMetaConstructor, groovy.lang.ExpandoMetaClass.ExpandoMetaProperty
 
Nested classes/interfaces inherited from class groovy.lang.MetaClassImpl
groovy.lang.MetaClassImpl.Index
 
Field Summary
 
Fields inherited from class groovy.lang.ExpandoMetaClass
CONSTRUCTOR, inRegistry, STATIC_QUALIFIER
 
Fields inherited from class groovy.lang.MetaClassImpl
getPropertyMethod, INVOKE_METHOD_METHOD, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, METHOD_MISSING, PROPERTY_MISSING, registry, setPropertyMethod, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, theCachedClass, theClass
 
Constructor Summary
DynamicMethodsExpandoMetaClass(Class aClass)
          Constructs a new DynamicMethodsExpandoMetaClass given the current class.
DynamicMethodsExpandoMetaClass(Class aClass, boolean inReg)
          Constructs a new DynamicMethodsExpandoMetaClass given the current class and places it in the MetaClassRegistry
 
Method Summary
 Object getProperty(Class aClass, Object object, String property, boolean b, boolean b1)
           
 Object getProperty(String name)
           
 Object invokeConstructor(Object[] arg0)
           
 Object invokeMethod(Class aClass, Object target, String methodName, Object[] arguments, boolean b, boolean b1)
           
 Object invokeStaticMethod(Object target, String methodName, Object[] arguments)
          Either invokes a intercepted dyanmic static method or the adapted original MetaClass
 void setProperty(Class aClass, Object object, String property, Object newValue, boolean b, boolean b1)
           
 void setProperty(String name, Object value)
           
 
Methods inherited from class groovy.lang.ExpandoMetaClass
addMixinClass, castToMixedType, createConstructorSite, createPogoCallCurrentSite, createPogoCallSite, createPojoCallSite, createStaticSite, define, disableGlobally, enableGlobally, findMixinMethod, getExpandoMethods, getExpandoProperties, getExpandoSubclassMethods, getJavaClass, getMetaClass, getMetaProperty, getMethods, getProperties, getProperty, getPropertyForSetter, getSubclassMetaMethods, hasMetaMethod, hasMetaProperty, initialize, invokeMethod, isInitialized, isModified, isSetter, isValidExpandoProperty, onGetPropertyFoundInHierarchy, onInvokeMethodFoundInHierarchy, onSetPropertyFoundInHierarchy, onSuperMethodFoundInHierarchy, onSuperPropertyFoundInHierarchy, performOperationOnMetaClass, refreshInheritedMethods, registerBeanProperty, registerInstanceMethod, registerInstanceMethod, registerStaticMethod, registerStaticMethod, registerSubclassInstanceMethod, registerSubclassInstanceMethod, setInitialized, setMetaClass
 
Methods inherited from class groovy.lang.MetaClassImpl
addMetaBeanProperty, addMetaMethod, addMetaMethodToIndex, addNewInstanceMethod, addNewStaticMethod, checkIfGroovyObjectMethod, checkInitalised, chooseMethod, clearInvocationCaches, createPogoCallCurrentSite, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMetaMethods, getMethodWithCaching, getMethodWithoutCaching, getStaticMetaMethod, getSuperClasses, getTheCachedClass, getTheClass, getVersion, hasProperty, incVersion, invokeConstructorAt, invokeMethod, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMissingProperty, isGroovyObject, onMixinMethodFound, pickMethod, respondsTo, respondsTo, retrieveConstructor, retrieveMethod, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setAttribute, setProperties, setProperty, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DynamicMethodsExpandoMetaClass

public DynamicMethodsExpandoMetaClass(Class aClass)
                               throws IntrospectionException
Constructs a new DynamicMethodsExpandoMetaClass given the current class. Note that this constructor will NOT place this MetaClass in the MetaClassRegistry. It is up to you to either place it in the MetaClassRegistry or call setMetaClass on GroovyObject

Parameters:
aClass - The class to create the MetaClass for
Throws:
IntrospectionException - Thrown if an error occurs introspecting the class

DynamicMethodsExpandoMetaClass

public DynamicMethodsExpandoMetaClass(Class aClass,
                                      boolean inReg)
                               throws IntrospectionException
Constructs a new DynamicMethodsExpandoMetaClass given the current class and places it in the MetaClassRegistry

Parameters:
aClass - The class to create the MetaClass for
Throws:
IntrospectionException - Thrown if an error occurs introspecting the class
Method Detail

invokeStaticMethod

public Object invokeStaticMethod(Object target,
                                 String methodName,
                                 Object[] arguments)
Either invokes a intercepted dyanmic static method or the adapted original MetaClass

Specified by:
invokeStaticMethod in interface groovy.lang.MetaObjectProtocol
Overrides:
invokeStaticMethod in class groovy.lang.ExpandoMetaClass
Parameters:
target - The target object
methodName - The method name
arguments - The arguments to the method
Returns:
The return value

setProperty

public void setProperty(Class aClass,
                        Object object,
                        String property,
                        Object newValue,
                        boolean b,
                        boolean b1)
Specified by:
setProperty in interface groovy.lang.MetaClass
Overrides:
setProperty in class groovy.lang.ExpandoMetaClass

getProperty

public Object getProperty(Class aClass,
                          Object object,
                          String property,
                          boolean b,
                          boolean b1)
Specified by:
getProperty in interface groovy.lang.MetaClass
Overrides:
getProperty in class groovy.lang.ExpandoMetaClass

invokeConstructor

public Object invokeConstructor(Object[] arg0)
Specified by:
invokeConstructor in interface groovy.lang.MetaObjectProtocol
Overrides:
invokeConstructor in class groovy.lang.ExpandoMetaClass

invokeMethod

public Object invokeMethod(Class aClass,
                           Object target,
                           String methodName,
                           Object[] arguments,
                           boolean b,
                           boolean b1)
Specified by:
invokeMethod in interface groovy.lang.MetaClass
Overrides:
invokeMethod in class groovy.lang.ExpandoMetaClass

getProperty

public Object getProperty(String name)
Specified by:
getProperty in interface groovy.lang.GroovyObject
Overrides:
getProperty in class groovy.lang.ExpandoMetaClass

setProperty

public void setProperty(String name,
                        Object value)
Specified by:
setProperty in interface groovy.lang.GroovyObject
Overrides:
setProperty in class groovy.lang.ExpandoMetaClass


Copyright (c) 2005-2009 The Grails project