Groovy 1.7.0

groovy.lang
Class ExpandoMetaClass

java.lang.Object
  groovy.lang.MetaClassImpl
      groovy.lang.ExpandoMetaClass
All Implemented Interfaces:
GroovyObject

class ExpandoMetaClass
extends MetaClassImpl

A MetaClass that implements GroovyObject and behaves like an Expando, allowing the addition of new methods on the fly. Some examples of usage:

 // 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"
 
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. ExpandoMetaClass differs here from the default in that it allows you adding methods after initialize has been called. This is done by setting the initialize flag internally to false and then add the methods. Since this is not thread safe it has to be done in a synchronized block. The methods to check for modification and initialization are therefore synchronized as well. Any method call done through this meta class will first check if the it is synchronized. Should this happen during a modification, then the method cannot be selected or called unless the modification is completed. 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.
author:
Graeme Rocher
since:
1.5


Nested Class Summary
interface ExpandoMetaClass.Callable

For simulating closures in Java

class ExpandoMetaClass.DefiningClosure

class ExpandoMetaClass.ExpandoMetaConstructor

Handles the ability to use the left shift operator to append new constructors

class ExpandoMetaClass.ExpandoMetaProperty

Instances of this class are returned when using the << left shift operator.

class ExpandoMetaClass.MixedInAccessor

class ExpandoMetaClass.StaticDefiningClosure

class ExpandoMetaClass.SubClassDefiningClosure

 
Field Summary
static String CONSTRUCTOR

static String STATIC_QUALIFIER

boolean inRegistry

 
Fields inherited from class MetaClassImpl
INVOKE_METHOD_METHOD, METHOD_MISSING, PROPERTY_MISSING, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, getPropertyMethod, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, registry, setPropertyMethod, theCachedClass, theClass
 
Constructor Summary
ExpandoMetaClass(Class theClass)

Constructs a new ExpandoMetaClass instance for the given class

ExpandoMetaClass(Class theClass, MetaMethod[] add)

ExpandoMetaClass(Class theClass, boolean register)

Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass in the MetaClassRegistry automatically

ExpandoMetaClass(Class theClass, boolean register, MetaMethod[] add)

ExpandoMetaClass(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
void addMixinClass(MixinInMetaClass mixin)

Object castToMixedType(Object obj, Class type)

CallSite createConstructorSite(CallSite site, Object[] args)

CallSite createPogoCallCurrentSite(CallSite site, Class sender, String name, Object[] args)

CallSite createPogoCallSite(CallSite site, Object[] args)

CallSite createPojoCallSite(CallSite site, Object receiver, Object[] args)

CallSite createStaticSite(CallSite site, Object[] args)

ExpandoMetaClass define(Closure closure)

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.

MetaMethod findMixinMethod(String methodName, Class[] arguments)

List getExpandoMethods()

Returns a list of expando MetaMethod instances added to this ExpandoMetaClass

Collection getExpandoProperties()

Returns a list of MetaBeanProperty instances added to this ExpandoMetaClass

Collection getExpandoSubclassMethods()

Class getJavaClass()

MetaClass getMetaClass()

MetaProperty getMetaProperty(String name)

Looks up an existing MetaProperty by name

List getMethods()

Overrides the behavior of parent getMethods() method to make MetaClass aware of added Expando methods

List getProperties()

Object getProperty(String property)

Object getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass)

Overrides default implementation just in case getProperty method has been overridden by ExpandoMetaClass

Object getProperty(Object object, String name)

Overrides default implementation just in case getProperty method has been overriden by ExpandoMetaClass

String getPropertyForSetter(String setterName)

Returns a property name equivalent for the given setter name or null if it is not a getter

protected Object getSubclassMetaMethods(String methodName)

boolean hasMetaMethod(String name, Class[] args)

Checks whether a MetaMethod for the given name and arguments exists

boolean hasMetaProperty(String name)

Returns true if the MetaClass has the given property

void initialize()

Object invokeConstructor(Object[] arguments)

Object invokeMethod(String name, Object args)

Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass)

Overrides default implementation just in case invokeMethod has been overriden by ExpandoMetaClass

Object invokeStaticMethod(Object object, String methodName, Object[] arguments)

Overrides default implementation just in case a static invoke method has been set on ExpandoMetaClass

protected boolean isInitialized()

Checks if the meta class is initialized.

boolean isModified()

boolean isSetter(String name, CachedClass[] args)

static boolean isValidExpandoProperty(String property)

protected void onGetPropertyFoundInHierarchy(MetaMethod method)

protected void onInvokeMethodFoundInHierarchy(MetaMethod method)

protected void onSetPropertyFoundInHierarchy(MetaMethod method)

