Skip to content

Commit cabfa28

Browse files
strelok1schauder
authored andcommitted
DATACMNS-1082 - Skip synthetic constructors.
Constructor discovery no longer considers synthetic constructors for preferred constructor. Original pull request: #225
1 parent 11c1ad8 commit cabfa28

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* @author Oliver Gierke
3636
* @author Christoph Strobl
37+
* @author Roman Rodov
3738
*/
3839
public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
3940

@@ -75,6 +76,11 @@ protected PreferredConstructorDiscoverer(TypeInformation<T> type, Optional<Persi
7576

7677
PreferredConstructor<T, P> preferredConstructor = buildPreferredConstructor(candidate, type, entity);
7778

79+
// Synthetic constructors should not be considered
80+
if (preferredConstructor.getConstructor().isSynthetic()) {
81+
continue;
82+
}
83+
7884
// Explicitly defined constructor trumps all
7985
if (preferredConstructor.isExplicitlyAnnotated()) {
8086
this.constructor = Optional.of(preferredConstructor);

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

+29
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Unit tests for {@link PreferredConstructorDiscoverer}.
3232
*
3333
* @author Oliver Gierke
34+
* @author Roman Rodov
3435
*/
3536
public class PreferredConstructorDiscovererUnitTests<P extends PersistentProperty<P>> {
3637

@@ -106,6 +107,34 @@ public void discoversInnerClassConstructorCorrectly() {
106107
});
107108
}
108109

110+
@Test // DATACMNS-1082
111+
public void skipsSyntheticConstructor() {
112+
113+
PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<>(ClassTypeInformation.from(SyntheticConstructor.class));
114+
PreferredConstructorDiscoverer<SyntheticConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(entity);
115+
116+
assertThat(discoverer.getConstructor()).hasValueSatisfying(constructor -> {
117+
118+
PersistenceConstructor annotation = constructor.getConstructor().getAnnotation(PersistenceConstructor.class);
119+
assertThat(annotation).isNotNull();
120+
assertThat(constructor.getConstructor().isSynthetic()).isFalse();
121+
});
122+
}
123+
124+
static class SyntheticConstructor {
125+
@PersistenceConstructor
126+
private SyntheticConstructor(String x) {
127+
}
128+
129+
class InnerSynthetic {
130+
// Compiler will generate a synthetic constructor since
131+
// SyntheticConstructor() is private.
132+
InnerSynthetic() {
133+
new SyntheticConstructor("");
134+
}
135+
}
136+
}
137+
109138
static class EntityWithoutConstructor {
110139

111140
}

0 commit comments

Comments
 (0)