Groovy 2.2.0

groovy.util
[Java] Class FactoryBuilderSupport

java.lang.Object
  groovy.lang.GroovyObjectSupport
      groovy.lang.Binding
          groovy.util.FactoryBuilderSupport

public abstract class FactoryBuilderSupport
extends Binding

Mix of BuilderSupport and SwingBuilder's factory support. Warning: this implementation is not thread safe and should not be used across threads in a multi-threaded environment. A locking mechanism should be implemented by the subclass if use is expected across multiple threads.

Authors:
James Strachan
Andres Almiray
Danno Ferrin


Field Summary
static String CHILD_BUILDER

static String CURRENT_BUILDER

static String CURRENT_FACTORY

static String CURRENT_NAME

static String CURRENT_NODE

static String OWNER

static String PARENT_BUILDER

static String PARENT_CONTEXT

static String PARENT_FACTORY

static String PARENT_NAME

static String PARENT_NODE

static String SCRIPT_CLASS_NAME

protected LinkedList attributeDelegates

protected boolean autoRegistrationComplete

protected boolean autoRegistrationRunning

protected Map explicitMethods

protected Map explicitProperties

protected Closure methodMissingDelegate

protected LinkedList postInstantiateDelegates

protected LinkedList postNodeCompletionDelegates

protected LinkedList preInstantiateDelegates

protected Closure propertyMissingDelegate

protected Map registrationGroup

protected String registrationGroupName

 
Constructor Summary
FactoryBuilderSupport()

FactoryBuilderSupport(boolean init)

 
Method Summary
Closure addAttributeDelegate(Closure attrDelegate)

Add an attribute delegate so it can intercept attributes being set.

void addDisposalClosure(Closure closure)

Closure addPostInstantiateDelegate(Closure delegate)

Closure addPostNodeCompletionDelegate(Closure delegate)

Closure addPreInstantiateDelegate(Closure delegate)

void autoRegisterNodes()

Ask the nodes to be registered

Object build(Class viewClass)

Object build(Script script)

Object build(String script, GroovyClassLoader loader)

protected boolean checkExplicitMethod(String methodName, Object args, Reference result)

static void checkValueIsNull(Object value, Object name)

Throws an exception if value is null.

static boolean checkValueIsType(Object value, Object name, Class type)

Checks type of value against builder type

static boolean checkValueIsTypeNotString(Object value, Object name, Class type)

Checks values against factory's type

protected Object createNode(Object name, Map attributes, Object value)

protected Object dispatchNodeCall(Object name, Object args)

protected Object dispathNodeCall(Object name, Object args)

Use FactoryBuilderSupport.dispatchNodeCall instead.

void dispose()

List getAttributeDelegates()

FactoryBuilderSupport getChildBuilder()

Map getContext()

Object getContextAttribute(String key)

protected LinkedList getContexts()

Restores the state of the current builder to the same state as an older build.

protected Map getContinuationData()

Object getCurrent()

@return the current node being built.

FactoryBuilderSupport getCurrentBuilder()

@return the node of the parent of the current node.

Factory getCurrentFactory()

@return the factory of the parent of the current node.

String getCurrentName()

@return the builder that built the current node.

Map getExplicitMethods()

@return the explicit properties map (Unmodifiable Map).

Map getExplicitProperties()

@return the factory map (Unmodifiable Map).

Map getFactories()

@return the factory map (Unmodifiable Map).

Map getLocalExplicitMethods()

@return the explicit properties map (Unmodifiable Map).

Map getLocalExplicitProperties()

Map getLocalFactories()

@return the explicit methods map (Unmodifiable Map).

Closure getMethodMissingDelegate()

Object getName(String methodName)

A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.

Closure getNameMappingClosure()

Map getParentContext()

@return the name of the parent of the current node.

Factory getParentFactory()

@return the context of the parent of the current node.

String getParentName()

Object getParentNode()

@return the factory of the parent of the current node.

List getPostInstantiateDelegates()

List getPostNodeCompletionDelegates()

List getPreInstantiateDelegates()

Object getProperty(String property)

Closure getPropertyMissingDelegate()

protected FactoryBuilderSupport getProxyBuilder()

Set getRegistrationGroupItems(String group)

Set getRegistrationGroups()

Object getVariable(String name)

@param name the name of the variable to lookup

Map getVariables()

protected void handleNodeAttributes(Object node, Map attributes)

Object invokeMethod(String methodName)

Convenience method when no arguments are required

Object invokeMethod(String methodName, Object args)

protected void newContext()

A hook to allow nodes to be processed once they have had all of their children applied.

protected void nodeCompleted(Object parent, Object node)