protected void onSuperMethodFoundInHierarchy(MetaMethod method)

protected void onSuperPropertyFoundInHierarchy(MetaBeanProperty property)

protected void performOperationOnMetaClass(Callable c)

void refreshInheritedMethods(Set modifiedSuperExpandos)

Called from ExpandoMetaClassCreationHandle in the registry if it exists to set up inheritance handling

void registerBeanProperty(String property, Object newValue)

Registers a new bean property

void registerInstanceMethod(MetaMethod metaMethod)

Registers a new instance method for the given method name and closure on this MetaClass

void registerInstanceMethod(String name, Closure closure)

protected void registerStaticMethod(String name, Closure callable)

protected void registerStaticMethod(String name, Closure callable, Class[] paramTypes)

Registers a new static method for the given method name and closure on this MetaClass

void registerSubclassInstanceMethod(String name, Class klazz, Closure closure)

void registerSubclassInstanceMethod(MetaMethod metaMethod)

protected void setInitialized(boolean b)

void setMetaClass(MetaClass metaClass)

void setProperty(String property, Object newValue)

void setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass)

Overrides default implementation just in case setProperty method has been overriden by ExpandoMetaClass

 
Methods inherited from class MetaClassImpl
getMetaMethod, getMetaProperty, getStaticMetaMethod, getSuperClasses, getTheCachedClass, getTheClass, hasProperty, isGroovyObject, respondsTo, respondsTo
 
Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 

Field Detail

CONSTRUCTOR

static final String CONSTRUCTOR


STATIC_QUALIFIER

static final String STATIC_QUALIFIER


inRegistry

boolean inRegistry


 
Constructor Detail

ExpandoMetaClass

public ExpandoMetaClass(Class theClass)
Constructs a new ExpandoMetaClass instance for the given class
param:
theClass The class that the MetaClass applies to


ExpandoMetaClass

public ExpandoMetaClass(Class theClass, MetaMethod[] add)


ExpandoMetaClass

public ExpandoMetaClass(Class theClass, boolean register)
Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass in the MetaClassRegistry automatically
param:
theClass The class that the MetaClass applies to
param:
register True if the MetaClass should be registered inside the MetaClassRegistry. This defaults to true and ExpandoMetaClass will effect all instances if changed


ExpandoMetaClass

public ExpandoMetaClass(Class theClass, boolean register, MetaMethod[] add)


ExpandoMetaClass

public ExpandoMetaClass(Class theClass, boolean register, boolean allowChangesAfterInit)
Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass in the MetaClassRegistry automatically
param:
theClass The class that the MetaClass applies to
param:
register True if the MetaClass should be registered inside the MetaClassRegistry. This defaults to true and ExpandoMetaClass will effect all instances if changed
param:
allowChangesAfterInit Should the meta class be modifiable after initialization. Default is false.


 
Method Detail

addMixinClass

public void addMixinClass(MixinInMetaClass mixin)


castToMixedType

public Object castToMixedType(Object obj, Class type)


createConstructorSite

public CallSite createConstructorSite(CallSite site, Object[] args)


createPogoCallCurrentSite

public CallSite createPogoCallCurrentSite(CallSite site, Class sender, String name, Object[] args)


createPogoCallSite

public CallSite createPogoCallSite(CallSite site, Object[] args)


createPojoCallSite

public CallSite createPojoCallSite(CallSite site, Object receiver, Object[] args)


createStaticSite

public CallSite createStaticSite(CallSite site, Object[] args)


define

public ExpandoMetaClass define(Closure closure)


disableGlobally

public static void disableGlobally()
Call to disable the global use of ExpandoMetaClass


enableGlobally

public static void enableGlobally()
Call to enable global use of global use of ExpandoMetaClass within the registry. This has the advantage that inheritance will function correctly, but has a higher memory usage on the JVM than normal Groovy


findMixinMethod

public MetaMethod findMixinMethod(String methodName, Class[] arguments)


getExpandoMethods

public List getExpandoMethods()
Returns a list of expando MetaMethod instances added to this ExpandoMetaClass
return:
the expandoMethods


getExpandoProperties

public Collection getExpandoProperties()
Returns a list of MetaBeanProperty instances added to this ExpandoMetaClass
return:
the expandoProperties


getExpandoSubclassMethods

public Collection getExpandoSubclassMethods()


getJavaClass

public Class getJavaClass()
return:
The Java class enhanced by this MetaClass


getMetaClass

public MetaClass getMetaClass()


getMetaProperty

public MetaProperty getMetaProperty(String name)
Looks up an existing MetaProperty by name
param:
name The name of the MetaProperty
return:
The MetaProperty or null if it doesn't exist


