Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GROOVY-6158 ObjectGraphBuilder.NewInstanceResolver disregards node value #358

Open
wants to merge 1 commit into
base: GROOVY_2_2_X
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/groovy/util/ObjectGraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 19 additions & 1 deletion src/test/groovy/util/ObjectGraphBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -374,4 +392,4 @@ class PetMonkey {
String name

String toString() { "PetMonkey=[name:${name}]" }
}
}