protected Map popContext()

protected void postInstantiate(Object name, Map attributes, Object node)

protected Object postNodeCompletion(Object parent, Object node)

A hook before the factory creates the node.

protected void preInstantiate(Object name, Map attributes, Object value)

A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders.

void registerBeanFactory(String theName, Class beanClass)

void registerBeanFactory(String theName, String groupName, Class beanClass)

void registerExplicitMethod(String name, Closure closure)

void registerExplicitMethod(String name, String groupName, Closure closure)

void registerExplicitProperty(String name, Closure getter, Closure setter)

void registerExplicitProperty(String name, String groupName, Closure getter, Closure setter)

void registerFactory(String name, Factory factory)

Registers a factory for a node name.

void registerFactory(String name, String groupName, Factory factory)

void removeAttributeDelegate(Closure attrDelegate)

void removePostInstantiateDelegate(Closure delegate)

void removePostNodeCompletionDelegate(Closure delegate)

void removePreInstantiateDelegate(Closure delegate)

protected void reset()

protected Closure resolveExplicitMethod(String methodName, Object args)

protected Closure[] resolveExplicitProperty(String propertyName)

protected Factory resolveFactory(Object name, Map attributes, Object value)

This is a hook for subclasses to plugin a custom strategy for mapping names to factories.

protected void restoreFromContinuationData(Map data)

protected void setClosureDelegate(Closure closure, Object node)

void setMethodMissingDelegate(Closure delegate)

void setNameMappingClosure(Closure nameMappingClosure)

protected void setNodeAttributes(Object node, Map attributes)

protected void setParent(Object parent, Object child)

@return the stack of available contexts.

void setProperty(String property, Object newValue)

Overloaded to make variables appear as bean properties or via the subscript operator

void setPropertyMissingDelegate(Closure delegate)

@return the context of the current node.

protected void setProxyBuilder(FactoryBuilderSupport proxyBuilder)

Assigns any existing properties to the node.

void setVariable(String name, Object value)

Object withBuilder(FactoryBuilderSupport builder, Closure closure)

Object withBuilder(FactoryBuilderSupport builder, String name, Closure closure)

Switches the builder's proxyBuilder during the execution of a closure.

Object withBuilder(Map attributes, FactoryBuilderSupport builder, String name, Closure closure)

 
Methods inherited from class Binding
getProperty, getVariable, getVariables, hasVariable, setProperty, setVariable
 
Methods inherited from class GroovyObjectSupport
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty
 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

CHILD_BUILDER

public static final String CHILD_BUILDER


CURRENT_BUILDER

public static final String CURRENT_BUILDER


CURRENT_FACTORY

public static final String CURRENT_FACTORY


CURRENT_NAME

public static final String CURRENT_NAME


CURRENT_NODE

public static final String CURRENT_NODE


OWNER

public static final String OWNER


PARENT_BUILDER

public static final String PARENT_BUILDER


PARENT_CONTEXT

public static final String PARENT_CONTEXT


PARENT_FACTORY

public static final String PARENT_FACTORY


PARENT_NAME

public static final String PARENT_NAME


PARENT_NODE

public static final String PARENT_NODE


SCRIPT_CLASS_NAME

public static final String SCRIPT_CLASS_NAME


attributeDelegates

protected LinkedList attributeDelegates


autoRegistrationComplete

protected boolean autoRegistrationComplete


autoRegistrationRunning

protected boolean autoRegistrationRunning


explicitMethods

protected Map explicitMethods


explicitProperties

protected Map explicitProperties


methodMissingDelegate

protected Closure methodMissingDelegate


postInstantiateDelegates

protected LinkedList postInstantiateDelegates


postNodeCompletionDelegates

protected LinkedList postNodeCompletionDelegates


preInstantiateDelegates

protected LinkedList preInstantiateDelegates


propertyMissingDelegate

protected Closure propertyMissingDelegate


registrationGroup

protected Map registrationGroup


registrationGroupName

protected String registrationGroupName


 
Constructor Detail

FactoryBuilderSupport

public FactoryBuilderSupport()


FactoryBuilderSupport

public FactoryBuilderSupport(boolean init)


 
Method Detail

addAttributeDelegate

public Closure addAttributeDelegate(Closure attrDelegate)
Add an attribute delegate so it can intercept attributes being set. Attribute delegates are fired in a FILO pattern, so that nested delegates get first crack.
Parameters:
attrDelegate - the closure to be called
Returns:
attrDelegate


addDisposalClosure

public void addDisposalClosure(Closure closure)


addPostInstantiateDelegate

public Closure addPostInstantiateDelegate(Closure delegate)


