diff --git a/src/main/java/org/springframework/data/mapping/Parameter.java b/src/main/java/org/springframework/data/mapping/Parameter.java index bc2ad1414..50bb28385 100644 --- a/src/main/java/org/springframework/data/mapping/Parameter.java +++ b/src/main/java/org/springframework/data/mapping/Parameter.java @@ -91,9 +91,10 @@ public class Parameter> { } /** - * Returns the name of the parameter. + * Returns the name of the parameter (through constructor/method parameter naming). * - * @return + * @return the name of the parameter. + * @see org.springframework.core.ParameterNameDiscoverer */ @Nullable public String getName() { @@ -101,10 +102,22 @@ public class Parameter> { } /** - * Returns the required parameter name. + * Returns whether the parameter has a name. + * + * @return whether the parameter has a name. + * @since 3.5 + */ + public boolean hasName() { + return this.name != null; + } + + /** + * Returns the required name of the parameter (through constructor/method parameter naming) or throws + * {@link IllegalStateException} if the parameter has no name. * - * @return the parameter name or throws {@link IllegalStateException} if the parameter does not have a name + * @return the parameter name or throws {@link IllegalStateException} if the parameter does not have a name. * @since 3.5 + * @see org.springframework.core.ParameterNameDiscoverer */ public String getRequiredName() { @@ -115,16 +128,6 @@ public class Parameter> { return getName(); } - /** - * Returns whether the parameter has a name. - * - * @return whether the parameter has a name - * @since 3.5 - */ - public boolean hasName() { - return this.name != null; - } - /** * Returns the {@link TypeInformation} of the parameter. * diff --git a/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java b/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java index 53a0a1aec..f447f6f64 100644 --- a/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java +++ b/src/main/java/org/springframework/data/mapping/model/InstantiationAwarePropertyAccessor.java @@ -95,7 +95,7 @@ public class InstantiationAwarePropertyAccessor implements PersistentProperty creator.getParameters().forEach(it -> { - if (it.getName() == null) { + if (!it.hasName()) { throw new IllegalStateException( String.format("Cannot detect parameter names of copy creator of %s", owner.getType())); } diff --git a/src/main/java/org/springframework/data/repository/query/Parameter.java b/src/main/java/org/springframework/data/repository/query/Parameter.java index 2f2bdeadc..03b1a4f97 100644 --- a/src/main/java/org/springframework/data/repository/query/Parameter.java +++ b/src/main/java/org/springframework/data/repository/query/Parameter.java @@ -165,10 +165,23 @@ public class Parameter { return !isSpecialParameter() && getName().isPresent(); } + /** + * Returns whether the parameter is named explicitly, i.e. annotated with {@link Param}. + * + * @return + * @since 1.11 + * @see Param + */ + public boolean isExplicitlyNamed() { + return parameter.hasParameterAnnotation(Param.class); + } + /** * Returns the name of the parameter (through {@link Param} annotation or method parameter naming). * * @return the optional name of the parameter. + * @see Param + * @see org.springframework.core.ParameterNameDiscoverer */ public Optional getName() { return this.name.get(); @@ -181,6 +194,8 @@ public class Parameter { * @return the required parameter name. * @throws IllegalStateException if the parameter has no name. * @since 3.4 + * @see Param + * @see org.springframework.core.ParameterNameDiscoverer */ public String getRequiredName() { @@ -197,15 +212,6 @@ public class Parameter { return parameterType; } - /** - * Returns whether the parameter is named explicitly, i.e. annotated with {@link Param}. - * - * @return - * @since 1.11 - */ - public boolean isExplicitlyNamed() { - return parameter.hasParameterAnnotation(Param.class); - } @Override public String toString() { diff --git a/src/main/java/org/springframework/data/repository/query/ReturnedType.java b/src/main/java/org/springframework/data/repository/query/ReturnedType.java index 840eff42b..754a45eda 100644 --- a/src/main/java/org/springframework/data/repository/query/ReturnedType.java +++ b/src/main/java/org/springframework/data/repository/query/ReturnedType.java @@ -303,8 +303,8 @@ public abstract class ReturnedType { List properties = new ArrayList<>(parameterCount); for (Parameter parameter : constructor.getParameters()) { - if (parameter.getName() != null) { - properties.add(parameter.getName()); + if (parameter.hasName()) { + properties.add(parameter.getRequiredName()); } }