DATACMNS-1082 - Skip synthetic constructors.
Constructor discovery no longer considers synthetic constructors for preferred constructor. Original pull request: #225
This commit is contained in:
committed by
Jens Schauder
parent
679f30e109
commit
90fb97bcf2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2015 by the original author(s).
|
||||
* Copyright 2011-2017 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,6 +32,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
* Helper class to find a {@link PreferredConstructor}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Roman Rodov
|
||||
*/
|
||||
public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
|
||||
|
||||
@@ -73,6 +74,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 = preferredConstructor;
|
||||
|
||||
@@ -32,6 +32,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>> {
|
||||
|
||||
@@ -97,6 +98,34 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
assertThat(constructor.isEnclosingClassParameter(parameter), is(true));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1082
|
||||
public void skipsSyntheticConstructor() {
|
||||
|
||||
PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<SyntheticConstructor, P>(
|
||||
ClassTypeInformation.from(SyntheticConstructor.class));
|
||||
PreferredConstructorDiscoverer<SyntheticConstructor, P> discoverer = //
|
||||
new PreferredConstructorDiscoverer<SyntheticConstructor, P>(entity);
|
||||
|
||||
PreferredConstructor<SyntheticConstructor, P> constructor = discoverer.getConstructor();
|
||||
|
||||
PersistenceConstructor annotation = constructor.getConstructor().getAnnotation(PersistenceConstructor.class);
|
||||
assertNotNull("The preferred constructor should have 'PersistenctConstructor' annotation.", annotation);
|
||||
assertFalse("The preferred constructor must not be synthetic", constructor.getConstructor().isSynthetic());
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user