Skip to content

DATACMNS-1082: skip synthetic constructors #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Roman Rodov
*/
public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {

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

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

/**
* Unit tests for {@link PreferredConstructorDiscoverer}.
*
*
* @author Oliver Gierke
* @author Roman Rodov
*/
public class PreferredConstructorDiscovererUnitTests<P extends PersistentProperty<P>> {

Expand Down Expand Up @@ -106,6 +107,34 @@ public void discoversInnerClassConstructorCorrectly() {
});
}

@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 {

}
Expand Down