use of org.grails.spring.BeanConfiguration in project grails-core by grails.
the class BeanBuilder method setPropertyOnBeanConfig.
protected void setPropertyOnBeanConfig(String name, Object value) {
if (value instanceof GString) {
value = value.toString();
}
if (addToDeferred(currentBeanConfig, name, value)) {
return;
}
if (value instanceof Closure) {
BeanConfiguration current = currentBeanConfig;
try {
Closure<?> callable = (Closure<?>) value;
Class<?> parameterType = callable.getParameterTypes()[0];
if (parameterType.equals(Object.class)) {
currentBeanConfig = springConfig.createSingletonBean("");
callable.call(new Object[] { currentBeanConfig });
} else {
currentBeanConfig = springConfig.createSingletonBean(parameterType);
callable.call();
}
value = currentBeanConfig.getBeanDefinition();
} finally {
currentBeanConfig = current;
}
}
currentBeanConfig.addProperty(name, value);
}
use of org.grails.spring.BeanConfiguration in project grails-core by grails.
the class BeanBuilder method invokeBeanDefiningMethod.
/**
* Called when a bean definition node is called.
*
* @param name The name of the bean to define
* @param args The arguments to the bean. The first argument is the class name, the last argument is sometimes a closure. All
* the arguments in between are constructor arguments
* @return The bean configuration instance
*/
protected BeanConfiguration invokeBeanDefiningMethod(String name, Object[] args) {
boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
if (args[0] instanceof Class) {
Class<?> beanClass = args[0] instanceof Class ? (Class<?>) args[0] : args[0].getClass();
if (args.length >= 1) {
if (hasClosureArgument) {
if (args.length - 1 != 1) {
currentBeanConfig = springConfig.addSingletonBean(name, beanClass, resolveConstructorArguments(args, 1, args.length - 1));
} else {
currentBeanConfig = springConfig.addSingletonBean(name, beanClass);
}
} else {
currentBeanConfig = springConfig.addSingletonBean(name, beanClass, resolveConstructorArguments(args, 1, args.length));
}
}
} else if (args[0] instanceof RuntimeBeanReference) {
currentBeanConfig = springConfig.addSingletonBean(name);
currentBeanConfig.setFactoryBean(((RuntimeBeanReference) args[0]).getBeanName());
} else if (args[0] instanceof Map) {
// named constructor arguments
if (args.length > 1 && args[1] instanceof Class) {
List<?> constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length - 1 : args.length);
currentBeanConfig = springConfig.addSingletonBean(name, (Class<?>) args[1], constructorArgs);
@SuppressWarnings("rawtypes") Map namedArgs = (Map) args[0];
for (Object o : namedArgs.keySet()) {
String propName = (String) o;
setProperty(propName, namedArgs.get(propName));
}
} else // factory method syntax
{
//First arg is the map containing factoryBean : factoryMethod
@SuppressWarnings("rawtypes") Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next();
// If we have a closure body, that will be the last argument.
// In between are the constructor args
int constructorArgsTest = hasClosureArgument ? 2 : 1;
// If we have more than this number of args, we have constructor args
if (args.length > constructorArgsTest) {
//factory-method requires args
int endOfConstructArgs = hasClosureArgument ? args.length - 1 : args.length;
currentBeanConfig = springConfig.addSingletonBean(name, null, resolveConstructorArguments(args, 1, endOfConstructArgs));
} else {
currentBeanConfig = springConfig.addSingletonBean(name);
}
currentBeanConfig.setFactoryBean(factoryBeanEntry.getKey().toString());
currentBeanConfig.setFactoryMethod(factoryBeanEntry.getValue().toString());
}
} else if (args[0] instanceof Closure) {
currentBeanConfig = springConfig.addAbstractBean(name);
} else {
List<?> constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length - 1 : args.length);
currentBeanConfig = new DefaultBeanConfiguration(name, null, constructorArgs);
springConfig.addBeanConfiguration(name, currentBeanConfig);
}
if (hasClosureArgument) {
Closure<?> callable = (Closure<?>) args[args.length - 1];
callable.setDelegate(this);
callable.setResolveStrategy(Closure.DELEGATE_FIRST);
callable.call(new Object[] { currentBeanConfig });
}
BeanConfiguration beanConfig = currentBeanConfig;
currentBeanConfig = null;
return beanConfig;
}
use of org.grails.spring.BeanConfiguration in project grails-core by grails.
the class BeanBuilder method bean.
/**
* Defines an inner bean definition.
*
* @param type The bean type
* @param args The constructors arguments and closure configurer
* @return The bean definition
*/
@SuppressWarnings("rawtypes")
public AbstractBeanDefinition bean(Class type, Object... args) {
BeanConfiguration current = currentBeanConfig;
try {
Closure callable = null;
Collection constructorArgs = null;
if (args != null && args.length > 0) {
int index = args.length;
Object lastArg = args[index - 1];
if (lastArg instanceof Closure) {
callable = (Closure) lastArg;
index--;
}
if (index > -1) {
constructorArgs = resolveConstructorArguments(args, 0, index);
}
}
currentBeanConfig = constructorArgs == null ? springConfig.createSingletonBean(type) : springConfig.createSingletonBean(type, constructorArgs);
if (callable != null) {
callable.call(new Object[] { currentBeanConfig });
}
return currentBeanConfig.getBeanDefinition();
} finally {
currentBeanConfig = current;
}
}
Aggregations