1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
30
30
import org .springframework .util .ClassUtils ;
31
31
32
32
/**
33
- * Encapsulates information about a bean method consisting of a {@link #getMethod() method}
34
- * and a {@link #getBean() bean}. Provides convenient access to method parameters,
35
- * method return value, method annotations.
33
+ * Encapsulates information about a handler method consisting of a
34
+ * {@linkplain #getMethod() method} and a {@linkplain #getBean() bean}.
35
+ * Provides convenient access to method parameters, 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 bean,
38
- * prototype bean). Use {@link #createWithResolvedBean()} to obtain an {@link HandlerMethod}
39
- * instance with a bean instance initialized through the 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} .
40
40
*
41
41
* @author Arjen Poutsma
42
42
* @author Rossen Stoyanchev
43
+ * @author Juergen Hoeller
43
44
* @since 4.0
44
45
*/
45
46
public class HandlerMethod {
46
47
47
- protected final Log logger = LogFactory .getLog (HandlerMethod .class );
48
+ /** Logger that is available to subclasses */
49
+ protected final Log logger = LogFactory .getLog (getClass ());
48
50
49
51
private final Object bean ;
50
52
51
53
private final BeanFactory beanFactory ;
52
54
55
+ private final Class <?> beanType ;
56
+
53
57
private final Method method ;
54
58
55
59
private final Method bridgedMethod ;
@@ -65,6 +69,7 @@ public HandlerMethod(Object bean, Method method) {
65
69
Assert .notNull (method , "Method is required" );
66
70
this .bean = bean ;
67
71
this .beanFactory = null ;
72
+ this .beanType = ClassUtils .getUserClass (bean );
68
73
this .method = method ;
69
74
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
70
75
this .parameters = initMethodParameters ();
@@ -79,6 +84,7 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
79
84
Assert .notNull (methodName , "Method name is required" );
80
85
this .bean = bean ;
81
86
this .beanFactory = null ;
87
+ this .beanType = ClassUtils .getUserClass (bean );
82
88
this .method = bean .getClass ().getMethod (methodName , parameterTypes );
83
89
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (this .method );
84
90
this .parameters = initMethodParameters ();
@@ -93,22 +99,22 @@ public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
93
99
Assert .hasText (beanName , "Bean name is required" );
94
100
Assert .notNull (beanFactory , "BeanFactory is required" );
95
101
Assert .notNull (method , "Method is required" );
96
- Assert .isTrue (beanFactory .containsBean (beanName ),
97
- "BeanFactory [" + beanFactory + "] does not contain bean [" + beanName + "]" );
98
102
this .bean = beanName ;
99
103
this .beanFactory = beanFactory ;
104
+ this .beanType = ClassUtils .getUserClass (beanFactory .getType (beanName ));
100
105
this .method = method ;
101
106
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
102
107
this .parameters = initMethodParameters ();
103
108
}
104
109
105
110
/**
106
- * Copy constructor for use in sub-classes .
111
+ * Copy constructor for use in subclasses .
107
112
*/
108
113
protected HandlerMethod (HandlerMethod handlerMethod ) {
109
114
Assert .notNull (handlerMethod , "HandlerMethod is required" );
110
115
this .bean = handlerMethod .bean ;
111
116
this .beanFactory = handlerMethod .beanFactory ;
117
+ this .beanType = handlerMethod .beanType ;
112
118
this .method = handlerMethod .method ;
113
119
this .bridgedMethod = handlerMethod .bridgedMethod ;
114
120
this .parameters = handlerMethod .parameters ;
@@ -122,6 +128,7 @@ private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
122
128
Assert .notNull (handler , "Handler object is required" );
123
129
this .bean = handler ;
124
130
this .beanFactory = handlerMethod .beanFactory ;
131
+ this .beanType = handlerMethod .beanType ;
125
132
this .method = handlerMethod .method ;
126
133
this .bridgedMethod = handlerMethod .bridgedMethod ;
127
134
this .parameters = handlerMethod .parameters ;
@@ -152,18 +159,17 @@ public Method getMethod() {
152
159
}
153
160
154
161
/**
155
- * Returns the type of the handler for this handler method.
156
- * Note that if the bean type is a CGLIB-generated class, the original, user-defined class is returned.
162
+ * This method returns the type of the handler for this handler method.
163
+ * <p>Note that if the bean type is a CGLIB-generated class, the original
164
+ * user-defined class is returned.
157
165
*/
158
166
public Class <?> getBeanType () {
159
- Class <?> clazz = (this .bean instanceof String ?
160
- this .beanFactory .getType ((String ) this .bean ) : this .bean .getClass ());
161
- return ClassUtils .getUserClass (clazz );
167
+ return this .beanType ;
162
168
}
163
169
164
170
/**
165
- * If the bean method is a bridge method, this method returns the bridged (user-defined) method.
166
- * Otherwise it returns the same method as {@link #getMethod()}.
171
+ * If the bean method is a bridge method, this method returns the bridged
172
+ * (user-defined) method. Otherwise it returns the same method as {@link #getMethod()}.
167
173
*/
168
174
protected Method getBridgedMethod () {
169
175
return this .bridgedMethod ;
@@ -198,8 +204,8 @@ public boolean isVoid() {
198
204
}
199
205
200
206
/**
201
- * Returns a single annotation on the underlying method traversing its super methods if no
202
- * annotation can be found on the given method itself.
207
+ * Returns a single annotation on the underlying method traversing its super methods
208
+ * if no annotation can be found on the given method itself.
203
209
* @param annotationType the type of annotation to introspect the method for.
204
210
* @return the annotation, or {@code null} if none found
205
211
*/
@@ -208,8 +214,8 @@ public <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
208
214
}
209
215
210
216
/**
211
- * If the provided instance contains a bean name rather than an object instance, the bean name is resolved
212
- * before a {@link HandlerMethod} is created and returned.
217
+ * If the provided instance contains a bean name rather than an object instance,
218
+ * the bean name is resolved before a {@link HandlerMethod} is created and returned.
213
219
*/
214
220
public HandlerMethod createWithResolvedBean () {
215
221
Object handler = this .bean ;
@@ -220,26 +226,27 @@ public HandlerMethod createWithResolvedBean() {
220
226
return new HandlerMethod (this , handler );
221
227
}
222
228
229
+ public String getShortLogMessage () {
230
+ int args = this .method .getParameterTypes ().length ;
231
+ return getBeanType ().getName () + "#" + this .method .getName () + "[" + args + " args]" ;
232
+ }
233
+
234
+
223
235
@ Override
224
- public boolean equals (Object obj ) {
225
- if (this == obj ) {
236
+ public boolean equals (Object other ) {
237
+ if (this == other ) {
226
238
return true ;
227
239
}
228
- if (obj != null && obj instanceof HandlerMethod ) {
229
- HandlerMethod other = (HandlerMethod ) obj ;
230
- return this .bean .equals (other .bean ) && this .method .equals (other .method );
240
+ if (!(other instanceof HandlerMethod )) {
241
+ return false ;
231
242
}
232
- return false ;
243
+ HandlerMethod otherMethod = (HandlerMethod ) other ;
244
+ return (this .bean .equals (otherMethod .bean ) && this .method .equals (otherMethod .method ));
233
245
}
234
246
235
247
@ Override
236
248
public int hashCode () {
237
- return this .bean .hashCode () * 31 + this .method .hashCode ();
238
- }
239
-
240
- public String getShortLogMessage () {
241
- int args = method .getParameterTypes ().length ;
242
- return getBeanType ().getName () + "#" + this .method .getName () + "[" + args + " args]" ;
249
+ return (this .bean .hashCode () * 31 + this .method .hashCode ());
243
250
}
244
251
245
252
@ Override
0 commit comments