Java Code Examples for org.springframework.core.MethodParameter#getMethod()

The following examples show how to use org.springframework.core.MethodParameter#getMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QualifierAnnotationAutowireCandidateResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine whether the provided bean definition is an autowire candidate.
 * <p>To be considered a candidate the bean's <em>autowire-candidate</em>
 * attribute must not have been set to 'false'. Also, if an annotation on
 * the field or parameter to be autowired is recognized by this bean factory
 * as a <em>qualifier</em>, the bean must 'match' against the annotation as
 * well as any attributes it may contain. The bean definition must contain
 * the same qualifier or match by meta attributes. A "value" attribute will
 * fallback to match against the bean name or an alias if a qualifier or
 * attribute does not match.
 * @see Qualifier
 */
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	boolean match = super.isAutowireCandidate(bdHolder, descriptor);
	if (match && descriptor != null) {
		match = checkQualifiers(bdHolder, descriptor.getAnnotations());
		if (match) {
			MethodParameter methodParam = descriptor.getMethodParameter();
			if (methodParam != null) {
				Method method = methodParam.getMethod();
				if (method == null || void.class == method.getReturnType()) {
					match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations());
				}
			}
		}
	}
	return match;
}
 
Example 2
Source File: ModelMethodProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		return;
	}
	else if (returnValue instanceof Model) {
		mavContainer.addAllAttributes(((Model) returnValue).asMap());
	}
	else {
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example 3
Source File: ViewMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		return;
	}
	else if (returnValue instanceof View){
		View view = (View) returnValue;
		mavContainer.setView(view);
		if (view instanceof SmartView) {
			if (((SmartView) view).isRedirectView()) {
				mavContainer.setRedirectModelScenario(true);
			}
		}
	}
	else {
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example 4
Source File: MapMethodProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		return;
	}
	else if (returnValue instanceof Map){
		mavContainer.addAllAttributes((Map) returnValue);
	}
	else {
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example 5
Source File: HttpEntityMethodProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Type getHttpEntityType(MethodParameter parameter) {
	Assert.isAssignable(HttpEntity.class, parameter.getParameterType());
	Type parameterType = parameter.getGenericParameterType();
	if (parameterType instanceof ParameterizedType) {
		ParameterizedType type = (ParameterizedType) parameterType;
		if (type.getActualTypeArguments().length != 1) {
			throw new IllegalArgumentException("Expected single generic parameter on '" +
					parameter.getParameterName() + "' in method " + parameter.getMethod());
		}
		return type.getActualTypeArguments()[0];
	}
	else if (parameterType instanceof Class) {
		return Object.class;
	}
	else {
		return null;
	}
}
 
Example 6
Source File: ViewMethodReturnValueHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue instanceof View) {
		View view = (View) returnValue;
		mavContainer.setView(view);
		if (view instanceof SmartView && ((SmartView) view).isRedirectView()) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else if (returnValue != null) {
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example 7
Source File: HttpEntityMethodProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
		throws IOException, HttpMediaTypeNotSupportedException {

	ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
	Type paramType = getHttpEntityType(parameter);
	if (paramType == null) {
		throw new IllegalArgumentException("HttpEntity parameter '" + parameter.getParameterName() +
				"' in method " + parameter.getMethod() + " is not parameterized");
	}

	Object body = readWithMessageConverters(webRequest, parameter, paramType);
	if (RequestEntity.class == parameter.getParameterType()) {
		return new RequestEntity<Object>(body, inputMessage.getHeaders(),
				inputMessage.getMethod(), inputMessage.getURI());
	}
	else {
		return new HttpEntity<Object>(body, inputMessage.getHeaders());
	}
}
 
Example 8
Source File: SpringMVCTranslator.java    From httpdoc with Apache License 2.0 6 votes vote down vote up
/**
 * 获取多请求体的该参数部分的绑定名称, 缺省情况下采用参数变量名, 如果有@RequestPart 和 @RequestParam 则 @RequestParam 要优先于 @RequestPart
 *
 * @param parameter 参数
 * @return 多请求体情况下的该参数部分的绑定名称
 */
protected String getPartName(MethodParameter parameter) {
    Method method = parameter.getMethod();
    int index = parameter.getParameterIndex();
    String[] names = DISCOVERER.getParameterNames(method);
    String bindName = names[index];

    RequestPart part = parameter.getParameterAnnotation(RequestPart.class);
    String partValue = part != null ? part.value() : EMPTY;
    String partName = part != null ? part.name() : EMPTY;
    bindName = partValue.isEmpty() ? partName.isEmpty() ? bindName : partName : partValue;

    RequestParam param = parameter.getParameterAnnotation(RequestParam.class);
    String paramValue = param != null ? param.value() : EMPTY;
    String paramName = param != null ? param.name() : EMPTY;
    bindName = paramValue.isEmpty() ? paramName.isEmpty() ? bindName : paramName : paramValue;

    return bindName;
}
 
Example 9
Source File: QualifierAnnotationAutowireCandidateResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determine whether the provided bean definition is an autowire candidate.
 * <p>To be considered a candidate the bean's <em>autowire-candidate</em>
 * attribute must not have been set to 'false'. Also, if an annotation on
 * the field or parameter to be autowired is recognized by this bean factory
 * as a <em>qualifier</em>, the bean must 'match' against the annotation as
 * well as any attributes it may contain. The bean definition must contain
 * the same qualifier or match by meta attributes. A "value" attribute will
 * fallback to match against the bean name or an alias if a qualifier or
 * attribute does not match.
 * @see Qualifier
 */
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	boolean match = super.isAutowireCandidate(bdHolder, descriptor);
	if (match && descriptor != null) {
		match = checkQualifiers(bdHolder, descriptor.getAnnotations());
		if (match) {
			MethodParameter methodParam = descriptor.getMethodParameter();
			if (methodParam != null) {
				Method method = methodParam.getMethod();
				if (method == null || void.class == method.getReturnType()) {
					match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations());
				}
			}
		}
	}
	return match;
}
 
