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

The following examples show how to use org.springframework.core.MethodParameter#getContainingClass() . 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: DefaultPredicateArgumentResolver.java    From java-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains the domain type information from the given method parameter. Will
 * favor an explicitly registered on through
 * {@link QuerydslPredicate#root()} but use the actual type of the method's
 * return type as fallback.
 * 
 * @param parameter
 *            must not be {@literal null}.
 * @return
 */
static TypeInformation<?> extractTypeInfo(MethodParameter parameter) {

	QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);

	if (annotation != null && !Object.class.equals(annotation.root())) {
		return ClassTypeInformation.from(annotation.root());
	}

	Class<?> containingClass = parameter.getContainingClass();
	if (ClassUtils.isAssignable(EntityController.class, containingClass)) {
		ResolvableType resolvableType = ResolvableType.forClass(containingClass);
		return ClassTypeInformation.from(resolvableType.as(EntityController.class).getGeneric(0).resolve());
	}

	return detectDomainType(ClassTypeInformation.fromReturnTypeOf(parameter.getMethod()));
}
 
Example 2
Source File: DependencyDescriptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new descriptor for a method or constructor parameter.
 * @param methodParameter the MethodParameter to wrap
 * @param required whether the dependency is required
 * @param eager whether this dependency is 'eager' in the sense of
 * eagerly resolving potential target beans for type matching
 */
public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) {
	Assert.notNull(methodParameter, "MethodParameter must not be null");
	this.methodParameter = methodParameter;
	this.declaringClass = methodParameter.getDeclaringClass();
	this.containingClass = methodParameter.getContainingClass();
	if (this.methodParameter.getMethod() != null) {
		this.methodName = methodParameter.getMethod().getName();
		this.parameterTypes = methodParameter.getMethod().getParameterTypes();
	}
	else {
		this.parameterTypes = methodParameter.getConstructor().getParameterTypes();
	}
	this.parameterIndex = methodParameter.getParameterIndex();
	this.required = required;
	this.eager = eager;
}
 
Example 3
Source File: DependencyDescriptor.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new descriptor for a method or constructor parameter.
 * @param methodParameter the MethodParameter to wrap
 * @param required whether the dependency is required
 * @param eager whether this dependency is 'eager' in the sense of
 * eagerly resolving potential target beans for type matching
 */
public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) {
	Assert.notNull(methodParameter, "MethodParameter must not be null");
	this.methodParameter = methodParameter;
	this.declaringClass = methodParameter.getDeclaringClass();
	this.containingClass = methodParameter.getContainingClass();
	if (this.methodParameter.getMethod() != null) {
		this.methodName = methodParameter.getMethod().getName();
		this.parameterTypes = methodParameter.getMethod().getParameterTypes();
	}
	else {
		this.parameterTypes = methodParameter.getConstructor().getParameterTypes();
	}
	this.parameterIndex = methodParameter.getParameterIndex();
	this.required = required;
	this.eager = eager;
}
 
Example 4
Source File: DependencyDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new descriptor for a method or constructor parameter.
 * @param methodParameter the MethodParameter to wrap
 * @param required whether the dependency is required
 * @param eager whether this dependency is 'eager' in the sense of
 * eagerly resolving potential target beans for type matching
 */
public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) {
	super(methodParameter);
	this.declaringClass = methodParameter.getDeclaringClass();
	if (this.methodParameter.getMethod() != null) {
		this.methodName = methodParameter.getMethod().getName();
		this.parameterTypes = methodParameter.getMethod().getParameterTypes();
	}
	else {
		this.parameterTypes = methodParameter.getConstructor().getParameterTypes();
	}
	this.parameterIndex = methodParameter.getParameterIndex();
	this.containingClass = methodParameter.getContainingClass();
	this.required = required;
	this.eager = eager;
}
 
Example 5
Source File: DependencyDescriptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new descriptor for a method or constructor parameter.
 * @param methodParameter the MethodParameter to wrap
 * @param required whether the dependency is required
 * @param eager whether this dependency is 'eager' in the sense of
 * eagerly resolving potential target beans for type matching
 */
public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) {
	super(methodParameter);

	this.declaringClass = methodParameter.getDeclaringClass();
	if (methodParameter.getMethod() != null) {
		this.methodName = methodParameter.getMethod().getName();
	}
	this.parameterTypes = methodParameter.getExecutable().getParameterTypes();
	this.parameterIndex = methodParameter.getParameterIndex();
	this.containingClass = methodParameter.getContainingClass();
	this.required = required;
	this.eager = eager;
}
 
