DATACMNS-56 - PreferredConstructorDiscoverer does not throw an exception in case no preferred constructor is found.
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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,23 +63,9 @@ public class PreferredConstructorDiscoverer<T> {
|
||||
return;
|
||||
}
|
||||
|
||||
// No-arg constructor trumps custom ones
|
||||
if (this.constructor == null || preferredConstructor.isNoArgConstructor()) {
|
||||
if (preferredConstructor.isNoArgConstructor()) {
|
||||
this.constructor = preferredConstructor;
|
||||
}
|
||||
|
||||
if (preferredConstructor.isNoArgConstructor()) {
|
||||
noArgConstructorFound = true;
|
||||
} else {
|
||||
numberOfArgConstructors++;
|
||||
}
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,5 @@ public abstract class PersistentEntitySpec {
|
||||
|
||||
public static void assertInvariants(PersistentEntity<?, ?> entity) {
|
||||
assertThat(entity.getName(), is(notNullValue()));
|
||||
assertThat(entity.getPreferredConstructor(), is(notNullValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user