Browse Source

DATACMNS-1082 - Skip synthetic constructors.

Constructor discovery no longer considers synthetic constructors for preferred constructor.

Original pull request: #225
pull/221/merge
Roman Rodov 9 years ago committed by Jens Schauder
parent
commit
cabfa287cb
  1. 6
      src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java
  2. 29
      src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java

6
src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

@ -34,6 +34,7 @@ import org.springframework.data.util.TypeInformation; @@ -34,6 +34,7 @@ import org.springframework.data.util.TypeInformation;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Roman Rodov
*/
public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
@ -75,6 +76,11 @@ public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> @@ -75,6 +76,11 @@ public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>>
PreferredConstructor<T, P> preferredConstructor = buildPreferredConstructor(candidate, type, entity);
// Synthetic constructors should not be considered
if (preferredConstructor.getConstructor().isSynthetic()) {
continue;
}
// Explicitly defined constructor trumps all
if (preferredConstructor.isExplicitlyAnnotated()) {
this.constructor = Optional.of(preferredConstructor);

29
src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java

@ -31,6 +31,7 @@ import org.springframework.data.util.ClassTypeInformation; @@ -31,6 +31,7 @@ import org.springframework.data.util.ClassTypeInformation;
* Unit tests for {@link PreferredConstructorDiscoverer}.
*
* @author Oliver Gierke
* @author Roman Rodov
*/
public class PreferredConstructorDiscovererUnitTests<P extends PersistentProperty<P>> {
@ -106,6 +107,34 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert @@ -106,6 +107,34 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
});
}
@Test // DATACMNS-1082
public void skipsSyntheticConstructor() {
PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<>(ClassTypeInformation.from(SyntheticConstructor.class));
PreferredConstructorDiscoverer<SyntheticConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(entity);
assertThat(discoverer.getConstructor()).hasValueSatisfying(constructor -> {
PersistenceConstructor annotation = constructor.getConstructor().getAnnotation(PersistenceConstructor.class);
assertThat(annotation).isNotNull();
assertThat(constructor.getConstructor().isSynthetic()).isFalse();
});
}
static class SyntheticConstructor {
@PersistenceConstructor
private SyntheticConstructor(String x) {
}
class InnerSynthetic {
// Compiler will generate a synthetic constructor since
// SyntheticConstructor() is private.
InnerSynthetic() {
new SyntheticConstructor("");
}
}
}
static class EntityWithoutConstructor {
}

Loading…
Cancel
Save