package net.sourceforge.javautil.groovy.builder.interceptor.objectfactory;
import groovy.lang.MetaClass;
import net.sourceforge.javautil.groovy.builder.GroovyBuilder;
import org.codehaus.groovy.runtime.InvokerHelper;
/**
* This is the standard {@link NodeMapper} expecting optional addChild(node) and setParent(node) methods
* on node object instances.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: StandardNodeMapper.java 1844 2010-02-19 05:21:05Z ponderator $
*/
public class StandardNodeMapper implements NodeMapper {
/**
* This will assume that parent nodes will have a method called 'addChild'
* that takes a single argument which is the child node. It will also assume
* that child nodes will have a method called 'setParent'.
*
* @see ObjectFactoryInterceptor#isPassParentToConstructor()
*/
public void associate(GroovyBuilder builder, ObjectFactoryInterceptor ofi, Object child, Object parent) {
if (parent != null) {
MetaClass pmc = InvokerHelper.getMetaClass(child.getClass());
pmc.invokeMethod(parent, "addChild", new Object[] { parent });
}
MetaClass cmc = InvokerHelper.getMetaClass(child.getClass());
cmc.invokeMethod(child, "setParent", new Object[] { parent });
}
}