|
24 | 24 | import java.util.Optional;
|
25 | 25 |
|
26 | 26 | import org.springframework.core.MethodParameter;
|
| 27 | +import org.springframework.core.ParameterNameDiscoverer; |
27 | 28 | import org.springframework.core.ResolvableType;
|
28 | 29 | import org.springframework.data.domain.Limit;
|
29 | 30 | import org.springframework.data.domain.Pageable;
|
@@ -59,7 +60,8 @@ public class Parameter {
|
59 | 60 |
|
60 | 61 | static {
|
61 | 62 |
|
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)); |
63 | 65 |
|
64 | 66 | // consider Kotlin Coroutines Continuation a special parameter. That parameter is synthetic and should not get
|
65 | 67 | // bound to any query.
|
@@ -153,23 +155,39 @@ public int getIndex() {
|
153 | 155 | }
|
154 | 156 |
|
155 | 157 | /**
|
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. |
157 | 159 | *
|
158 | 160 | * @return
|
| 161 | + * @see Param |
| 162 | + * @see ParameterNameDiscoverer |
159 | 163 | */
|
160 | 164 | public boolean isNamedParameter() {
|
161 | 165 | return !isSpecialParameter() && getName().isPresent();
|
162 | 166 | }
|
163 | 167 |
|
164 | 168 | /**
|
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). |
166 | 170 | *
|
167 |
| - * @return |
| 171 | + * @return the optional name of the parameter. |
168 | 172 | */
|
169 | 173 | public Optional<String> getName() {
|
170 | 174 | return this.name.get();
|
171 | 175 | }
|
172 | 176 |
|
| 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 | + |
173 | 191 | /**
|
174 | 192 | * Returns the type of the {@link Parameter}.
|
175 | 193 | *
|
|
0 commit comments