Skip to content

Commit 90fb97b

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 679f30e commit 90fb97b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 by the original author(s).
2+
* Copyright 2011-2017 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* Helper class to find a {@link PreferredConstructor}.
3333
*
3434
* @author Oliver Gierke
35+
* @author Roman Rodov
3536
*/
3637
public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
3738

@@ -73,6 +74,11 @@ protected PreferredConstructorDiscoverer(TypeInformation<T> type, PersistentEnti
7374

7475
PreferredConstructor<T, P> preferredConstructor = buildPreferredConstructor(candidate, type, entity);
7576

77+
// Synthetic constructors should not be considered
78+
if (preferredConstructor.getConstructor().isSynthetic()) {
79+
continue;
80+
}
81+
7682
// Explicitly defined constructor trumps all
7783
if (preferredConstructor.isExplicitlyAnnotated()) {
7884
this.constructor = preferredConstructor;

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

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

@@ -97,6 +98,34 @@ public void discoversInnerClassConstructorCorrectly() {
9798
assertThat(constructor.isEnclosingClassParameter(parameter), is(true));
9899
}
99100

101+
@Test // DATACMNS-1082
102+
public void skipsSyntheticConstructor() {
103+
104+
PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<SyntheticConstructor, P>(
105+
ClassTypeInformation.from(SyntheticConstructor.class));
106+
PreferredConstructorDiscoverer<SyntheticConstructor, P> discoverer = //
107+
new PreferredConstructorDiscoverer<SyntheticConstructor, P>(entity);
108+
109+
PreferredConstructor<SyntheticConstructor, P> constructor = discoverer.getConstructor();
110+
111+
PersistenceConstructor annotation = constructor.getConstructor().getAnnotation(PersistenceConstructor.class);
112+
assertNotNull("The preferred constructor should have 'PersistenctConstructor' annotation.", annotation);
113+
assertFalse("The preferred constructor must not be synthetic", constructor.getConstructor().isSynthetic());
114+
}
115+
116+
static class SyntheticConstructor {
117+
@PersistenceConstructor
118+
private SyntheticConstructor(String x) {}
119+
120+
class InnerSynthetic {
121+
// Compiler will generate a synthetic constructor since
122+
// SyntheticConstructor() is private.
123+
InnerSynthetic() {
124+
new SyntheticConstructor("");
125+
}
126+
}
127+
}
128+
100129
static class EntityWithoutConstructor {
101130

102131
}

0 commit comments

Comments
 (0)