|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgroovy.lang.MetaClassImpl
groovy.lang.ExpandoMetaClass
public class ExpandoMetaClass
A MetaClass that implements GroovyObject and behaves like an Expando, allowing the addition of new methods on the fly
By default methods are only allowed to be added before initialize() is called. In other words you create a new
ExpandoMetaClass, add some methods and then call initialize(). If you attempt to add new methods after initialize()
has been called an error will be thrown.
This is to ensure that the MetaClass can operate appropriately in multi threaded environments as it forces you
to do all method additions at the beginning, before using the MetaClass.
If you need more fine grained control of how a method is matched you can use DynamicMethodsMetaClass
WARNING: This MetaClass uses a thread-bound ThreadLocal instance to store and retrieve properties.
In addition properties stored use soft references so they are both bound by the life of the Thread and by the soft
references. The implication here is you should NEVER use dynamic properties if you want their values to stick around
for long periods because as soon as the JVM is running low on memory or the thread dies they will be garbage collected.
// defines or replaces instance method:
metaClass.myMethod = { args -> }
// defines a new instance method
metaClass.myMethod << { args -> }
// creates multiple overloaded methods of the same name
metaClass.myMethod << { String s -> } << { Integer i -> }
// defines or replaces a static method with the 'static' qualifier
metaClass.'static'.myMethod = { args -> }
// defines a new static method with the 'static' qualifier
metaClass.'static'.myMethod << { args -> }
// defines a new constructor
metaClass.constructor << { String arg -> }
// defines or replaces a constructor
metaClass.constructor = { String arg -> }
// defines a new property with an initial value of "blah"
metaClass.myProperty = "blah"
Nested Class Summary | |
---|---|
private static interface |
ExpandoMetaClass.Callable
For simulating closures in Java |
protected class |
ExpandoMetaClass.ExpandoMetaConstructor
Handles the ability to use the left shift operator to append new constructors |
protected class |
ExpandoMetaClass.ExpandoMetaProperty
Instances of this class are returned when using the << left shift operator. |
Nested classes/interfaces inherited from class groovy.lang.MetaClassImpl |
---|
MetaClassImpl.Index, MetaClassImpl.MethodIndex |
Field Summary | |
---|---|
private boolean |
allowChangesAfterInit
|
private java.util.Map |
beanPropertyCache
|
private static java.lang.String |
CLASS
|
private static java.lang.String |
CLASS_PROPERTY
|
private static java.lang.String |
CONSTRUCTOR
|
private java.util.Map |
expandoMethods
|
private java.util.Map |
expandoProperties
|
private static java.lang.String |
GET_PROPERTY_METHOD
|
private ClosureMetaMethod |
getPropertyMethod
|
private static java.lang.String |
GROOVY_CONSTRUCTOR
|
private java.util.Set |
inheritedMetaMethods
|
private boolean |
initCalled
|
private boolean |
initialized
|
private boolean |
inRegistry
|
private static java.lang.String |
INVOKE_METHOD_METHOD
|
private ClosureMetaMethod |
invokeMethodMethod
|
private ClosureStaticMetaMethod |
invokeStaticMethodMethod
|
private static java.lang.String |
META_CLASS
|
private static java.lang.String |
META_CLASS_PROPERTY
|
private static java.lang.String |
META_METHODS
|
private static java.lang.String |
METHODS
|
private boolean |
modified
|
private MetaClass |
myMetaClass
|
private static java.lang.String |
PROPERTIES
|
private static java.lang.String |
SET_PROPERTY_METHOD
|
private ClosureMetaMethod |
setPropertyMethod
|
static java.lang.String |
STATIC_QUALIFIER
|
private java.util.Map |
staticBeanPropertyCache
|
private static java.lang.Class[] |
ZERO_ARGUMENTS
|
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 |
Constructor Summary | |
---|---|
ExpandoMetaClass(java.lang.Class theClass)
Constructs a new ExpandoMetaClass instance for the given class |
|
ExpandoMetaClass(java.lang.Class theClass,
boolean register)
Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass in the MetaClassRegistry automatically |
|
ExpandoMetaClass(java.lang.Class theClass,
boolean register,
boolean allowChangesAfterInit)
Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass in the MetaClassRegistry automatically |
Method Summary | |
---|---|
private void |
addSuperMethodIfNotOverriden(MetaMethod metaMethodFromSuper)
|
private void |
checkIfGroovyObjectMethod(ClosureMetaMethod metaMethod,
java.lang.String methodName)
Checks if the metaMethod is a method from the GroovyObject interface such as setProperty, getProperty and invokeMethod |
private java.lang.String |
convertPropertyName(java.lang.String prop)
|
static void |
disableGlobally()
Call to disable the global use of ExpandoMetaClass |
static void |
enableGlobally()
Call to enable global use of global use of ExpandoMetaClass within the registry. |
private MetaMethod |
findMethodInClassHeirarchy(java.lang.String methodName,
java.lang.Object[] arguments,
java.lang.Class theClass)
|
private MetaBeanProperty |
findPropertyInClassHierarchy(java.lang.String propertyName,
CachedClass theClass)
|
java.util.List |
getExpandoMethods()
Returns a list of expando MetaMethod instances added to this ExpandoMetaClass |
java.util.Collection |
getExpandoProperties()
Returns a list of MetaBeanProperty instances added to this ExpandoMetaClass |
java.lang.Class |
getJavaClass()
|
MetaClass |
getMetaClass()
Returns the metaclass for a given class. |
private MetaMethod |
getMetaMethodFromMutableMetaClass(java.lang.String methodName,
java.lang.Object[] arguments,
MetaClass metaClass)
|
MetaProperty |
getMetaProperty(java.lang.String name)
Looks up an existing MetaProperty by name |
private MetaBeanProperty |
getMetaPropertyFromMutableMetaClass(java.lang.String propertyName,
MetaClass metaClass)
|
java.util.List |
getMethods()
Overrides the behaviour of parent getMethods() method to make MetaClass aware of added Expando methods |
java.util.List |
getProperties()
Get all the properties defined for this type |
java.lang.Object |
getProperty(java.lang.Class sender,
java.lang.Object object,
java.lang.String name,
boolean useSuper,
boolean fromInsideClass)
Overrides default implementation just in case getProperty method has been overriden by ExpandoMetaClass |
java.lang.Object |
getProperty(java.lang.Object object,
java.lang.String name)
Overrides default implementation just in case getProperty method has been overriden by ExpandoMetaClass |
java.lang.Object |
getProperty(java.lang.String property)
Retrieves a property value. |
private java.lang.String |
getPropertyForGetter(java.lang.String getterName)
Returns a property name equivalent for the given getter name or null if it is not a getter |
java.lang.String |
getPropertyForSetter(java.lang.String setterName)
Returns a property name equivalent for the given setter name or null if it is not a getter |
protected java.util.LinkedList |
getSuperClasses()
Retrieves a list of super classes. |
boolean |
hasMetaMethod(java.lang.String name,
java.lang.Class[] args)
Checks whether a MetaMethod for the given name and arguments exists |
boolean |
hasMetaProperty(java.lang.String name)
Returns true if the MetaClass has the given property |
private boolean |
hasOverrideGetProperty(java.lang.String name)
|
void |
initialize()
complete the initlialisation process. |
java.lang.Object |
invokeConstructor(java.lang.Object[] arguments)
Invokes a constructor for the given arguments. |
java.lang.Object |
invokeMethod(java.lang.Class sender,
java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] originalArguments,
boolean isCallToSuper,
boolean fromInsideClass)
Overrides default implementation just in case invokeMethod has been overriden by ExpandoMetaClass |
java.lang.Object |
invokeMethod(java.lang.String name,
java.lang.Object args)
Invokes the given method. |
java.lang.Object |
invokeMissingMethod(java.lang.Object instance,
java.lang.String methodName,
java.lang.Object[] arguments)
Overrides the default missing method behaviour and adds the capability to look up a method from super class |
java.lang.Object |
invokeMissingProperty(java.lang.Object instance,
java.lang.String propertyName,
java.lang.Object optionalValue,
boolean isGetter)
Overrides the default missing method behaviour and adds the capability to look up a method from the super class in the case where it has been overriden |
java.lang.Object |
invokeStaticMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] arguments)
Overrides default implementation just in case a static invoke method has been set on ExpandoMetaClass |
private boolean |
isGetPropertyMethod(java.lang.String methodName)
|
private boolean |
isGetter(java.lang.String name,
CachedClass[] args)
Returns true if the name of the method specified and the number of arguments make it a javabean property |
protected boolean |
isInitialized()
|
private boolean |
isInvokeMethod(java.lang.String methodName,
ClosureMetaMethod metaMethod)
|
boolean |
isModified()
Return whether the MetaClass has been modified or not |
private boolean |
isSetPropertyMethod(java.lang.String methodName,
ClosureMetaMethod metaMethod)
|
boolean |
isSetter(java.lang.String name,
CachedClass[] args)
|
private boolean |
isValidExpandoProperty(java.lang.String property)
|
protected void |
performOperationOnMetaClass(ExpandoMetaClass.Callable c)
|
private void |
performRegistryCallbacks()
|
void |
refreshInheritedMethods(java.util.Set modifiedSuperExpandos)
Called from ExpandoMetaClassCreationHandle in the registry if it exists to setup inheritance handling |
protected void |
registerBeanProperty(java.lang.String property,
java.lang.Object newValue)
Registers a new bean property |
private void |
registerBeanPropertyForMethod(MetaMethod metaMethod,
java.lang.String propertyName,
boolean getter,
boolean isStatic)
|
protected void |
registerInstanceMethod(java.lang.String methodName,
Closure callable)
Registers a new instance method for the given method name and closure on this MetaClass |
protected void |
registerStaticMethod(java.lang.String name,
Closure callable)
Registers a new static method for the given method name and closure on this MetaClass |
private MetaMethod |
searchInterfacesForMetaMethod(java.lang.String methodName,
java.lang.Object[] arguments,
java.lang.Class[] interfaces)
|
private MetaBeanProperty |
searchInterfacesForMetaProperty(java.lang.String propertyName,
java.lang.Class[] interfaces)
|
protected void |
setInitialized(boolean b)
|
void |
setMetaClass(MetaClass metaClass)
Allows the MetaClass to be replaced with a derived implementation. |
void |
setProperty(java.lang.Class sender,
java.lang.Object object,
java.lang.String name,
java.lang.Object newValue,
boolean useSuper,
boolean fromInsideClass)
Overrides default implementation just in case setProperty method has been overriden by ExpandoMetaClass |
void |
setProperty(java.lang.String property,
java.lang.Object newValue)
Sets the given property to the new value. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private static final java.lang.String META_CLASS
private static final java.lang.String CLASS
private static final java.lang.String META_METHODS
private static final java.lang.String METHODS
private static final java.lang.String PROPERTIES
public static final java.lang.String STATIC_QUALIFIER
private static final java.lang.Class[] ZERO_ARGUMENTS
private static final java.lang.String CONSTRUCTOR
private static final java.lang.String GET_PROPERTY_METHOD
private static final java.lang.String SET_PROPERTY_METHOD
private static final java.lang.String INVOKE_METHOD_METHOD
private static final java.lang.String CLASS_PROPERTY
private static final java.lang.String META_CLASS_PROPERTY
private static final java.lang.String GROOVY_CONSTRUCTOR
private MetaClass myMetaClass
private boolean allowChangesAfterInit
private boolean initialized
private boolean initCalled
private boolean modified
private boolean inRegistry
private final java.util.Set inheritedMetaMethods
private final java.util.Map beanPropertyCache
private final java.util.Map staticBeanPropertyCache
private final java.util.Map expandoMethods
private final java.util.Map expandoProperties
private ClosureMetaMethod getPropertyMethod
private ClosureMetaMethod invokeMethodMethod
private ClosureMetaMethod setPropertyMethod
private ClosureStaticMetaMethod invokeStaticMethodMethod
Constructor Detail |
---|
public ExpandoMetaClass(java.lang.Class theClass)
theClass
- The class that the MetaClass applies topublic ExpandoMetaClass(java.lang.Class theClass, boolean register)
theClass
- The class that the MetaClass applies toregister
- True if the MetaClass should be registered inside the MetaClassRegistry. This defaults to true and ExpandoMetaClass will effect all instances if changedpublic ExpandoMetaClass(java.lang.Class theClass, boolean register, boolean allowChangesAfterInit)
theClass
- The class that the MetaClass applies toregister
- True if the MetaClass should be registered inside the MetaClassRegistry. This defaults to true and ExpandoMetaClass will effect all instances if changedallowChangesAfterInit
- Should the meta class be modifiable after initialization. Default is false.Method Detail |
---|
public java.lang.Object invokeMissingMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Object[] arguments)
invokeMissingMethod
in interface MetaClass
invokeMissingMethod
in class MetaClassImpl
instance
- The instance to invoke methodMissing onmethodName
- The name of the methodarguments
- The arguments to the method
MetaClassImpl.invokeMissingMethod(Object, String, Object[])
public java.lang.Object invokeMissingProperty(java.lang.Object instance, java.lang.String propertyName, java.lang.Object optionalValue, boolean isGetter)
invokeMissingProperty
in interface MetaClass
invokeMissingProperty
in class MetaClassImpl
instance
- The instance of the objectpropertyName
- The property nameoptionalValue
- The property value in the case of a setterisGetter
- True if it is a getter
private MetaBeanProperty findPropertyInClassHierarchy(java.lang.String propertyName, CachedClass theClass)
private MetaBeanProperty searchInterfacesForMetaProperty(java.lang.String propertyName, java.lang.Class[] interfaces)
private MetaBeanProperty getMetaPropertyFromMutableMetaClass(java.lang.String propertyName, MetaClass metaClass)
private MetaMethod findMethodInClassHeirarchy(java.lang.String methodName, java.lang.Object[] arguments, java.lang.Class theClass)
private MetaMethod searchInterfacesForMetaMethod(java.lang.String methodName, java.lang.Object[] arguments, java.lang.Class[] interfaces)
private MetaMethod getMetaMethodFromMutableMetaClass(java.lang.String methodName, java.lang.Object[] arguments, MetaClass metaClass)
public boolean isModified()
MutableMetaClass
isModified
in interface MutableMetaClass
isModified
in class MetaClassImpl
public static void enableGlobally()
public static void disableGlobally()
public void initialize()
MetaClass
initialize
in interface MetaClass
initialize
in class MetaClassImpl
protected boolean isInitialized()
isInitialized
in class MetaClassImpl
protected void setInitialized(boolean b)
private void addSuperMethodIfNotOverriden(MetaMethod metaMethodFromSuper)
public java.lang.Object invokeConstructor(java.lang.Object[] arguments)
MetaObjectProtocol
invokeConstructor
in interface MetaObjectProtocol
invokeConstructor
in class MetaClassImpl
arguments
- The arguments to the constructor
protected java.util.LinkedList getSuperClasses()
public MetaClass getMetaClass()
GroovyObject
getMetaClass
in interface GroovyObject
public java.lang.Object getProperty(java.lang.String property)
GroovyObject
getProperty
in interface GroovyObject
property
- the name of the property of interest
private boolean isValidExpandoProperty(java.lang.String property)
public java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)
GroovyObject
invokeMethod
in interface GroovyObject
name
- the name of the method to callargs
- the arguments to use for the method call
public void setMetaClass(MetaClass metaClass)
GroovyObject
setMetaClass
in interface GroovyObject
metaClass
- the new metaclasspublic void setProperty(java.lang.String property, java.lang.Object newValue)
GroovyObject
setProperty
in interface GroovyObject
property
- the name of the property of interestnewValue
- the new value for the propertyprotected void performOperationOnMetaClass(ExpandoMetaClass.Callable c)
protected void registerBeanProperty(java.lang.String property, java.lang.Object newValue)
property
- The property namenewValue
- The properties initial valueprotected void registerInstanceMethod(java.lang.String methodName, Closure callable)
methodName
- The method namecallable
- The callable Closurepublic java.util.List getMethods()
getMethods
in interface MetaClass
getMethods
in interface MetaObjectProtocol
getMethods
in class MetaClassImpl
MetaObjectProtocol.getMethods()
public java.util.List getProperties()
MetaClassImpl
getProperties
in interface MetaClass
getProperties
in interface MetaObjectProtocol
getProperties
in class MetaClassImpl
MetaProperty
private void checkIfGroovyObjectMethod(ClosureMetaMethod metaMethod, java.lang.String methodName)
metaMethod
- The metaMethod instancemethodName
- The method nameGroovyObject
private boolean isSetPropertyMethod(java.lang.String methodName, ClosureMetaMethod metaMethod)
private boolean isGetPropertyMethod(java.lang.String methodName)
private boolean isInvokeMethod(java.lang.String methodName, ClosureMetaMethod metaMethod)
private void performRegistryCallbacks()
private void registerBeanPropertyForMethod(MetaMethod metaMethod, java.lang.String propertyName, boolean getter, boolean isStatic)
protected void registerStaticMethod(java.lang.String name, Closure callable)
name
- The method namecallable
- The callable Closurepublic java.lang.Class getJavaClass()
public void refreshInheritedMethods(java.util.Set modifiedSuperExpandos)
modifiedSuperExpandos
- A list of modified super ExpandoMetaClasspublic java.util.List getExpandoMethods()
public java.util.Collection getExpandoProperties()
public java.lang.Object invokeMethod(java.lang.Class sender, java.lang.Object object, java.lang.String methodName, java.lang.Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass)
invokeMethod
in interface MetaClass
invokeMethod
in class MetaClassImpl
sender
- The java.lang.Class instance that invoked the methodobject
- The object which the method was invoked onmethodName
- The name of the methodoriginalArguments
- The arguments to the methodisCallToSuper
- Whether the method is a call to a super class methodfromInsideClass
- Whether the call was invoked from the inside or the outside of the class
MetaClassImpl.invokeMethod(Class, Object, String, Object[], boolean, boolean)
public java.lang.Object invokeStaticMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] arguments)
invokeStaticMethod
in interface MetaObjectProtocol
invokeStaticMethod
in class MetaClassImpl
object
- An instance of the class returned by the getTheClass() method or the class itselfmethodName
- The name of the methodarguments
- The arguments to the method
MetaClassImpl.invokeStaticMethod(Object, String, Object[])
public java.lang.Object getProperty(java.lang.Class sender, java.lang.Object object, java.lang.String name, boolean useSuper, boolean fromInsideClass)
getProperty
in interface MetaClass
getProperty
in class MetaClassImpl
sender
- The java.lang.Class instance that requested the propertyobject
- The Object which the property is being retrieved fromname
- The name of the propertyuseSuper
- Whether the call is to a super class propertyfromInsideClass
- ??
MetaClassImpl.getProperty(Class, Object, String, boolean, boolean)
public java.lang.Object getProperty(java.lang.Object object, java.lang.String name)
getProperty
in interface MetaObjectProtocol
getProperty
in class MetaClassImpl
object
- An instance of the class returned by the getTheClass() methodname
- The name of the property to retrieve the value for
MetaClassImpl.getProperty(Object, String)
private boolean hasOverrideGetProperty(java.lang.String name)
public void setProperty(java.lang.Class sender, java.lang.Object object, java.lang.String name, java.lang.Object newValue, boolean useSuper, boolean fromInsideClass)
setProperty
in interface MetaClass
setProperty
in class MetaClassImpl
sender
- The java.lang.Class instance that is mutating the propertyobject
- The Object which the property is being set onname
- The name of the propertynewValue
- The new value of the property to setuseSuper
- Whether the call is to a super class propertyfromInsideClass
- ??MetaClassImpl.setProperty(Class, Object, String, Object, boolean, boolean)
public MetaProperty getMetaProperty(java.lang.String name)
getMetaProperty
in interface MetaObjectProtocol
getMetaProperty
in class MetaClassImpl
name
- The name of the MetaProperty
MetaObjectProtocol.getMetaProperty(String)
public boolean hasMetaProperty(java.lang.String name)
name
- The name of the MetaProperty
public boolean hasMetaMethod(java.lang.String name, java.lang.Class[] args)
name
- The name of the MetaMethodargs
- The arguments to the meta method
private boolean isGetter(java.lang.String name, CachedClass[] args)
name
- True if its a Javabean propertyargs
- The arguments
private java.lang.String getPropertyForGetter(java.lang.String getterName)
getterName
- The getter name
private java.lang.String convertPropertyName(java.lang.String prop)
public java.lang.String getPropertyForSetter(java.lang.String setterName)
setterName
- The setter name
public boolean isSetter(java.lang.String name, CachedClass[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |