Skip to content

Commit fbcdc57

Browse files
GRAILS-4995 - Improve the handling of List and Map ctor args
1 parent a6879b1 commit fbcdc57

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

spring-lang-groovy/src/main/java/org/springframework/context/groovy/GroovyBeanDefinitionReader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,13 @@ else if(args[0] instanceof Closure) {
714714
protected List resolveConstructorArguments(Object[] args, int start, int end) {
715715
Object[] constructorArgs = subarray(args, start, end);
716716
filterGStringReferences(constructorArgs);
717+
for(int i = 0; i < constructorArgs.length; i++) {
718+
if(constructorArgs[i] instanceof List) {
719+
constructorArgs[i] = manageListIfNecessary(constructorArgs[i]);
720+
} else if(constructorArgs[i] instanceof Map){
721+
constructorArgs[i] = manageMapIfNecessary(constructorArgs[i]);
722+
}
723+
}
717724
List constructorArgsList = Arrays.asList(constructorArgs);
718725
return constructorArgsList;
719726
}

spring-lang-groovy/src/test/groovy/org/springframework/context/groovy/GroovyBeanDefinitionReaderTests.groovy

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,7 @@ beanReader.createApplicationContext()
772772
assertEquals "Fred", appCtx.getBean("personA").name
773773
}
774774

775-
// test for GRAILS-4995
776775
void testListOfBeansAsConstructorArg() {
777-
if(notYetImplemented()) return
778776
def beanReader = new GroovyBeanDefinitionReader()
779777

780778
beanReader.beans {
@@ -790,6 +788,34 @@ beanReader.createApplicationContext()
790788
assert ctx.containsBean('someotherbean2')
791789
assert ctx.containsBean('somebean')
792790
}
791+
792+
void testBeanWithListAndMapConstructor() {
793+
def beanReader = new GroovyBeanDefinitionReader()
794+
beanReader.beans {
795+
bart(Bean1) {
796+
person = "bart"
797+
age = 11
798+
}
799+
lisa(Bean1) {
800+
person = "lisa"
801+
age = 9
802+
}
803+
804+
beanWithList(Bean5, [bart, lisa])
805+
806+
// test runtime references both as ref() and as plain name
807+
beanWithMap(Bean6, [bart:bart, lisa:ref('lisa')])
808+
}
809+
def ctx = beanReader.createApplicationContext()
810+
811+
def beanWithList = ctx.getBean("beanWithList")
812+
assertEquals 2, beanWithList.people.size()
813+
assertEquals "bart", beanWithList.people[0].person
814+
815+
def beanWithMap = ctx.getBean("beanWithMap")
816+
assertEquals 9, beanWithMap.peopleByName.lisa.age
817+
assertEquals "bart", beanWithMap.peopleByName.bart.person
818+
}
793819

794820
void testAnonymousInnerBeanViaBeanMethod() {
795821
def beanReader = new GroovyBeanDefinitionReader()
@@ -903,6 +929,20 @@ class Bean4 {
903929
}
904930
String person
905931
}
932+
// bean with List-valued constructor arg
933+
class Bean5 {
934+
Bean5(List<Bean1> people) {
935+
this.people = people
936+
}
937+
List<Bean1> people
938+
}
939+
// bean with Map-valued constructor arg
940+
class Bean6 {
941+
Bean6(Map<String, Bean1> peopleByName) {
942+
this.peopleByName = peopleByName
943+
}
944+
Map<String, Bean1> peopleByName
945+
}
906946
// a factory bean
907947
class Bean1Factory {
908948
Bean1 newInstance() {

0 commit comments

Comments
 (0)