Polish GroovyBeanDefinitionReader formatting

Consistent use of tabs and remove trailing whitespace.

Issue: SPR-7123
This commit is contained in:
Phillip Webb
2013-10-17 13:46:32 -07:00
parent bb2802208b
commit f4a66a4326
6 changed files with 721 additions and 721 deletions

View File

@@ -45,53 +45,53 @@ class GroovyDynamicElementReader extends GroovyObjectSupport {
private boolean callAfterInvocation = true private boolean callAfterInvocation = true
public GroovyDynamicElementReader(String namespace, Map<String, String> namespaceMap, public GroovyDynamicElementReader(String namespace, Map<String, String> namespaceMap,
BeanDefinitionParserDelegate delegate, GroovyBeanDefinitionWrapper beanDefinition, boolean decorating) { BeanDefinitionParserDelegate delegate, GroovyBeanDefinitionWrapper beanDefinition, boolean decorating) {
super(); super();
this.rootNamespace = namespace this.rootNamespace = namespace
this.xmlNamespaces = namespaceMap this.xmlNamespaces = namespaceMap
this.delegate = delegate this.delegate = delegate
this.beanDefinition = beanDefinition; this.beanDefinition = beanDefinition;
this.decorating = decorating; this.decorating = decorating;
} }
@Override @Override
public Object invokeMethod(String name, Object args) { public Object invokeMethod(String name, Object args) {
if (name.equals("doCall")) { if (name.equals("doCall")) {
def callable = args[0] def callable = args[0]
callable.resolveStrategy = Closure.DELEGATE_FIRST callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = this callable.delegate = this
def result = callable.call() def result = callable.call()
if (this.callAfterInvocation) { if (this.callAfterInvocation) {
afterInvocation() afterInvocation()
this.callAfterInvocation = false this.callAfterInvocation = false
} }
return result return result
} }
else { else {
StreamingMarkupBuilder builder = new StreamingMarkupBuilder(); StreamingMarkupBuilder builder = new StreamingMarkupBuilder();
def myNamespace = this.rootNamespace def myNamespace = this.rootNamespace
def myNamespaces = this.xmlNamespaces def myNamespaces = this.xmlNamespaces
def callable = { def callable = {
for (namespace in myNamespaces) { for (namespace in myNamespaces) {
mkp.declareNamespace([(namespace.key):namespace.value]) mkp.declareNamespace([(namespace.key):namespace.value])
} }
if (args && (args[-1] instanceof Closure)) { if (args && (args[-1] instanceof Closure)) {
args[-1].resolveStrategy = Closure.DELEGATE_FIRST args[-1].resolveStrategy = Closure.DELEGATE_FIRST
args[-1].delegate = builder args[-1].delegate = builder
} }
delegate."$myNamespace"."$name"(*args) delegate."$myNamespace"."$name"(*args)
} }
callable.resolveStrategy = Closure.DELEGATE_FIRST callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = builder callable.delegate = builder
def writable = builder.bind(callable) def writable = builder.bind(callable)
def sw = new StringWriter() def sw = new StringWriter()
writable.writeTo(sw) writable.writeTo(sw)
Element element = this.delegate.readerContext.readDocumentFromString(sw.toString()).documentElement Element element = this.delegate.readerContext.readDocumentFromString(sw.toString()).documentElement
this.delegate.initDefaults(element) this.delegate.initDefaults(element)
@@ -106,13 +106,13 @@ class GroovyDynamicElementReader extends GroovyObjectSupport {
this.beanDefinition.setBeanDefinition(beanDefinition) this.beanDefinition.setBeanDefinition(beanDefinition)
} }
} }
if (this.callAfterInvocation) { if (this.callAfterInvocation) {
afterInvocation() afterInvocation()
this.callAfterInvocation = false this.callAfterInvocation = false
} }
return element return element
} }
} }
/** /**
* Hook that subclass or anonymous classes can overwrite to implement custom behavior * Hook that subclass or anonymous classes can overwrite to implement custom behavior

View File

@@ -132,7 +132,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
private final Map <String, String> namespaces = new HashMap<String, String>(); private final Map <String, String> namespaces = new HashMap<String, String>();
private final Map<String, DeferredProperty> deferredProperties = new HashMap<String, DeferredProperty>(); private final Map<String, DeferredProperty> deferredProperties = new HashMap<String, DeferredProperty>();
/** /**
@@ -140,11 +140,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* @param registry the BeanFactory to load bean definitions into, * @param registry the BeanFactory to load bean definitions into,
* in the form of a BeanDefinitionRegistry * in the form of a BeanDefinitionRegistry
*/ */
public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry) { public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry); super(registry);
this.xmlBeanDefinitionReader = new XmlBeanDefinitionReader(registry); this.xmlBeanDefinitionReader = new XmlBeanDefinitionReader(registry);
this.xmlBeanDefinitionReader.setValidating(false); this.xmlBeanDefinitionReader.setValidating(false);
} }
/** /**
* Create new GroovyBeanDefinitionReader based on the given XmlBeanDefinitionReader, * Create new GroovyBeanDefinitionReader based on the given XmlBeanDefinitionReader,
@@ -182,7 +182,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
} }
// TRADITIONAL BEAN DEFINITION READER METHODS // TRADITIONAL BEAN DEFINITION READER METHODS
/** /**
* Load bean definitions from the specified Groovy script. * Load bean definitions from the specified Groovy script.
@@ -293,26 +293,26 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
} }
/** /**
* Defines an Spring namespace definition to use. * Defines an Spring namespace definition to use.
* @param definition the namespace definition * @param definition the namespace definition
*/ */
public void xmlns(Map<String, String> definition) { public void xmlns(Map<String, String> definition) {
if (!definition.isEmpty()) { if (!definition.isEmpty()) {
for (Map.Entry<String,String> entry : definition.entrySet()) { for (Map.Entry<String,String> entry : definition.entrySet()) {
String namespace = entry.getKey(); String namespace = entry.getKey();
String uri = entry.getValue(); String uri = entry.getValue();
if (uri == null) { if (uri == null) {
throw new IllegalArgumentException("Namespace definition must supply a non-null URI"); throw new IllegalArgumentException("Namespace definition must supply a non-null URI");
} }
NamespaceHandler namespaceHandler = this.xmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri); NamespaceHandler namespaceHandler = this.xmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri);
if (namespaceHandler == null) { if (namespaceHandler == null) {
throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri,
new Location(new DescriptiveResource(("Groovy"))))); new Location(new DescriptiveResource(("Groovy")))));
} }
this.namespaces.put(namespace, uri); this.namespaces.put(namespace, uri);
} }
} }
} }
/** /**
* Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance. * Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance.
@@ -335,25 +335,25 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
// INTERNAL HANDLING OF GROOVY CLOSURES AND PROPERTIES // INTERNAL HANDLING OF GROOVY CLOSURES AND PROPERTIES
/** /**
* This method overrides method invocation to create beans for each method name that * This method overrides method invocation to create beans for each method name that
* takes a class argument * takes a class argument
*/ */
public Object invokeMethod(String name, Object arg) { public Object invokeMethod(String name, Object arg) {
Object[] args = (Object[])arg; Object[] args = (Object[])arg;
if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) { if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
return beans((Closure) args[0]); return beans((Closure) args[0]);
} }
else if ("ref".equals(name)) { else if ("ref".equals(name)) {
String refName; String refName;
if (args[0] == null) if (args[0] == null)
throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found"); throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");
if (args[0] instanceof RuntimeBeanReference) { if (args[0] instanceof RuntimeBeanReference) {
refName = ((RuntimeBeanReference)args[0]).getBeanName(); refName = ((RuntimeBeanReference)args[0]).getBeanName();
} }
else { else {
refName = args[0].toString(); refName = args[0].toString();
} }
boolean parentRef = false; boolean parentRef = false;
if (args.length > 1) { if (args.length > 1) {
@@ -363,27 +363,27 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
} }
return new RuntimeBeanReference(refName, parentRef); return new RuntimeBeanReference(refName, parentRef);
} }
else if (this.namespaces.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) { else if (this.namespaces.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) {
GroovyDynamicElementReader reader = createDynamicElementReader(name); GroovyDynamicElementReader reader = createDynamicElementReader(name);
reader.invokeMethod("doCall", args); reader.invokeMethod("doCall", args);
} }
else if (args.length > 0 && args[0] instanceof Closure) { else if (args.length > 0 && args[0] instanceof Closure) {
// abstract bean definition // abstract bean definition
return invokeBeanDefiningMethod(name, args); return invokeBeanDefiningMethod(name, args);
} }
else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) { else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
return invokeBeanDefiningMethod(name, args); return invokeBeanDefiningMethod(name, args);
} }
else if (args.length > 1 && args[args.length -1] instanceof Closure) { else if (args.length > 1 && args[args.length -1] instanceof Closure) {
return invokeBeanDefiningMethod(name, args); return invokeBeanDefiningMethod(name, args);
} }
MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry()); MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){ if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
return mc.invokeMethod(getRegistry(), name, args); return mc.invokeMethod(getRegistry(), name, args);
} }
return this; return this;
} }
private boolean addDeferredProperty(String property, Object newValue) { private boolean addDeferredProperty(String property, Object newValue) {
if (newValue instanceof List) { if (newValue instanceof List) {
this.deferredProperties.put(this.currentBeanDefinition.getBeanName() + '.' + property, this.deferredProperties.put(this.currentBeanDefinition.getBeanName() + '.' + property,
@@ -431,102 +431,102 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* @return the bean definition wrapper * @return the bean definition wrapper
*/ */
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) { private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
boolean hasClosureArgument = args[args.length - 1] instanceof Closure; boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
if (args[0] instanceof Class) { if (args[0] instanceof Class) {
Class beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass()); Class beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass());
if (args.length >= 1) { if (args.length >= 1) {
if (hasClosureArgument) { if (hasClosureArgument) {
if (args.length-1 != 1) { if (args.length-1 != 1) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper( this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(
beanName, beanClass, resolveConstructorArguments(args,1,args.length-1)); beanName, beanClass, resolveConstructorArguments(args,1,args.length-1));
} }
else { else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, beanClass); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, beanClass);
} }
} }
else { else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper( this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(
beanName, beanClass, resolveConstructorArguments(args,1,args.length)); beanName, beanClass, resolveConstructorArguments(args,1,args.length));
} }
} }
} }
else if (args[0] instanceof RuntimeBeanReference) { else if (args[0] instanceof RuntimeBeanReference) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName()); this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName());
} }
else if (args[0] instanceof Map) { else if (args[0] instanceof Map) {
// named constructor arguments // named constructor arguments
if (args.length > 1 && args[1] instanceof Class) { if (args.length > 1 && args[1] instanceof Class) {
List constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length-1 : args.length); List constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length-1 : args.length);
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class)args[1], constructorArgs); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class)args[1], constructorArgs);
Map namedArgs = (Map)args[0]; Map namedArgs = (Map)args[0];
for (Object o : namedArgs.keySet()) { for (Object o : namedArgs.keySet()) {
String propName = (String) o; String propName = (String) o;
setProperty(propName, namedArgs.get(propName)); setProperty(propName, namedArgs.get(propName));
} }
} }
// factory method syntax // factory method syntax
else { else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
//First arg is the map containing factoryBean : factoryMethod //First arg is the map containing factoryBean : factoryMethod
Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next(); Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next();
// If we have a closure body, that will be the last argument. // If we have a closure body, that will be the last argument.
// In between are the constructor args // In between are the constructor args
int constructorArgsTest = hasClosureArgument?2:1; int constructorArgsTest = hasClosureArgument?2:1;
// If we have more than this number of args, we have constructor args // If we have more than this number of args, we have constructor args
if (args.length > constructorArgsTest){ if (args.length > constructorArgsTest){
// factory-method requires args // factory-method requires args
int endOfConstructArgs = (hasClosureArgument? args.length - 1 : args.length); int endOfConstructArgs = (hasClosureArgument? args.length - 1 : args.length);
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null,
resolveConstructorArguments(args, 1, endOfConstructArgs)); resolveConstructorArguments(args, 1, endOfConstructArgs));
} }
else { else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
} }
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(factoryBeanEntry.getKey().toString()); this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(factoryBeanEntry.getKey().toString());
this.currentBeanDefinition.getBeanDefinition().setFactoryMethodName(factoryBeanEntry.getValue().toString()); this.currentBeanDefinition.getBeanDefinition().setFactoryMethodName(factoryBeanEntry.getValue().toString());
} }
} }
else if (args[0] instanceof Closure) { else if (args[0] instanceof Closure) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
this.currentBeanDefinition.getBeanDefinition().setAbstract(true); this.currentBeanDefinition.getBeanDefinition().setAbstract(true);
} }
else { else {
List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length-1 : args.length); List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length-1 : args.length);
currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs); currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs);
}
if (hasClosureArgument) {
Closure callable = (Closure)args[args.length-1];
callable.setDelegate(this);
callable.setResolveStrategy(Closure.DELEGATE_FIRST);
callable.call(new Object[]{currentBeanDefinition});
} }
GroovyBeanDefinitionWrapper beanDefinition = currentBeanDefinition; if (hasClosureArgument) {
Closure callable = (Closure)args[args.length-1];
callable.setDelegate(this);
callable.setResolveStrategy(Closure.DELEGATE_FIRST);
callable.call(new Object[]{currentBeanDefinition});
}
GroovyBeanDefinitionWrapper beanDefinition = currentBeanDefinition;
this.currentBeanDefinition = null; this.currentBeanDefinition = null;
beanDefinition.getBeanDefinition().setAttribute(GroovyBeanDefinitionWrapper.class.getName(), beanDefinition); beanDefinition.getBeanDefinition().setAttribute(GroovyBeanDefinitionWrapper.class.getName(), beanDefinition);
getRegistry().registerBeanDefinition(beanName, beanDefinition.getBeanDefinition()); getRegistry().registerBeanDefinition(beanName, beanDefinition.getBeanDefinition());
return beanDefinition; return beanDefinition;
} }
protected List<Object> resolveConstructorArguments(Object[] args, int start, int end) { protected List<Object> resolveConstructorArguments(Object[] args, int start, int end) {
Object[] constructorArgs = Arrays.copyOfRange(args, start, end); Object[] constructorArgs = Arrays.copyOfRange(args, start, end);
for (int i = 0; i < constructorArgs.length; i++) { for (int i = 0; i < constructorArgs.length; i++) {
if (constructorArgs[i] instanceof GString) { if (constructorArgs[i] instanceof GString) {
constructorArgs[i] = constructorArgs[i].toString(); constructorArgs[i] = constructorArgs[i].toString();
} }
else if (constructorArgs[i] instanceof List) { else if (constructorArgs[i] instanceof List) {
constructorArgs[i] = manageListIfNecessary((List) constructorArgs[i]); constructorArgs[i] = manageListIfNecessary((List) constructorArgs[i]);
} }
else if (constructorArgs[i] instanceof Map){ else if (constructorArgs[i] instanceof Map){
constructorArgs[i] = manageMapIfNecessary((Map) constructorArgs[i]); constructorArgs[i] = manageMapIfNecessary((Map) constructorArgs[i]);
} }
} }
return Arrays.asList(constructorArgs); return Arrays.asList(constructorArgs);
} }
/** /**
* Checks whether there are any {@link RuntimeBeanReference} inside the Map * Checks whether there are any {@link RuntimeBeanReference} inside the Map
@@ -578,39 +578,39 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
*/ */
public void setProperty(String name, Object value) { public void setProperty(String name, Object value) {
if (this.currentBeanDefinition != null) { if (this.currentBeanDefinition != null) {
applyPropertyToBeanDefinition(name, value); applyPropertyToBeanDefinition(name, value);
} }
} }
protected void applyPropertyToBeanDefinition(String name, Object value) { protected void applyPropertyToBeanDefinition(String name, Object value) {
if (value instanceof GString) { if (value instanceof GString) {
value = value.toString(); value = value.toString();
} }
if (addDeferredProperty(name, value)) { if (addDeferredProperty(name, value)) {
return; return;
} }
else if (value instanceof Closure) { else if (value instanceof Closure) {
GroovyBeanDefinitionWrapper current = this.currentBeanDefinition; GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
try { try {
Closure callable = (Closure) value; Closure callable = (Closure) value;
Class parameterType = callable.getParameterTypes()[0]; Class parameterType = callable.getParameterTypes()[0];
if (parameterType.equals(Object.class)) { if (parameterType.equals(Object.class)) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(""); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
callable.call(this.currentBeanDefinition); callable.call(this.currentBeanDefinition);
} }
else { else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
callable.call((Object) null); callable.call((Object) null);
} }
value = this.currentBeanDefinition.getBeanDefinition(); value = this.currentBeanDefinition.getBeanDefinition();
} }
finally { finally {
this.currentBeanDefinition = current; this.currentBeanDefinition = current;
} }
} }
this.currentBeanDefinition.addProperty(name, value); this.currentBeanDefinition.addProperty(name, value);
} }
/** /**
* This method overrides property retrieval in the scope of the GroovyBeanDefinitionReader to either: * This method overrides property retrieval in the scope of the GroovyBeanDefinitionReader to either:
@@ -624,15 +624,15 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
return binding.getVariable(name); return binding.getVariable(name);
} }
else { else {
if (this.namespaces.containsKey(name)) { if (this.namespaces.containsKey(name)) {
return createDynamicElementReader(name); return createDynamicElementReader(name);
} }
if (getRegistry().containsBeanDefinition(name)) { if (getRegistry().containsBeanDefinition(name)) {
GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper) GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName()); getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
if (beanDefinition != null) { if (beanDefinition != null) {
return new GroovyRuntimeBeanReference(name, beanDefinition, false); return new GroovyRuntimeBeanReference(name, beanDefinition, false);
} }
else { else {
return new RuntimeBeanReference(name, false); return new RuntimeBeanReference(name, false);
} }
@@ -656,26 +656,26 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
} }
else { else {
return getMetaClass().getProperty(this, name); return getMetaClass().getProperty(this, name);
} }
} }
} }
private GroovyDynamicElementReader createDynamicElementReader(String namespace) { private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
XmlReaderContext readerContext = this.xmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy")); XmlReaderContext readerContext = this.xmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy"));
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment()); BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment());
boolean decorating = (this.currentBeanDefinition != null); boolean decorating = (this.currentBeanDefinition != null);
if (!decorating) { if (!decorating) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
} }
return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) { return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
@Override @Override
protected void afterInvocation() { protected void afterInvocation() {
if (!this.decorating) { if (!this.decorating) {
currentBeanDefinition = null; currentBeanDefinition = null;
} }
} }
}; };
} }
/** /**

View File

@@ -50,7 +50,7 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
private static final String FACTORY_METHOD = "factoryMethod"; private static final String FACTORY_METHOD = "factoryMethod";
private static final String INIT_METHOD = "initMethod"; private static final String INIT_METHOD = "initMethod";
private static final String DESTROY_METHOD = "destroyMethod"; private static final String DESTROY_METHOD = "destroyMethod";
private static final String SINGLETON = "singleton"; private static final String SINGLETON = "singleton";
private static final List<String> dynamicProperties = new ArrayList<String>(8); private static final List<String> dynamicProperties = new ArrayList<String>(8);
@@ -173,69 +173,69 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
} }
public void setProperty(String property, Object newValue) { public void setProperty(String property, Object newValue) {
if (PARENT.equals(property)) { if (PARENT.equals(property)) {
setParent(newValue); setParent(newValue);
} }
else { else {
AbstractBeanDefinition bd = getBeanDefinition(); AbstractBeanDefinition bd = getBeanDefinition();
if (AUTOWIRE.equals(property)) { if (AUTOWIRE.equals(property)) {
if ("byName".equals(newValue)) { if ("byName".equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME); bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
} }
else if ("byType".equals(newValue)) { else if ("byType".equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE); bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
} }
else if ("constructor".equals(newValue)) { else if ("constructor".equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR); bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
} }
else if (Boolean.TRUE.equals(newValue)) { else if (Boolean.TRUE.equals(newValue)) {
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME); bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
} }
} }
// constructorArgs // constructorArgs
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) { else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
ConstructorArgumentValues cav = new ConstructorArgumentValues(); ConstructorArgumentValues cav = new ConstructorArgumentValues();
List args = (List) newValue; List args = (List) newValue;
for (Object arg : args) { for (Object arg : args) {
cav.addGenericArgumentValue(arg); cav.addGenericArgumentValue(arg);
} }
bd.setConstructorArgumentValues(cav); bd.setConstructorArgumentValues(cav);
} }
// factoryBean // factoryBean
else if (FACTORY_BEAN.equals(property)) { else if (FACTORY_BEAN.equals(property)) {
if (newValue != null) { if (newValue != null) {
bd.setFactoryBeanName(newValue.toString()); bd.setFactoryBeanName(newValue.toString());
} }
} }
// factoryMethod // factoryMethod
else if (FACTORY_METHOD.equals(property)) { else if (FACTORY_METHOD.equals(property)) {
if (newValue != null) if (newValue != null)
bd.setFactoryMethodName(newValue.toString()); bd.setFactoryMethodName(newValue.toString());
} }
// initMethod // initMethod
else if (INIT_METHOD.equals(property)) { else if (INIT_METHOD.equals(property)) {
if (newValue != null) { if (newValue != null) {
bd.setInitMethodName(newValue.toString()); bd.setInitMethodName(newValue.toString());
} }
} }
// destroyMethod // destroyMethod
else if (DESTROY_METHOD.equals(property)) { else if (DESTROY_METHOD.equals(property)) {
if (newValue != null) { if (newValue != null) {
bd.setDestroyMethodName(newValue.toString()); bd.setDestroyMethodName(newValue.toString());
} }
} }
// singleton property // singleton property
else if (SINGLETON.equals(property)) { else if (SINGLETON.equals(property)) {
bd.setScope(Boolean.TRUE.equals(newValue) ? bd.setScope(Boolean.TRUE.equals(newValue) ?
BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE); BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
} }
else if (this.definitionWrapper.isWritableProperty(property)) { else if (this.definitionWrapper.isWritableProperty(property)) {
this.definitionWrapper.setPropertyValue(property, newValue); this.definitionWrapper.setPropertyValue(property, newValue);
} }
else { else {
super.setProperty(property, newValue); super.setProperty(property, newValue);
} }
} }
} }
} }

View File

@@ -23,27 +23,27 @@ import org.springframework.context.support.GenericGroovyApplicationContext
* @author Jeff Brown * @author Jeff Brown
*/ */
class GroovyApplicationContextDynamicBeanPropertyTests extends GroovyTestCase { class GroovyApplicationContextDynamicBeanPropertyTests extends GroovyTestCase {
void testAccessDynamicBeanProperties() {
def ctx = new GenericGroovyApplicationContext();
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh()
def framework = ctx.framework void testAccessDynamicBeanProperties() {
assertNotNull 'could not find framework bean', framework
assertEquals 'Grails', framework
}
void testAccessingNonExistentBeanViaDynamicProperty() {
def ctx = new GenericGroovyApplicationContext(); def ctx = new GenericGroovyApplicationContext();
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy"); ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh() ctx.refresh()
def err = shouldFail(NoSuchBeanDefinitionException) { def framework = ctx.framework
ctx.someNonExistentBean assertNotNull 'could not find framework bean', framework
} assertEquals 'Grails', framework
}
assertEquals "No bean named 'someNonExistentBean' is defined", err
} void testAccessingNonExistentBeanViaDynamicProperty() {
def ctx = new GenericGroovyApplicationContext();
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
ctx.refresh()
def err = shouldFail(NoSuchBeanDefinitionException) {
ctx.someNonExistentBean
}
assertEquals "No bean named 'someNonExistentBean' is defined", err
}
} }

