Browse Source

DATACMNS-56 - PreferredConstructorDiscoverer does not throw an exception in case no preferred constructor is found.

pull/4/head
Oliver Gierke 15 years ago
parent
commit
2c538dbb15
  1. 2
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/PersistentEntity.java
  2. 18
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java
  3. 1
      spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersistentEntitySpec.java
  4. 7
      spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java

2
spring-data-commons-core/src/main/java/org/springframework/data/mapping/PersistentEntity.java

@ -21,7 +21,7 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> { @@ -21,7 +21,7 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> {
/**
* Returns the {@link PreferredConstructor} to be used to instantiate objects of this {@link PersistentEntity}.
*
* @return must never return {@literal null}.
* @return {@literal null} in case no suitable constructor for automatic construction can be found.
*/
PreferredConstructor<T> getPreferredConstructor();

18
spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

@ -50,8 +50,6 @@ public class PreferredConstructorDiscoverer<T> { @@ -50,8 +50,6 @@ public class PreferredConstructorDiscoverer<T> {
*/
protected PreferredConstructorDiscoverer(TypeInformation<T> owningType) {
boolean noArgConstructorFound = false;
int numberOfArgConstructors = 0;
Class<?> rawOwningType = owningType.getType();
for (Constructor<?> constructor : rawOwningType.getDeclaredConstructors()) {
@ -65,24 +63,10 @@ public class PreferredConstructorDiscoverer<T> { @@ -65,24 +63,10 @@ public class PreferredConstructorDiscoverer<T> {
return;
}
// No-arg constructor trumps custom ones
if (this.constructor == null || preferredConstructor.isNoArgConstructor()) {
this.constructor = preferredConstructor;
}
if (preferredConstructor.isNoArgConstructor()) {
noArgConstructorFound = true;
} else {
numberOfArgConstructors++;
this.constructor = preferredConstructor;
}
}
if (!noArgConstructorFound && numberOfArgConstructors > 1) {
throw new IllegalArgumentException(
String.format("Multiple constructors with arguments found in class %s! Annotate " +
"one with @PersistenceConstructor explicitly to select it to be used in " +
"persistence operations.", rawOwningType.getName()));
}
}

1
spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersistentEntitySpec.java

@ -28,6 +28,5 @@ public abstract class PersistentEntitySpec { @@ -28,6 +28,5 @@ public abstract class PersistentEntitySpec {
public static void assertInvariants(PersistentEntity<?, ?> entity) {
assertThat(entity.getName(), is(notNullValue()));
assertThat(entity.getPreferredConstructor(), is(notNullValue()));
}
}

7
spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java

@ -64,11 +64,12 @@ public class PreferredConstructorDiscovererUnitTests { @@ -64,11 +64,12 @@ public class PreferredConstructorDiscovererUnitTests {
}
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() {
@Test
public void doesNotThrowExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() {
new PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne>(
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne> discoverer = new PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne>(
ClassWithMultipleConstructorsWithoutEmptyOne.class);
assertThat(discoverer.getConstructor(), is(nullValue()));
}
@Test

Loading…
Cancel
Save