diff --git a/src/main/groovy/util/ObjectGraphBuilder.java b/src/main/groovy/util/ObjectGraphBuilder.java index 47c59aef99..141e2fe56e 100644 --- a/src/main/groovy/util/ObjectGraphBuilder.java +++ b/src/main/groovy/util/ObjectGraphBuilder.java @@ -266,11 +266,11 @@ public void setNewInstanceResolver(final Object newInstanceResolver) { } else if (newInstanceResolver instanceof Closure) { final ObjectGraphBuilder self = this; this.newInstanceResolver = new NewInstanceResolver() { - public Object newInstance(Class klass, Map attributes) + public Object newInstance(Class klass, Object value, Map attributes) throws InstantiationException, IllegalAccessException { Closure cls = (Closure) newInstanceResolver; cls.setDelegate(self); - return cls.call(new Object[]{klass, attributes}); + return cls.call(new Object[]{klass, value, attributes}); } }; } else { @@ -466,7 +466,7 @@ public String getIdentifierFor(String nodeName) { * Default impl that calls Class.newInstance() */ public static class DefaultNewInstanceResolver implements NewInstanceResolver { - public Object newInstance(Class klass, Map attributes) throws InstantiationException, + public Object newInstance(Class klass, Object value, Map attributes) throws InstantiationException, IllegalAccessException { return klass.newInstance(); } @@ -539,7 +539,7 @@ public interface NewInstanceResolver { * @param klass the resolved class name * @param attributes the attribute Map available for the node */ - Object newInstance(Class klass, Map attributes) throws InstantiationException, + Object newInstance(Class klass, Object value, Map attributes) throws InstantiationException, IllegalAccessException; } @@ -665,7 +665,7 @@ protected Object resolveInstance(FactoryBuilderSupport builder, Object name, Obj return value; } - return ogbuilder.newInstanceResolver.newInstance(klass, properties); + return ogbuilder.newInstanceResolver.newInstance(klass, value, properties); } public void setChild(FactoryBuilderSupport builder, Object parent, Object child) { diff --git a/src/test/groovy/util/ObjectGraphBuilderTest.groovy b/src/test/groovy/util/ObjectGraphBuilderTest.groovy index 71f54b55f3..c9eca13aac 100644 --- a/src/test/groovy/util/ObjectGraphBuilderTest.groovy +++ b/src/test/groovy/util/ObjectGraphBuilderTest.groovy @@ -317,6 +317,24 @@ class ObjectGraphBuilderTest extends GroovyTestCase { reflectionBuilder = new ObjectGraphBuilder() reflectionBuilder.classNameResolver = [ name: 'reflection', root: "groovy.util" ] } + + void testGroovy6185() { + builder.newInstanceResolver = { klass, value, attributes -> + if (klass.simpleName == 'Company' && value instanceof String) { + klass.newInstance(name: value) + } else { + klass.newInstance() + } + } + + def expected = new Company(name: 'ACME', employees: []) + def actual = builder.company('ACME', employees: []) + assert actual != null + //assert actual.class == Company + assert actual.name == expected.name + assert actual.address == expected.address + assert actual.employees == expected.employees + } } class Company { @@ -374,4 +392,4 @@ class PetMonkey { String name String toString() { "PetMonkey=[name:${name}]" } -} +}