Example 10
Source File: ViewNameMethodReturnValueHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue instanceof CharSequence) {
		String viewName = returnValue.toString();
		mavContainer.setViewName(viewName);
		if (isRedirectViewName(viewName)) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else if (returnValue != null){
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example 11
Source File: SubscriptionMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, Message<?> message) throws Exception {
	if (returnValue == null) {
		return;
	}

	MessageHeaders headers = message.getHeaders();
	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);

	if (subscriptionId == null) {
		throw new IllegalStateException(
				"No subscriptionId in " + message + " returned by: " + returnType.getMethod());
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Reply to @SubscribeMapping: " + returnValue);
	}
	this.messagingTemplate.convertAndSend(
			destination, returnValue, createHeaders(sessionId, subscriptionId, returnType));
}
 
Example 12
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (this.mavResolvers != null) {
		for (ModelAndViewResolver mavResolver : this.mavResolvers) {
			Class<?> handlerType = returnType.getContainingClass();
			Method method = returnType.getMethod();
			Assert.state(method != null, "No handler method");
			ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
			ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
			if (mav != ModelAndViewResolver.UNRESOLVED) {
				mavContainer.addAllAttributes(mav.getModel());
				mavContainer.setViewName(mav.getViewName());
				if (!mav.isReference()) {
					mavContainer.setView(mav.getView());
				}
				return;
			}
		}
	}

	// No suitable ModelAndViewResolver...
	if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
		this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
	}
	else {
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example 13
Source File: LocResponseBodyAdvice.java    From loc-framework with MIT License 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
  Method method = methodParameter.getMethod();
  if (method == null) {
    return false;
  }

  Class<?> clazz = method.getReturnType();
  return clazz != null && BaseResult.class.isAssignableFrom(clazz);

}
 
Example 14
Source File: LoginUserArgumentResolver.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
    final Method method = methodParameter.getMethod();
    final Class<?> clazz = Objects.requireNonNull(methodParameter.getMethod()).getDeclaringClass();

    boolean isHasLoginAuthAnn = clazz.isAnnotationPresent(LoginAuth.class) || Objects.requireNonNull(method).isAnnotationPresent(LoginAuth.class);
    boolean isHasLoginUserParameter = methodParameter.getParameterType().isAssignableFrom(SysUser.class);

    return isHasLoginAuthAnn && isHasLoginUserParameter;
}
 
Example 15
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value using one of:
 * <ol>
 * 	<li>The method {@code ModelAttribute} annotation value
 * 	<li>The declared return type if it is more specific than {@code Object}
 * 	<li>The actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType the return type of the method
 * @return the model name, never {@code null} nor empty
 */
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
	ModelAttribute annotation = returnType.getMethodAnnotation(ModelAttribute.class);
	if (annotation != null && StringUtils.hasText(annotation.value())) {
		return annotation.value();
	}
	else {
		Method method = returnType.getMethod();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, returnType.getContainingClass());
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 16
Source File: SubscriptionMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message)
		throws Exception {

	if (returnValue == null) {
		return;
	}

	MessageHeaders headers = message.getHeaders();
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
	String destination = SimpMessageHeaderAccessor.getDestination(headers);

	if (subscriptionId == null) {
		throw new IllegalStateException("No simpSubscriptionId in " + message +
				" returned by: " + returnType.getMethod());
	}
	if (destination == null) {
		throw new IllegalStateException("No simpDestination in " + message +
				" returned by: " + returnType.getMethod());
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Reply to @SubscribeMapping: " + returnValue);
	}
	MessageHeaders headersToSend = createHeaders(sessionId, subscriptionId, returnType);
	this.messagingTemplate.convertAndSend(destination, returnValue, headersToSend);
}
 
Example 17
Source File: ExpressionValueMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException {
	throw new UnsupportedOperationException("@Value is never required: " + parameter.getMethod());
}
 
Example 18
Source File: ExpressionValueMethodArgumentResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException {
	throw new UnsupportedOperationException("@Value is never required: " + parameter.getMethod());
}
 
Example 19
Source File: ResponseBodyAdviceWrapper.java    From stategen with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {
    final Method method = methodParameter.getMethod();
    return supportMethod(method);
}
 
Example 20
Source File: ExpressionValueMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void handleMissingValue(String name, MethodParameter parameter) {
	throw new UnsupportedOperationException("@Value is never required: " + parameter.getMethod());
}