DATACMNS-56 - Tweak to algorithm of PreferredConstructorDiscoverer to pick up single argument-taking constructor.

This commit is contained in:
Oliver Gierke
2011-07-25 23:38:18 +02:00
parent 178b046f35
commit 30de1d55b7
2 changed files with 14 additions and 2 deletions

View File

@@ -50,6 +50,8 @@ public class PreferredConstructorDiscoverer<T> {
*/
protected PreferredConstructorDiscoverer(TypeInformation<T> owningType) {
boolean noArgConstructorFound = false;
int numberOfArgConstructors = 0;
Class<?> rawOwningType = owningType.getType();
for (Constructor<?> constructor : rawOwningType.getDeclaredConstructors()) {
@@ -63,9 +65,20 @@ public class PreferredConstructorDiscoverer<T> {
return;
}
if (preferredConstructor.isNoArgConstructor()) {
// No-arg constructor trumps custom ones
if (this.constructor == null || preferredConstructor.isNoArgConstructor()) {
this.constructor = preferredConstructor;
}
if (preferredConstructor.isNoArgConstructor()) {
noArgConstructorFound = true;
} else {
numberOfArgConstructors++;
}
}
if (!noArgConstructorFound && numberOfArgConstructors > 1) {
this.constructor = null;
}
}

View File

@@ -19,7 +19,6 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import org.springframework.data.annotation.PersistenceConstructor;