org.codehaus.groovy.grails.commons.metaclass
Class DynamicMethodsExpandoMetaClass
java.lang.Object
groovy.lang.MetaClassImpl
groovy.lang.ExpandoMetaClass
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 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 |
Fields inherited from class groovy.lang.ExpandoMetaClass |
STATIC_QUALIFIER |
Fields inherited from class groovy.lang.MetaClassImpl |
EMPTY_CLASS_ARRAY, isGroovyObject, isMap, LOG, METHOD_MISSING, PROPERTY_MISSING, registry, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, theCachedClass, theClass |
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 |
disableGlobally, enableGlobally, getExpandoMethods, getExpandoProperties, getJavaClass, getMetaClass, getMetaProperty, getMethods, getProperties, getProperty, getPropertyForSetter, getSuperClasses, hasMetaMethod, hasMetaProperty, initialize, invokeMethod, invokeMissingMethod, invokeMissingProperty, isInitialized, isModified, isSetter, performOperationOnMetaClass, refreshInheritedMethods, registerBeanProperty, registerInstanceMethod, registerStaticMethod, setInitialized, setMetaClass |
Methods inherited from class groovy.lang.MetaClassImpl |
addMetaBeanProperty, addMetaMethod, addNewInstanceMethod, addNewStaticMethod, checkInitalised, clearInvocationCaches, dropMethodCache, dropStaticMethodCache, getAttribute, getAttribute, getAttribute, getClassNode, getMetaMethod, getMetaMethods, getMethodWithCaching, getMethodWithoutCaching, getStaticMetaMethod, getTheClass, hasProperty, invokeConstructorAt, invokeMethod, invokeMethod, invokeStaticMissingProperty, isGroovyObject, pickMethod, respondsTo, respondsTo, retrieveConstructor, retrieveMethod, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setAttribute, setProperties, setProperty, toString |
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
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 objectmethodName
- The method namearguments
- 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-2006 The Grails project