View Javadoc
1   /*
2    * Copyright 2002-2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.method;
18  
19  import java.lang.annotation.Annotation;
20  import java.lang.reflect.Method;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.springframework.beans.factory.BeanFactory;
26  import org.springframework.core.BridgeMethodResolver;
27  import org.springframework.core.MethodParameter;
28  import org.springframework.core.annotation.AnnotationUtils;
29  import org.springframework.util.Assert;
30  import org.springframework.util.ClassUtils;
31  
32  /**
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   *
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   *
41   * @author Arjen Poutsma
42   * @author Rossen Stoyanchev
43   * @author Juergen Hoeller
44   * @since 3.1
45   */
46  public class HandlerMethod {
47  
48  	/** Logger that is available to subclasses */
49  	protected final Log logger = LogFactory.getLog(getClass());
50  
51  	private final Object bean;
52  
53  	private final BeanFactory beanFactory;
54  
55  	private final Class<?> beanType;
56  
57  	private final Method method;
58  
59  	private final Method bridgedMethod;
60  
61  	private final MethodParameter[] parameters;
62  
63  
64  	/**
65  	 * Create an instance from a bean instance and a method.
66  	 */
67  	public HandlerMethod(Object bean, Method method) {
68  		Assert.notNull(bean, "Bean is required");
69  		Assert.notNull(method, "Method is required");
70  		this.bean = bean;
71  		this.beanFactory = null;
72  		this.beanType = ClassUtils.getUserClass(bean);
73  		this.method = method;
74  		this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
75  		this.parameters = initMethodParameters();
76  	}
77  
78  	/**
79  	 * Create an instance from a bean instance, method name, and parameter types.
80  	 * @throws NoSuchMethodException when the method cannot be found
81  	 */
82  	public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
83  		Assert.notNull(bean, "Bean is required");
84  		Assert.notNull(methodName, "Method name is required");
85  		this.bean = bean;
86  		this.beanFactory = null;
87  		this.beanType = ClassUtils.getUserClass(bean);
88  		this.method = bean.getClass().getMethod(methodName, parameterTypes);
89  		this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method);
90  		this.parameters = initMethodParameters();
91  	}
92  
93  	/**
94  	 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
95  	 * The method {@link #createWithResolvedBean()} may be used later to
96  	 * re-create the {@code HandlerMethod} with an initialized the bean.
97  	 */
98  	public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
99  		Assert.hasText(beanName, "Bean name is required");
100 		Assert.notNull(beanFactory, "BeanFactory is required");
101 		Assert.notNull(method, "Method is required");
102 		this.bean = beanName;
103 		this.beanFactory = beanFactory;
104 		this.beanType = ClassUtils.getUserClass(beanFactory.getType(beanName));
105 		this.method = method;
106 		this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
107 		this.parameters = initMethodParameters();
108 	}
109 
110 	/**
111 	 * Copy constructor for use in subclasses.
112 	 */
113 	protected HandlerMethod(HandlerMethod handlerMethod) {
114 		Assert.notNull(handlerMethod, "HandlerMethod is required");
115 		this.bean = handlerMethod.bean;
116 		this.beanFactory = handlerMethod.beanFactory;
117 		this.beanType = handlerMethod.beanType;
118 		this.method = handlerMethod.method;
119 		this.bridgedMethod = handlerMethod.bridgedMethod;
120 		this.parameters = handlerMethod.parameters;
121 	}
122 
123 	/**
124 	 * Re-create HandlerMethod with the resolved handler.
125 	 */
126 	private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
127 		Assert.notNull(handlerMethod, "HandlerMethod is required");
128 		Assert.notNull(handler, "Handler object is required");
129 		this.bean = handler;
130 		this.beanFactory = handlerMethod.beanFactory;
131 		this.beanType = handlerMethod.beanType;
132 		this.method = handlerMethod.method;
133 		this.bridgedMethod = handlerMethod.bridgedMethod;
134 		this.parameters = handlerMethod.parameters;
135 	}
136 
137 
138 	private MethodParameter[] initMethodParameters() {
139 		int count = this.bridgedMethod.getParameterTypes().length;
140 		MethodParameter[] result = new MethodParameter[count];
141 		for (int i = 0; i < count; i++) {
142 			result[i] = new HandlerMethodParameter(i);
143 		}
144 		return result;
145 	}
146 
147 	/**
148 	 * Returns the bean for this handler method.
149 	 */
150 	public Object getBean() {
151 		return this.bean;
152 	}
153 
154 	/**
155 	 * Returns the method for this handler method.
156 	 */
157 	public Method getMethod() {
158 		return this.method;
159 	}
160 
161 	/**
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.
165 	 */
166 	public Class<?> getBeanType() {
167 		return this.beanType;
168 	}
169 
170 	/**
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()}.
173 	 */
174 	protected Method getBridgedMethod() {
175 		return this.bridgedMethod;
176 	}
177 
178 	/**
179 	 * Returns the method parameters for this handler method.
180 	 */
181 	public MethodParameter[] getMethodParameters() {
182 		return this.parameters;
183 	}
184 
185 	/**
186 	 * Return the HandlerMethod return type.
187 	 */
188 	public MethodParameter getReturnType() {
189 		return new HandlerMethodParameter(-1);
190 	}
191 
192 	/**
193 	 * Return the actual return value type.
194 	 */
195 	public MethodParameter getReturnValueType(Object returnValue) {
196 		return new ReturnValueMethodParameter(returnValue);
197 	}
198 
199 	/**
200 	 * Returns {@code true} if the method return type is void, {@code false} otherwise.
201 	 */
202 	public boolean isVoid() {
203 		return Void.TYPE.equals(getReturnType().getParameterType());
204 	}
205 
206 	/**
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.
209 	 * @param annotationType the type of annotation to introspect the method for.
210 	 * @return the annotation, or {@code null} if none found
211 	 */
212 	public <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
213 		return AnnotationUtils.findAnnotation(this.method, annotationType);
214 	}
215 
216 	/**
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.
219 	 */
220 	public HandlerMethod createWithResolvedBean() {
221 		Object handler = this.bean;
222 		if (this.bean instanceof String) {
223 			String beanName = (String) this.bean;
224 			handler = this.beanFactory.getBean(beanName);
225 		}
226 		return new HandlerMethod(this, handler);
227 	}
228 
229 
230 	@Override
231 	public boolean equals(Object other) {
232 		if (this == other) {
233 			return true;
234 		}
235 		if (!(other instanceof HandlerMethod)) {
236 			return false;
237 		}
238 		HandlerMethod otherMethod = (HandlerMethod) other;
239 		return (this.bean.equals(otherMethod.bean) && this.method.equals(otherMethod.method));
240 	}
241 
242 	@Override
243 	public int hashCode() {
244 		return (this.bean.hashCode() * 31 + this.method.hashCode());
245 	}
246 
247 	@Override
248 	public String toString() {
249 		return this.method.toGenericString();
250 	}
251 
252 
253 	/**
254 	 * A MethodParameter with HandlerMethod-specific behavior.
255 	 */
256 	protected class HandlerMethodParameter extends MethodParameter {
257 
258 		public HandlerMethodParameter(int index) {
259 			super(HandlerMethod.this.bridgedMethod, index);
260 		}
261 
262 		@Override
263 		public Class<?> getContainingClass() {
264 			return HandlerMethod.this.getBeanType();
265 		}
266 
267 		@Override
268 		public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) {
269 			return HandlerMethod.this.getMethodAnnotation(annotationType);
270 		}
271 	}
272 
273 
274 	/**
275 	 * A MethodParameter for a HandlerMethod return type based on an actual return value.
276 	 */
277 	private class ReturnValueMethodParameter extends HandlerMethodParameter {
278 
279 		private final Object returnValue;
280 
281 		public ReturnValueMethodParameter(Object returnValue) {
282 			super(-1);
283 			this.returnValue = returnValue;
284 		}
285 
286 		@Override
287 		public Class<?> getParameterType() {
288 			return (this.returnValue != null ? this.returnValue.getClass() : super.getParameterType());
289 		}
290 	}
291 
292 }