@@ -159,7 +159,9 @@ public static UriComponentsBuilder fromController(Class<?> controllerType) {
159
159
* @param controllerType the controller to build a URI for
160
160
* @return a UriComponentsBuilder instance (never {@code null})
161
161
*/
162
- public static UriComponentsBuilder fromController (UriComponentsBuilder builder , Class <?> controllerType ) {
162
+ public static UriComponentsBuilder fromController (UriComponentsBuilder builder ,
163
+ Class <?> controllerType ) {
164
+
163
165
builder = getBaseUrlToUse (builder );
164
166
String mapping = getTypeRequestMapping (controllerType );
165
167
return builder .path (mapping );
@@ -176,8 +178,11 @@ public static UriComponentsBuilder fromController(UriComponentsBuilder builder,
176
178
* @throws IllegalArgumentException if there is no matching or
177
179
* if there is more than one matching method
178
180
*/
179
- public static UriComponentsBuilder fromMethodName (Class <?> controllerType , String methodName , Object ... args ) {
180
- return fromMethodName (null , controllerType , methodName , args );
181
+ public static UriComponentsBuilder fromMethodName (Class <?> controllerType ,
182
+ String methodName , Object ... args ) {
183
+
184
+ Method method = getMethod (controllerType , methodName , args );
185
+ return fromMethodInternal (null , controllerType , method , args );
181
186
}
182
187
183
188
/**
@@ -198,7 +203,7 @@ public static UriComponentsBuilder fromMethodName(UriComponentsBuilder builder,
198
203
Class <?> controllerType , String methodName , Object ... args ) {
199
204
200
205
Method method = getMethod (controllerType , methodName , args );
201
- return fromMethod (builder , method , args );
206
+ return fromMethodInternal (builder , controllerType , method , args );
202
207
}
203
208
204
209
/**
@@ -232,12 +237,17 @@ public static UriComponentsBuilder fromMethodName(UriComponentsBuilder builder,
232
237
* controller.getAddressesForCountry("US")
233
238
* builder = MvcUriComponentsBuilder.fromMethodCall(controller);
234
239
* </pre>
235
- * @param invocationInfo either the value returned from a "mock" controller
240
+ * @param info either the value returned from a "mock" controller
236
241
* invocation or the "mock" controller itself after an invocation
237
242
* @return a UriComponents instance
238
243
*/
239
- public static UriComponentsBuilder fromMethodCall (Object invocationInfo ) {
240
- return fromMethodCall (null , invocationInfo );
244
+ public static UriComponentsBuilder fromMethodCall (Object info ) {
245
+ Assert .isInstanceOf (MethodInvocationInfo .class , info );
246
+ MethodInvocationInfo invocationInfo = (MethodInvocationInfo ) info ;
247
+ Class <?> controllerType = invocationInfo .getControllerType ();
248
+ Method method = invocationInfo .getControllerMethod ();
249
+ Object [] arguments = invocationInfo .getArgumentValues ();
250
+ return fromMethodInternal (null , controllerType , method , arguments );
241
251
}
242
252
243
253
/**
@@ -247,14 +257,17 @@ public static UriComponentsBuilder fromMethodCall(Object invocationInfo) {
247
257
* request or to apply a custom baseUrl not matching the current request.
248
258
* @param builder the builder for the base URL; the builder will be cloned
249
259
* and therefore not modified and may be re-used for further calls.
250
- * @param invocationInfo either the value returned from a "mock" controller
260
+ * @param info either the value returned from a "mock" controller
251
261
* invocation or the "mock" controller itself after an invocation
252
262
* @return a UriComponents instance
253
263
*/
254
- public static UriComponentsBuilder fromMethodCall (UriComponentsBuilder builder , Object invocationInfo ) {
255
- Assert .isInstanceOf (MethodInvocationInfo .class , invocationInfo );
256
- MethodInvocationInfo info = (MethodInvocationInfo ) invocationInfo ;
257
- return fromMethod (builder , info .getControllerMethod (), info .getArgumentValues ());
264
+ public static UriComponentsBuilder fromMethodCall (UriComponentsBuilder builder , Object info ) {
265
+ Assert .isInstanceOf (MethodInvocationInfo .class , info );
266
+ MethodInvocationInfo invocationInfo = (MethodInvocationInfo ) info ;
267
+ Class <?> controllerType = invocationInfo .getControllerType ();
268
+ Method method = invocationInfo .getControllerMethod ();
269
+ Object [] arguments = invocationInfo .getArgumentValues ();
270
+ return fromMethodInternal (builder , controllerType , method , arguments );
258
271
}
259
272
260
273
/**
@@ -330,7 +343,10 @@ public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder
330
343
throw new IllegalArgumentException ("No unique match for mapping mappingName " +
331
344
name + ": " + handlerMethods );
332
345
}
333
- return new MethodArgumentBuilder (builder , handlerMethods .get (0 ).getMethod ());
346
+ HandlerMethod handlerMethod = handlerMethods .get (0 );
347
+ Class <?> controllerType = handlerMethod .getBeanType ();
348
+ Method method = handlerMethod .getMethod ();
349
+ return new MethodArgumentBuilder (builder , controllerType , method );
334
350
}
335
351
336
352
/**
@@ -341,12 +357,13 @@ public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder
341
357
* {@link org.springframework.web.method.support.UriComponentsContributor
342
358
* UriComponentsContributor}) while remaining argument values are ignored and
343
359
* can be {@code null}.
360
+ * @param controllerType the controller type
344
361
* @param method the controller method
345
362
* @param args argument values for the controller method
346
363
* @return a UriComponentsBuilder instance, never {@code null}
347
364
*/
348
- public static UriComponentsBuilder fromMethod (Method method , Object ... args ) {
349
- return fromMethod (null , method , args );
365
+ public static UriComponentsBuilder fromMethod (Class <?> controllerType , Method method , Object ... args ) {
366
+ return fromMethodInternal (null , controllerType , method , args );
350
367
}
351
368
352
369
/**
@@ -357,13 +374,32 @@ public static UriComponentsBuilder fromMethod(Method method, Object... args) {
357
374
* current request.
358
375
* @param baseUrl the builder for the base URL; the builder will be cloned
359
376
* and therefore not modified and may be re-used for further calls.
377
+ * @param controllerType the controller type
360
378
* @param method the controller method
361
379
* @param args argument values for the controller method
362
380
* @return a UriComponentsBuilder instance, never {@code null}
363
381
*/
364
- public static UriComponentsBuilder fromMethod (UriComponentsBuilder baseUrl , Method method , Object ... args ) {
382
+ public static UriComponentsBuilder fromMethod (UriComponentsBuilder baseUrl ,
383
+ Class <?> controllerType , Method method , Object ... args ) {
384
+
385
+ return fromMethodInternal (baseUrl , method .getDeclaringClass (), method , args );
386
+ }
387
+
388
+ /**
389
+ * See {@link #fromMethod(Class, Method, Object...)}.
390
+ * @deprecated as of 4.2 this is deprecated in favor of the overloaded
391
+ * method that also accepts a controllerType.
392
+ */
393
+ @ Deprecated
394
+ public static UriComponentsBuilder fromMethod (Method method , Object ... args ) {
395
+ return fromMethodInternal (null , method .getDeclaringClass (), method , args );
396
+ }
397
+
398
+ private static UriComponentsBuilder fromMethodInternal (UriComponentsBuilder baseUrl ,
399
+ Class <?> controllerType , Method method , Object ... args ) {
400
+
365
401
baseUrl = getBaseUrlToUse (baseUrl );
366
- String typePath = getTypeRequestMapping (method . getDeclaringClass () );
402
+ String typePath = getTypeRequestMapping (controllerType );
367
403
String methodPath = getMethodRequestMapping (method );
368
404
String path = pathMatcher .combine (typePath , methodPath );
369
405
baseUrl .path (path );
@@ -560,7 +596,7 @@ public static <T> T on(Class<T> controllerType) {
560
596
*/
561
597
public static <T > T controller (Class <T > controllerType ) {
562
598
Assert .notNull (controllerType , "'controllerType' must not be null" );
563
- return initProxy (controllerType , new ControllerMethodInvocationInterceptor ());
599
+ return initProxy (controllerType , new ControllerMethodInvocationInterceptor (controllerType ));
564
600
}
565
601
566
602
@ SuppressWarnings ("unchecked" )
@@ -617,11 +653,11 @@ public MethodArgumentBuilder withMappingName(String mappingName) {
617
653
}
618
654
619
655
/**
620
- * An alternative to {@link #fromMethod(java.lang.reflect. Method, Object...)}
656
+ * An alternative to {@link #fromMethod(Class, Method, Object...)}
621
657
* for use with an instance of this class created via {@link #relativeTo}.
622
658
*/
623
- public UriComponentsBuilder withMethod (Method method , Object ... args ) {
624
- return fromMethod (this .baseUrl , method , args );
659
+ public UriComponentsBuilder withMethod (Class <?> controllerType , Method method , Object ... args ) {
660
+ return fromMethod (this .baseUrl , controllerType , method , args );
625
661
}
626
662
627
663
@@ -634,11 +670,19 @@ private static class ControllerMethodInvocationInterceptor
634
670
private static final Method getArgumentValues =
635
671
ReflectionUtils .findMethod (MethodInvocationInfo .class , "getArgumentValues" );
636
672
673
+ private static final Method getControllerType =
674
+ ReflectionUtils .findMethod (MethodInvocationInfo .class , "getControllerType" );
675
+
637
676
private Method controllerMethod ;
638
677
639
678
private Object [] argumentValues ;
640
679
680
+ private Class <?> controllerType ;
641
681
682
+ ControllerMethodInvocationInterceptor (Class <?> controllerType ) {
683
+ this .controllerType = controllerType ;
684
+ }
685
+
642
686
@ Override
643
687
public Object intercept (Object obj , Method method , Object [] args , MethodProxy proxy ) {
644
688
if (getControllerMethod .equals (method )) {
@@ -647,6 +691,9 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
647
691
else if (getArgumentValues .equals (method )) {
648
692
return this .argumentValues ;
649
693
}
694
+ else if (getControllerType .equals (method )) {
695
+ return this .controllerType ;
696
+ }
650
697
else if (ReflectionUtils .isObjectMethod (method )) {
651
698
return ReflectionUtils .invokeMethod (method , obj , args );
652
699
}
@@ -670,32 +717,46 @@ public interface MethodInvocationInfo {
670
717
Method getControllerMethod ();
671
718
672
719
Object [] getArgumentValues ();
720
+
721
+ Class <?> getControllerType ();
673
722
}
674
723
675
724
676
725
public static class MethodArgumentBuilder {
677
726
727
+ private final Class <?> controllerType ;
728
+
678
729
private final Method method ;
679
730
680
731
private final Object [] argumentValues ;
681
732
682
733
private final UriComponentsBuilder baseUrl ;
683
734
684
735
685
- public MethodArgumentBuilder (Method method ) {
686
- this (null , method );
736
+ public MethodArgumentBuilder (Class <?> controllerType , Method method ) {
737
+ this (null , controllerType , method );
687
738
}
688
739
689
- public MethodArgumentBuilder (UriComponentsBuilder baseUrl , Method method ) {
740
+ public MethodArgumentBuilder (UriComponentsBuilder baseUrl , Class <?> controllerType , Method method ) {
741
+ Assert .notNull (controllerType , "'controllerType' is required" );
690
742
Assert .notNull (method , "'method' is required" );
691
743
this .baseUrl = baseUrl ;
744
+ this .controllerType = controllerType ;
692
745
this .method = method ;
693
746
this .argumentValues = new Object [method .getParameterTypes ().length ];
694
747
for (int i = 0 ; i < this .argumentValues .length ; i ++) {
695
748
this .argumentValues [i ] = null ;
696
749
}
697
750
}
698
751
752
+ /**
753
+ * @deprecated as of 4.2 deprecated in favor of alternative constructors
754
+ * that accept the controllerType.
755
+ */
756
+ @ Deprecated
757
+ public MethodArgumentBuilder (Method method ) {
758
+ this (method .getDeclaringClass (), method );
759
+ }
699
760
700
761
701
762
public MethodArgumentBuilder arg (int index , Object value ) {
@@ -704,13 +765,13 @@ public MethodArgumentBuilder arg(int index, Object value) {
704
765
}
705
766
706
767
public String build () {
707
- return MvcUriComponentsBuilder . fromMethod (this .baseUrl , this .method , this .argumentValues )
708
- .build (false ).encode ().toUriString ();
768
+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
769
+ this . argumentValues ) .build (false ).encode ().toUriString ();
709
770
}
710
771
711
- public String buildAndExpand (Object ... uriVariables ) {
712
- return MvcUriComponentsBuilder . fromMethod (this .baseUrl , this .method , this .argumentValues )
713
- . build (false ).expand (uriVariables ).encode ().toString ();
772
+ public String buildAndExpand (Object ... uriVars ) {
773
+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
774
+ this . argumentValues ). build (false ).expand (uriVars ).encode ().toString ();
714
775
}
715
776
}
716
777
0 commit comments