Skip to content

Commit 7148415

Browse files
committed
Introduce Parameter.getRequiredName() method.
Add getRequiredName to shortcut parameter name discovery and fast-fail in cases where the parameter name is required. Closes #3124
1 parent 9ab34d8 commit 7148415

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/main/java/org/springframework/data/repository/query/Parameter.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Optional;
2525

2626
import org.springframework.core.MethodParameter;
27+
import org.springframework.core.ParameterNameDiscoverer;
2728
import org.springframework.core.ResolvableType;
2829
import org.springframework.data.domain.Limit;
2930
import org.springframework.data.domain.Pageable;
@@ -59,7 +60,8 @@ public class Parameter {
5960

6061
static {
6162

62-
List<Class<?>> types = new ArrayList<>(Arrays.asList(ScrollPosition.class, Pageable.class, Sort.class, Limit.class));
63+
List<Class<?>> types = new ArrayList<>(
64+
Arrays.asList(ScrollPosition.class, Pageable.class, Sort.class, Limit.class));
6365

6466
// consider Kotlin Coroutines Continuation a special parameter. That parameter is synthetic and should not get
6567
// bound to any query.
@@ -153,23 +155,39 @@ public int getIndex() {
153155
}
154156

155157
/**
156-
* Returns whether the parameter is annotated with {@link Param}.
158+
* Returns whether the parameter is annotated with {@link Param} or has a method parameter name.
157159
*
158160
* @return
161+
* @see Param
162+
* @see ParameterNameDiscoverer
159163
*/
160164
public boolean isNamedParameter() {
161165
return !isSpecialParameter() && getName().isPresent();
162166
}
163167

164168
/**
165-
* Returns the name of the parameter (through {@link Param} annotation).
169+
* Returns the name of the parameter (through {@link Param} annotation or method parameter naming).
166170
*
167-
* @return
171+
* @return the optional name of the parameter.
168172
*/
169173
public Optional<String> getName() {
170174
return this.name.get();
171175
}
172176

177+
/**
178+
* Returns the required name of the parameter (through {@link Param} annotation or method parameter naming) or throws
179+
* {@link IllegalStateException} if the parameter has no name.
180+
*
181+
* @return the required parameter name.
182+
* @throws IllegalStateException if the parameter has no name.
183+
* @since 3.4
184+
*/
185+
public String getRequiredName() {
186+
187+
return getName().orElseThrow(() -> new IllegalStateException("Parameter " + parameter
188+
+ " is not named. For queries with named parameters you need to provide names for method parameters; Use @Param for query method parameters, or use the javac flag -parameters."));
189+
}
190+
173191
/**
174192
* Returns the type of the {@link Parameter}.
175193
*

src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,13 @@ void doesNotRejectParameterIfPageableComesFirst() throws Exception {
128128
getParametersFor("validWithPageableFirst", Pageable.class, String.class);
129129
}
130130

131-
@Test // DATACMNS-731
131+
@Test // DATACMNS-731, GH-3124
132132
void detectsExplicitlyNamedParameter() throws Exception {
133133

134134
var parameter = getParametersFor("valid", String.class).getBindableParameter(0);
135135

136-
assertThat(parameter.getName()).isNotNull();
136+
assertThat(parameter.getName()).isNotEmpty();
137+
assertThat(parameter.getRequiredName()).isNotNull();
137138
assertThat(parameter.isExplicitlyNamed()).isTrue();
138139
}
139140

@@ -145,7 +146,7 @@ void doesNotConsiderParameterExplicitlyNamedEvenIfNamePresent() throws Exception
145146
var methodParameter = ReflectionTestUtils.getField(parameter, "parameter");
146147
ReflectionTestUtils.setField(methodParameter, "parameterName", "name");
147148

148-
assertThat(parameter.getName()).isNotNull();
149+
assertThat(parameter.getName()).isNotEmpty();
149150
assertThat(parameter.isExplicitlyNamed()).isFalse();
150151
}
151152

0 commit comments

Comments
 (0)