@@ -159,7 +159,9 @@ public static UriComponentsBuilder fromController(Class<?> controllerType) {
159159 * @param controllerType the controller to build a URI for
160160 * @return a UriComponentsBuilder instance (never {@code null})
161161 */
162- public static UriComponentsBuilder fromController (UriComponentsBuilder builder , Class <?> controllerType ) {
162+ public static UriComponentsBuilder fromController (UriComponentsBuilder builder ,
163+ Class <?> controllerType ) {
164+
163165 builder = getBaseUrlToUse (builder );
164166 String mapping = getTypeRequestMapping (controllerType );
165167 return builder .path (mapping );
@@ -176,8 +178,11 @@ public static UriComponentsBuilder fromController(UriComponentsBuilder builder,
176178 * @throws IllegalArgumentException if there is no matching or
177179 * if there is more than one matching method
178180 */
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 );
181186 }
182187
183188 /**
@@ -198,7 +203,7 @@ public static UriComponentsBuilder fromMethodName(UriComponentsBuilder builder,
198203 Class <?> controllerType , String methodName , Object ... args ) {
199204
200205 Method method = getMethod (controllerType , methodName , args );
201- return fromMethod (builder , method , args );
206+ return fromMethodInternal (builder , controllerType , method , args );
202207 }
203208
204209 /**
@@ -232,12 +237,17 @@ public static UriComponentsBuilder fromMethodName(UriComponentsBuilder builder,
232237 * controller.getAddressesForCountry("US")
233238 * builder = MvcUriComponentsBuilder.fromMethodCall(controller);
234239 * </pre>
235- * @param invocationInfo either the value returned from a "mock" controller
240+ * @param info either the value returned from a "mock" controller
236241 * invocation or the "mock" controller itself after an invocation
237242 * @return a UriComponents instance
238243 */
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 );
241251 }
242252
243253 /**
@@ -247,14 +257,17 @@ public static UriComponentsBuilder fromMethodCall(Object invocationInfo) {
247257 * request or to apply a custom baseUrl not matching the current request.
248258 * @param builder the builder for the base URL; the builder will be cloned
249259 * 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
251261 * invocation or the "mock" controller itself after an invocation
252262 * @return a UriComponents instance
253263 */
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 .getControllerType (), 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 );
258271 }
259272
260273 /**
@@ -330,7 +343,10 @@ public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder
330343 throw new IllegalArgumentException ("No unique match for mapping mappingName " +
331344 name + ": " + handlerMethods );
332345 }
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 );
334350 }
335351
336352 /**
@@ -341,12 +357,13 @@ public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder
341357 * {@link org.springframework.web.method.support.UriComponentsContributor
342358 * UriComponentsContributor}) while remaining argument values are ignored and
343359 * can be {@code null}.
360+ * @param controllerType the controller type
344361 * @param method the controller method
345362 * @param args argument values for the controller method
346363 * @return a UriComponentsBuilder instance, never {@code null}
347364 */
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 );
350367 }
351368
352369 /**
@@ -357,15 +374,30 @@ public static UriComponentsBuilder fromMethod(Method method, Object... args) {
357374 * current request.
358375 * @param baseUrl the builder for the base URL; the builder will be cloned
359376 * and therefore not modified and may be re-used for further calls.
377+ * @param controllerType the controller type
360378 * @param method the controller method
361379 * @param args argument values for the controller method
362380 * @return a UriComponentsBuilder instance, never {@code null}
363381 */
364- public static UriComponentsBuilder fromMethod (UriComponentsBuilder baseUrl , Method method , Object ... args ) {
365- return fromMethod (baseUrl , method .getDeclaringClass (), method , 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 );
366396 }
367-
368- private static UriComponentsBuilder fromMethod (UriComponentsBuilder baseUrl , Class <?> controllerType , Method method , Object ... args ) {
397+
398+ private static UriComponentsBuilder fromMethodInternal (UriComponentsBuilder baseUrl ,
399+ Class <?> controllerType , Method method , Object ... args ) {
400+
369401 baseUrl = getBaseUrlToUse (baseUrl );
370402 String typePath = getTypeRequestMapping (controllerType );
371403 String methodPath = getMethodRequestMapping (method );
@@ -621,11 +653,11 @@ public MethodArgumentBuilder withMappingName(String mappingName) {
621653 }
622654
623655 /**
624- * An alternative to {@link #fromMethod(java.lang.reflect. Method, Object...)}
656+ * An alternative to {@link #fromMethod(Class, Method, Object...)}
625657 * for use with an instance of this class created via {@link #relativeTo}.
626658 */
627- public UriComponentsBuilder withMethod (Method method , Object ... args ) {
628- return fromMethod (this .baseUrl , method , args );
659+ public UriComponentsBuilder withMethod (Class <?> controllerType , Method method , Object ... args ) {
660+ return fromMethod (this .baseUrl , controllerType , method , args );
629661 }
630662
631663
@@ -692,27 +724,39 @@ public interface MethodInvocationInfo {
692724
693725 public static class MethodArgumentBuilder {
694726
727+ private final Class <?> controllerType ;
728+
695729 private final Method method ;
696730
697731 private final Object [] argumentValues ;
698732
699733 private final UriComponentsBuilder baseUrl ;
700734
701735
702- public MethodArgumentBuilder (Method method ) {
703- this (null , method );
736+ public MethodArgumentBuilder (Class <?> controllerType , Method method ) {
737+ this (null , controllerType , method );
704738 }
705739
706- public MethodArgumentBuilder (UriComponentsBuilder baseUrl , Method method ) {
740+ public MethodArgumentBuilder (UriComponentsBuilder baseUrl , Class <?> controllerType , Method method ) {
741+ Assert .notNull (controllerType , "'controllerType' is required" );
707742 Assert .notNull (method , "'method' is required" );
708743 this .baseUrl = baseUrl ;
744+ this .controllerType = controllerType ;
709745 this .method = method ;
710746 this .argumentValues = new Object [method .getParameterTypes ().length ];
711747 for (int i = 0 ; i < this .argumentValues .length ; i ++) {
712748 this .argumentValues [i ] = null ;
713749 }
714750 }
715751
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+ }
716760
717761
718762 public MethodArgumentBuilder arg (int index , Object value ) {
@@ -721,13 +765,13 @@ public MethodArgumentBuilder arg(int index, Object value) {
721765 }
722766
723767 public String build () {
724- return MvcUriComponentsBuilder . fromMethod (this .baseUrl , this .method , this .argumentValues )
725- .build (false ).encode ().toUriString ();
768+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
769+ this . argumentValues ) .build (false ).encode ().toUriString ();
726770 }
727771
728- public String buildAndExpand (Object ... uriVariables ) {
729- return MvcUriComponentsBuilder . fromMethod (this .baseUrl , this .method , this .argumentValues )
730- . 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 ();
731775 }
732776 }
733777
0 commit comments