addPostNodeCompletionDelegate

public Closure addPostNodeCompletionDelegate(Closure delegate)


addPreInstantiateDelegate

public Closure addPreInstantiateDelegate(Closure delegate)


autoRegisterNodes

public void autoRegisterNodes()
Ask the nodes to be registered


build

public Object build(Class viewClass)


build

public Object build(Script script)


build

public Object build(String script, GroovyClassLoader loader)


checkExplicitMethod

protected boolean checkExplicitMethod(String methodName, Object args, Reference result)


checkValueIsNull

public static void checkValueIsNull(Object value, Object name)
Throws an exception if value is null.
Parameters:
value - the node's value
name - the node's name


checkValueIsType

public static boolean checkValueIsType(Object value, Object name, Class type)
Checks type of value against builder type
Parameters:
value - the node's value
name - the node's name
type - a Class that may be assignable to the value's class
Returns:
true if type is assignable to the value's class, false if value is null.


checkValueIsTypeNotString

public static boolean checkValueIsTypeNotString(Object value, Object name, Class type)
Checks values against factory's type
Parameters:
value - the node's value
name - the node's name
type - a Class that may be assignable to the value's class
Returns:
Returns true if type is assignable to the value's class, false if value is null or a String.


createNode

protected Object createNode(Object name, Map attributes, Object value)


dispatchNodeCall

protected Object dispatchNodeCall(Object name, Object args)


dispathNodeCall

@Deprecated// should be called on first build method only
protected Object dispathNodeCall(Object name, Object args)
Use FactoryBuilderSupport.dispatchNodeCall instead.


dispose

public void dispose()


getAttributeDelegates

public List getAttributeDelegates()


getChildBuilder

public FactoryBuilderSupport getChildBuilder()


getContext

public Map getContext()


getContextAttribute

public Object getContextAttribute(String key)


getContexts

protected LinkedList getContexts()
Restores the state of the current builder to the same state as an older build. Caution, this will destroy rather than merge the current build context if there is any,
Parameters:
data - the data retrieved from a compatible getContinuationData call


getContinuationData

protected Map getContinuationData()


getCurrent

public Object getCurrent()
Returns:
the current node being built.


getCurrentBuilder

public FactoryBuilderSupport getCurrentBuilder()
Returns:
the node of the parent of the current node.


getCurrentFactory

public Factory getCurrentFactory()
Returns:
the factory of the parent of the current node.


getCurrentName

public String getCurrentName()
Returns:
the builder that built the current node.


getExplicitMethods

public Map getExplicitMethods()
Returns:
the explicit properties map (Unmodifiable Map).


getExplicitProperties

public Map getExplicitProperties()
Returns:
the factory map (Unmodifiable Map).


getFactories

public Map getFactories()
Returns:
the factory map (Unmodifiable Map).


getLocalExplicitMethods

public Map getLocalExplicitMethods()
Returns:
the explicit properties map (Unmodifiable Map).


getLocalExplicitProperties

public Map getLocalExplicitProperties()


getLocalFactories

public Map getLocalFactories()
Returns:
the explicit methods map (Unmodifiable Map).


getMethodMissingDelegate

public Closure getMethodMissingDelegate()


getName

public Object getName(String methodName)
A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.
Parameters:
methodName - the name of the desired method
Returns:
the object representing the name


getNameMappingClosure

public Closure getNameMappingClosure()


getParentContext

public Map getParentContext()
Returns:
the name of the parent of the current node.


getParentFactory

public Factory getParentFactory()
Returns:
the context of the parent of the current node.


getParentName

public String getParentName()


getParentNode

public Object getParentNode()
Returns:
the factory of the parent of the current node.


getPostInstantiateDelegates

public List getPostInstantiateDelegates()


getPostNodeCompletionDelegates

public List getPostNodeCompletionDelegates()


getPreInstantiateDelegates

public List getPreInstantiateDelegates()


getProperty

public Object getProperty(String property)


getPropertyMissingDelegate

public Closure getPropertyMissingDelegate()


getProxyBuilder

protected FactoryBuilderSupport getProxyBuilder()


getRegistrationGroupItems

public Set getRegistrationGroupItems(String group)


getRegistrationGroups

public Set getRegistrationGroups()


getVariable

public Object getVariable(String name)
Parameters:
name - the name of the variable to lookup
Returns:
the variable value


getVariables

public Map getVariables()


handleNodeAttributes

protected void handleNodeAttributes(Object node, Map attributes)


invokeMethod

public Object invokeMethod(String methodName)
Convenience method when no arguments are required
Parameters:
methodName - the name of the method to invoke
Returns:
the result of the call


