Browse Source

DATACMNS-1082 - Skip synthetic constructors.

Constructor discovery no longer considers synthetic constructors for preferred constructor.

Original pull request: #225
pull/227/head
Roman Rodov 9 years ago committed by Jens Schauder
parent
commit
90fb97bcf2
  1. 8
      src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java
  2. 29
      src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java

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

@ -1,5 +1,5 @@ @@ -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; @@ -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>> @@ -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;

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

@ -32,6 +32,7 @@ import org.springframework.data.util.ClassTypeInformation; @@ -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 @@ -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 {
}

Loading…
Cancel
Save