View File

@@ -35,32 +35,32 @@ public class GroovyApplicationContextTests extends TestCase {
assertEquals("Grails", framework); assertEquals("Grails", framework);
} }
public void testLoadingMultipleConfigFiles() { public void testLoadingMultipleConfigFiles() {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext( GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
"org/springframework/context/groovy/applicationContext2.groovy", "org/springframework/context/groovy/applicationContext2.groovy",
"org/springframework/context/groovy/applicationContext.groovy"); "org/springframework/context/groovy/applicationContext.groovy");
Object framework = ctx.getBean("framework"); Object framework = ctx.getBean("framework");
assertNotNull("could not find framework bean", framework); assertNotNull("could not find framework bean", framework);
assertEquals("Grails", framework); assertEquals("Grails", framework);
Object company = ctx.getBean("company"); Object company = ctx.getBean("company");
assertNotNull("could not find company bean", company); assertNotNull("could not find company bean", company);
assertEquals("SpringSource", company); assertEquals("SpringSource", company);
} }
public void testLoadingMultipleConfigFilesWithRelativeClass() { public void testLoadingMultipleConfigFilesWithRelativeClass() {
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(); GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy"); ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy");
ctx.refresh(); ctx.refresh();
Object framework = ctx.getBean("framework"); Object framework = ctx.getBean("framework");
assertNotNull("could not find framework bean", framework); assertNotNull("could not find framework bean", framework);
assertEquals("Grails", framework); assertEquals("Grails", framework);
Object company = ctx.getBean("company"); Object company = ctx.getBean("company");
assertNotNull("could not find company bean", company); assertNotNull("could not find company bean", company);
assertEquals("SpringSource", company); assertEquals("SpringSource", company);
} }
} }