Skip to content

Commit

Permalink
GRAILS-4995 - Improve the handling of List and Map ctor args
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Brown committed Aug 27, 2012
1 parent a6879b1 commit fbcdc57
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ else if(args[0] instanceof Closure) {
protected List resolveConstructorArguments(Object[] args, int start, int end) {
Object[] constructorArgs = subarray(args, start, end);
filterGStringReferences(constructorArgs);
for(int i = 0; i < constructorArgs.length; i++) {
if(constructorArgs[i] instanceof List) {
constructorArgs[i] = manageListIfNecessary(constructorArgs[i]);
} else if(constructorArgs[i] instanceof Map){
constructorArgs[i] = manageMapIfNecessary(constructorArgs[i]);
}
}
List constructorArgsList = Arrays.asList(constructorArgs);
return constructorArgsList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,7 @@ beanReader.createApplicationContext()
assertEquals "Fred", appCtx.getBean("personA").name
}

// test for GRAILS-4995
void testListOfBeansAsConstructorArg() {
if(notYetImplemented()) return
def beanReader = new GroovyBeanDefinitionReader()

beanReader.beans {
Expand All @@ -790,6 +788,34 @@ beanReader.createApplicationContext()
assert ctx.containsBean('someotherbean2')
assert ctx.containsBean('somebean')
}

void testBeanWithListAndMapConstructor() {
def beanReader = new GroovyBeanDefinitionReader()
beanReader.beans {
bart(Bean1) {
person = "bart"
age = 11
}
lisa(Bean1) {
person = "lisa"
age = 9
}

beanWithList(Bean5, [bart, lisa])

// test runtime references both as ref() and as plain name
beanWithMap(Bean6, [bart:bart, lisa:ref('lisa')])
}
def ctx = beanReader.createApplicationContext()

def beanWithList = ctx.getBean("beanWithList")
assertEquals 2, beanWithList.people.size()
assertEquals "bart", beanWithList.people[0].person

def beanWithMap = ctx.getBean("beanWithMap")
assertEquals 9, beanWithMap.peopleByName.lisa.age
assertEquals "bart", beanWithMap.peopleByName.bart.person
}

void testAnonymousInnerBeanViaBeanMethod() {
def beanReader = new GroovyBeanDefinitionReader()
Expand Down Expand Up @@ -903,6 +929,20 @@ class Bean4 {
}
String person
}
// bean with List-valued constructor arg
class Bean5 {
Bean5(List<Bean1> people) {
this.people = people
}
List<Bean1> people
}
// bean with Map-valued constructor arg
class Bean6 {
Bean6(Map<String, Bean1> peopleByName) {
this.peopleByName = peopleByName
}
Map<String, Bean1> peopleByName
}
// a factory bean
class Bean1Factory {
Bean1 newInstance() {
Expand Down

0 comments on commit fbcdc57

Please sign in to comment.