getMethods

public List getMethods()
Overrides the behavior of parent getMethods() method to make MetaClass aware of added Expando methods
see:
MetaObjectProtocol#getMethods()
return:
A list of MetaMethods


getProperties

public List getProperties()


getProperty

public Object getProperty(String property)


getProperty

public Object getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass)
Overrides default implementation just in case getProperty method has been overridden by ExpandoMetaClass
see:
MetaClassImpl#getProperty(Class, Object, String, boolean, boolean)


getProperty

public Object getProperty(Object object, String name)
Overrides default implementation just in case getProperty method has been overriden by ExpandoMetaClass
see:
MetaClassImpl#getProperty(Object, String)


getPropertyForSetter

public String getPropertyForSetter(String setterName)
Returns a property name equivalent for the given setter name or null if it is not a getter
param:
setterName The setter name
return:
The property name equivalent


getSubclassMetaMethods

protected Object getSubclassMetaMethods(String methodName)


hasMetaMethod

public boolean hasMetaMethod(String name, Class[] args)
Checks whether a MetaMethod for the given name and arguments exists
param:
name The name of the MetaMethod
param:
args The arguments to the meta method
return:
True if the method exists otherwise null


hasMetaProperty

public boolean hasMetaProperty(String name)
Returns true if the MetaClass has the given property
param:
name The name of the MetaProperty
return:
True it exists as a MetaProperty


initialize

public void initialize()


invokeConstructor

public Object invokeConstructor(Object[] arguments)


invokeMethod

public Object invokeMethod(String name, Object args)


invokeMethod

public Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass)
Overrides default implementation just in case invokeMethod has been overriden by ExpandoMetaClass
see:
MetaClassImpl#invokeMethod


invokeStaticMethod

public Object invokeStaticMethod(Object object, String methodName, Object[] arguments)
Overrides default implementation just in case a static invoke method has been set on ExpandoMetaClass
see:
MetaClassImpl#invokeStaticMethod(Object, String, Object[])


isInitialized

protected boolean isInitialized()
Checks if the meta class is initialized.
see:
MetaClassImpl#isInitialized


isModified

public boolean isModified()


isSetter

public boolean isSetter(String name, CachedClass[] args)


isValidExpandoProperty

public static boolean isValidExpandoProperty(String property)


onGetPropertyFoundInHierarchy

protected void onGetPropertyFoundInHierarchy(MetaMethod method)


onInvokeMethodFoundInHierarchy

protected void onInvokeMethodFoundInHierarchy(MetaMethod method)


onSetPropertyFoundInHierarchy

protected void onSetPropertyFoundInHierarchy(MetaMethod method)


onSuperMethodFoundInHierarchy

protected void onSuperMethodFoundInHierarchy(MetaMethod method)


onSuperPropertyFoundInHierarchy

protected void onSuperPropertyFoundInHierarchy(MetaBeanProperty property)


performOperationOnMetaClass

protected void performOperationOnMetaClass(Callable c)


refreshInheritedMethods

public void refreshInheritedMethods(Set modifiedSuperExpandos)
Called from ExpandoMetaClassCreationHandle in the registry if it exists to set up inheritance handling
param:
modifiedSuperExpandos A list of modified super ExpandoMetaClass


registerBeanProperty

public void registerBeanProperty(String property, Object newValue)
Registers a new bean property
param:
property The property name
param:
newValue The properties initial value


registerInstanceMethod

public void registerInstanceMethod(MetaMethod metaMethod)
Registers a new instance method for the given method name and closure on this MetaClass
param:
metaMethod


registerInstanceMethod

public void registerInstanceMethod(String name, Closure closure)


registerStaticMethod

protected void registerStaticMethod(String name, Closure callable)


registerStaticMethod

protected void registerStaticMethod(String name, Closure callable, Class[] paramTypes)
Registers a new static method for the given method name and closure on this MetaClass
param:
name The method name
param:
callable The callable Closure


registerSubclassInstanceMethod

public void registerSubclassInstanceMethod(String name, Class klazz, Closure closure)


registerSubclassInstanceMethod

public void registerSubclassInstanceMethod(MetaMethod metaMethod)


setInitialized

protected void setInitialized(boolean b)


setMetaClass

public void setMetaClass(MetaClass metaClass)


setProperty

public void setProperty(String property, Object newValue)


setProperty

public void setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass)
Overrides default implementation just in case setProperty method has been overriden by ExpandoMetaClass
see:
MetaClassImpl#setProperty(Class, Object, String, Object, boolean, boolean)


 

Copyright © 2003-2009 The Codehaus. All rights reserved.