Example 6
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleReturnValue(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();
			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 7
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value based on:
 * <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 a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 8
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleReturnValue(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();
			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 9
Source File: FileFilter.java    From jfilter with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to retrieve all FieldFilterSetting annotations from method
 *
 * @param methodParameter {@link MethodParameter} method parameter
 */
@Override
protected void setConfig(MethodParameter methodParameter) {
    controllerClass = methodParameter.getContainingClass();
    FileFilterSetting fileFilterSetting = getRequestMethodParameter().getDeclaredAnnotation(FileFilterSetting.class);
    if (fileFilterSetting != null)
        config = parseFile(fileFilterSetting.fileName());
}
 
Example 10
Source File: SecretRequestAdvice.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
/**
 * 是否支持加密消息体
 *
 * @param methodParameter methodParameter
 * @return true/false
 */
private boolean supportSecretRequest(MethodParameter methodParameter) {
    Class currentClass = methodParameter.getContainingClass();

    //如果当前类被排除,则不解析
    if (excludeClassList.contains(currentClass.getName())) {
        return false;
    }
    //如果扫描的不是全部,并且当前类不在扫描范围内,排除
    if (!this.isScanAllController && !includeClassList.contains(currentClass.getName())) {
        return false;
    }
    //如果不为注解扫描模式,则返回支持。
    if (!secretProperties.isScanAnnotation()) {
        return true;
    }
    //如果为注解扫描模式,判断
    //类注解
    Annotation classAnnotation = currentClass.getAnnotation(SecretBody.class);
    //方法注解
    SecretBody methodAnnotation = methodParameter.getMethodAnnotation(SecretBody.class);
    //如果类与方法均不存在注解,则排除
    if (classAnnotation == null && methodAnnotation == null) {
        return false;
    }
    //如果当前类的注解含有排除标识,则排除
    if (classAnnotation != null && ((SecretBody) classAnnotation).exclude()) {
        return false;
    }
    //如果当前方法的注解含有排除标识,则排除
    if (methodAnnotation != null && methodAnnotation.exclude()) {
        return false;
    }
    return true;
}
 
Example 11
Source File: MappingJackson2MessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private JavaType getJavaType(Class<?> targetClass, @Nullable Object conversionHint) {
	if (conversionHint instanceof MethodParameter) {
		MethodParameter param = (MethodParameter) conversionHint;
		param = param.nestedIfOptional();
		if (Message.class.isAssignableFrom(param.getParameterType())) {
			param = param.nested();
		}
		Type genericParameterType = param.getNestedGenericParameterType();
		Class<?> contextClass = param.getContainingClass();
		Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass);
		return this.objectMapper.getTypeFactory().constructType(type);
	}
	return this.objectMapper.constructType(targetClass);
}
 
Example 12
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value. Results will be
 * based on:
 * <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 a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Assert.state(method != null, "No handler method");
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 13
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 14
Source File: ResponseBodyResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean supports(HandlerResult result) {
	MethodParameter returnType = result.getReturnTypeSource();
	Class<?> containingClass = returnType.getContainingClass();
	return (AnnotatedElementUtils.hasAnnotation(containingClass, ResponseBody.class) ||
			returnType.hasMethodAnnotation(ResponseBody.class));
}
 
Example 15
Source File: MappingJackson2MessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private JavaType getJavaType(Class<?> targetClass, @Nullable Object conversionHint) {
	if (conversionHint instanceof MethodParameter) {
		MethodParameter param = (MethodParameter) conversionHint;
		param = param.nestedIfOptional();
		if (Message.class.isAssignableFrom(param.getParameterType())) {
			param = param.nested();
		}
		Type genericParameterType = param.getNestedGenericParameterType();
		Class<?> contextClass = param.getContainingClass();
		Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass);
		return this.objectMapper.getTypeFactory().constructType(type);
	}
	return this.objectMapper.constructType(targetClass);
}
 
Example 16
Source File: DependencyDescriptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new descriptor for a method or constructor parameter.
 * @param methodParameter the MethodParameter to wrap
 * @param required whether the dependency is required
 * @param eager whether this dependency is 'eager' in the sense of
 * eagerly resolving potential target beans for type matching
 */
public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) {
	super(methodParameter);

	this.declaringClass = methodParameter.getDeclaringClass();
	if (methodParameter.getMethod() != null) {
		this.methodName = methodParameter.getMethod().getName();
	}
	this.parameterTypes = methodParameter.getExecutable().getParameterTypes();
	this.parameterIndex = methodParameter.getParameterIndex();
	this.containingClass = methodParameter.getContainingClass();
	this.required = required;
	this.eager = eager;
}
 
Example 17
Source File: AbstractJackson2Decoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ObjectReader getObjectReader(ResolvableType elementType, @Nullable Map<String, Object> hints) {
	Assert.notNull(elementType, "'elementType' must not be null");
	MethodParameter param = getParameter(elementType);
	Class<?> contextClass = (param != null ? param.getContainingClass() : null);
	JavaType javaType = getJavaType(elementType.getType(), contextClass);
	Class<?> jsonView = (hints != null ? (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null);
	return jsonView != null ?
			getObjectMapper().readerWithView(jsonView).forType(javaType) :
			getObjectMapper().readerFor(javaType);
}
 
Example 18
Source File: ModelFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value. Results will be
 * based on:
 * <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 a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Assert.state(method != null, "No handler method");
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 19
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From spring-analysis-note 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 20
Source File: ResponseBodyResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean supports(HandlerResult result) {
	MethodParameter returnType = result.getReturnTypeSource();
	Class<?> containingClass = returnType.getContainingClass();
	return (AnnotatedElementUtils.hasAnnotation(containingClass, ResponseBody.class) ||
			returnType.hasMethodAnnotation(ResponseBody.class));
}