1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
+
24
25
import org .springframework .beans .factory .BeanFactory ;
25
26
import org .springframework .core .BridgeMethodResolver ;
26
27
import org .springframework .core .MethodParameter ;
29
30
import org .springframework .util .ClassUtils ;
30
31
31
32
/**
32
- * Encapsulates information about a bean method consisting of a
33
- * {@linkplain #getMethod() method} and a {@linkplain #getBean() bean}. Provides
34
- * convenient access to method parameters, the method return value, method
35
- * annotations.
33
+ * Encapsulates information about a handler method consisting of a {@linkplain #getMethod() method}
34
+ * and a {@linkplain #getBean() bean}. Provides convenient access to method parameters,
35
+ * method return value, method annotations.
36
36
*
37
- * <p>The class may be created with a bean instance or with a bean name (e.g. lazy
38
- * bean, prototype bean). Use {@link #createWithResolvedBean()} to obtain an
39
- * {@link HandlerMethod} instance with a bean instance initialized through the
40
- * bean factory.
37
+ * <p>The class may be created with a bean instance or with a bean name (e.g. lazy-init bean,
38
+ * prototype bean). Use {@link #createWithResolvedBean()} to obtain a {@link HandlerMethod}
39
+ * instance with a bean instance resolved through the associated {@link BeanFactory}.
41
40
*
42
41
* @author Arjen Poutsma
43
42
* @author Rossen Stoyanchev
@@ -50,48 +49,39 @@ public class HandlerMethod {
50
49
51
50
private final Object bean ;
52
51
53
- private final Method method ;
54
-
55
52
private final BeanFactory beanFactory ;
56
53
57
- private final MethodParameter [] parameters ;
54
+ private final Method method ;
58
55
59
56
private final Method bridgedMethod ;
60
57
58
+ private final MethodParameter [] parameters ;
59
+
61
60
62
61
/**
63
62
* Create an instance from a bean instance and a method.
64
63
*/
65
64
public HandlerMethod (Object bean , Method method ) {
66
- Assert .notNull (bean , "bean is required" );
67
- Assert .notNull (method , "method is required" );
65
+ Assert .notNull (bean , "Bean is required" );
66
+ Assert .notNull (method , "Method is required" );
68
67
this .bean = bean ;
69
68
this .beanFactory = null ;
70
69
this .method = method ;
71
70
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
72
71
this .parameters = initMethodParameters ();
73
72
}
74
73
75
- private MethodParameter [] initMethodParameters () {
76
- int count = this .bridgedMethod .getParameterTypes ().length ;
77
- MethodParameter [] result = new MethodParameter [count ];
78
- for (int i = 0 ; i < count ; i ++) {
79
- result [i ] = new HandlerMethodParameter (i );
80
- }
81
- return result ;
82
- }
83
-
84
74
/**
85
75
* Create an instance from a bean instance, method name, and parameter types.
86
76
* @throws NoSuchMethodException when the method cannot be found
87
77
*/
88
78
public HandlerMethod (Object bean , String methodName , Class <?>... parameterTypes ) throws NoSuchMethodException {
89
- Assert .notNull (bean , "bean is required" );
90
- Assert .notNull (methodName , "method is required" );
79
+ Assert .notNull (bean , "Bean is required" );
80
+ Assert .notNull (methodName , "Method name is required" );
91
81
this .bean = bean ;
92
82
this .beanFactory = null ;
93
83
this .method = bean .getClass ().getMethod (methodName , parameterTypes );
94
- this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
84
+ this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (this . method );
95
85
this .parameters = initMethodParameters ();
96
86
}
97
87
@@ -101,11 +91,11 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
101
91
* re-create the {@code HandlerMethod} with an initialized the bean.
102
92
*/
103
93
public HandlerMethod (String beanName , BeanFactory beanFactory , Method method ) {
104
- Assert .hasText (beanName , "beanName is required" );
105
- Assert .notNull (beanFactory , "beanFactory is required" );
106
- Assert .notNull (method , "method is required" );
94
+ Assert .hasText (beanName , "Bean name is required" );
95
+ Assert .notNull (beanFactory , "BeanFactory is required" );
96
+ Assert .notNull (method , "Method is required" );
107
97
Assert .isTrue (beanFactory .containsBean (beanName ),
108
- "Bean factory [" + beanFactory + "] does not contain bean [" + beanName + "]" );
98
+ "BeanFactory [" + beanFactory + "] does not contain bean [" + beanName + "]" );
109
99
this .bean = beanName ;
110
100
this .beanFactory = beanFactory ;
111
101
this .method = method ;
@@ -129,15 +119,25 @@ protected HandlerMethod(HandlerMethod handlerMethod) {
129
119
* Re-create HandlerMethod with the resolved handler.
130
120
*/
131
121
private HandlerMethod (HandlerMethod handlerMethod , Object handler ) {
132
- Assert .notNull (handlerMethod , "handlerMethod is required" );
133
- Assert .notNull (handler , "handler is required" );
122
+ Assert .notNull (handlerMethod , "HandlerMethod is required" );
123
+ Assert .notNull (handler , "Handler object is required" );
134
124
this .bean = handler ;
135
125
this .beanFactory = handlerMethod .beanFactory ;
136
126
this .method = handlerMethod .method ;
137
127
this .bridgedMethod = handlerMethod .bridgedMethod ;
138
128
this .parameters = handlerMethod .parameters ;
139
129
}
140
130
131
+
132
+ private MethodParameter [] initMethodParameters () {
133
+ int count = this .bridgedMethod .getParameterTypes ().length ;
134
+ MethodParameter [] result = new MethodParameter [count ];
135
+ for (int i = 0 ; i < count ; i ++) {
136
+ result [i ] = new HandlerMethodParameter (i );
137
+ }
138
+ return result ;
139
+ }
140
+
141
141
/**
142
142
* Returns the bean for this handler method.
143
143
*/
@@ -157,9 +157,8 @@ public Method getMethod() {
157
157
* Note that if the bean type is a CGLIB-generated class, the original, user-defined class is returned.
158
158
*/
159
159
public Class <?> getBeanType () {
160
- Class <?> clazz = (this .bean instanceof String )
161
- ? this .beanFactory .getType ((String ) this .bean ) : this .bean .getClass ();
162
-
160
+ Class <?> clazz = (this .bean instanceof String ?
161
+ this .beanFactory .getType ((String ) this .bean ) : this .bean .getClass ());
163
162
return ClassUtils .getUserClass (clazz );
164
163
}
165
164
@@ -223,33 +222,34 @@ public HandlerMethod createWithResolvedBean() {
223
222
}
224
223
225
224
@ Override
226
- public boolean equals (Object o ) {
227
- if (this == o ) {
225
+ public boolean equals (Object obj ) {
226
+ if (this == obj ) {
228
227
return true ;
229
228
}
230
- if (o != null && o instanceof HandlerMethod ) {
231
- HandlerMethod other = (HandlerMethod ) o ;
232
- return this .bean .equals (other .bean ) && this .method .equals (other .method );
229
+ if (obj != null && obj instanceof HandlerMethod ) {
230
+ HandlerMethod other = (HandlerMethod ) obj ;
231
+ return ( this .bean .equals (other .bean ) && this .method .equals (other .method ) );
233
232
}
234
233
return false ;
235
234
}
236
235
237
236
@ Override
238
237
public int hashCode () {
239
- return 31 * this .bean .hashCode () + this .method .hashCode ();
238
+ return this .bean .hashCode () * 31 + this .method .hashCode ();
240
239
}
241
240
242
241
@ Override
243
242
public String toString () {
244
- return method .toGenericString ();
243
+ return this . method .toGenericString ();
245
244
}
246
245
246
+
247
247
/**
248
248
* A MethodParameter with HandlerMethod-specific behavior.
249
249
*/
250
250
private class HandlerMethodParameter extends MethodParameter {
251
251
252
- protected HandlerMethodParameter (int index ) {
252
+ public HandlerMethodParameter (int index ) {
253
253
super (HandlerMethod .this .bridgedMethod , index );
254
254
}
255
255
@@ -264,6 +264,7 @@ public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) {
264
264
}
265
265
}
266
266
267
+
267
268
/**
268
269
* A MethodParameter for a HandlerMethod return type based on an actual return value.
269
270
*/
@@ -278,7 +279,7 @@ public ReturnValueMethodParameter(Object returnValue) {
278
279
279
280
@ Override
280
281
public Class <?> getParameterType () {
281
- return (this .returnValue != null ) ? this .returnValue .getClass () : super .getParameterType ();
282
+ return (this .returnValue != null ? this .returnValue .getClass () : super .getParameterType () );
282
283
}
283
284
}
284
285
0 commit comments