invokeMethod

public Object invokeMethod(String methodName, Object args)


newContext

protected void newContext()
A hook to allow nodes to be processed once they have had all of their children applied.
Parameters:
node - the current node being processed
parent - the parent of the node being processed


nodeCompleted

protected void nodeCompleted(Object parent, Object node)


popContext

protected Map popContext()


postInstantiate

protected void postInstantiate(Object name, Map attributes, Object node)


postNodeCompletion

protected Object postNodeCompletion(Object parent, Object node)
A hook before the factory creates the node.
It will call any registered preInstantiateDelegates, if you override this method be sure to call this impl somewhere in your code.
Parameters:
name - the name of the node
attributes - the attributes of the node
value - the value argument(s) of the node


preInstantiate

protected void preInstantiate(Object name, Map attributes, Object value)
A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders. This method should call the setDelegate() method on the closure which by default passes in this but if node is-a builder we could pass that in instead (or do something wacky too)
Parameters:
closure - the closure on which to call setDelegate()
node - the node value that we've just created, which could be a builder


registerBeanFactory

public void registerBeanFactory(String theName, Class beanClass)


registerBeanFactory

public void registerBeanFactory(String theName, String groupName, Class beanClass)


registerExplicitMethod

public void registerExplicitMethod(String name, Closure closure)


registerExplicitMethod

public void registerExplicitMethod(String name, String groupName, Closure closure)


registerExplicitProperty

public void registerExplicitProperty(String name, Closure getter, Closure setter)


registerExplicitProperty

public void registerExplicitProperty(String name, String groupName, Closure getter, Closure setter)


registerFactory

public void registerFactory(String name, Factory factory)
Registers a factory for a node name.
Parameters:
name - the name of the node
factory - the factory to return the values


registerFactory

public void registerFactory(String name, String groupName, Factory factory)


removeAttributeDelegate

public void removeAttributeDelegate(Closure attrDelegate)


removePostInstantiateDelegate

public void removePostInstantiateDelegate(Closure delegate)


removePostNodeCompletionDelegate

public void removePostNodeCompletionDelegate(Closure delegate)


removePreInstantiateDelegate

public void removePreInstantiateDelegate(Closure delegate)


reset

protected void reset()


resolveExplicitMethod

@SuppressWarnings*/
protected Closure resolveExplicitMethod(String methodName, Object args)


resolveExplicitProperty

protected Closure[] resolveExplicitProperty(String propertyName)


resolveFactory

protected Factory resolveFactory(Object name, Map attributes, Object value)
This is a hook for subclasses to plugin a custom strategy for mapping names to factories.
Parameters:
name - the name of the factory
attributes - the attributes from the node
value - value arguments from te node
Returns:
the Factory associated with name.


restoreFromContinuationData

protected void restoreFromContinuationData(Map data)


setClosureDelegate

@SuppressWarnings* @param parent the object from the parent node
protected void setClosureDelegate(Closure closure, Object node)


setMethodMissingDelegate

public void setMethodMissingDelegate(Closure delegate)


setNameMappingClosure

public void setNameMappingClosure(Closure nameMappingClosure)


setNodeAttributes

protected void setNodeAttributes(Object node, Map attributes)


setParent

protected void setParent(Object parent, Object child)
Returns:
the stack of available contexts.


setProperty

public void setProperty(String property, Object newValue)
Overloaded to make variables appear as bean properties or via the subscript operator


setPropertyMissingDelegate

public void setPropertyMissingDelegate(Closure delegate)
Returns:
the context of the current node.


setProxyBuilder

protected void setProxyBuilder(FactoryBuilderSupport proxyBuilder)
Assigns any existing properties to the node.
It will call attributeDelegates before passing control to the factory that built the node.
Parameters:
node - the object returned by tne node factory
attributes - the attributes for the node


setVariable

public void setVariable(String name, Object value)


withBuilder

public Object withBuilder(FactoryBuilderSupport builder, Closure closure)


withBuilder

public Object withBuilder(FactoryBuilderSupport builder, String name, Closure closure)
Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup. It will also take care of restoring the previous proxyBuilder when the execution finishes, even if an exception was thrown from inside the closure. Additionally it will use the closure's result as the value for the node identified by 'name'.
throws:
RuntimeException - any exception the closure might have thrown during execution.
Parameters:
builder - the temporary builder to switch to as proxyBuilder.
name - the node to build on the 'parent' builder.
closure - the closure to be executed under the temporary builder.
Returns:
a node that responds to value of name with the closure's result as its value.


withBuilder

public Object withBuilder(Map attributes, FactoryBuilderSupport builder, String name, Closure closure)


 

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