Java Code Examples for org.springframework.core.GenericTypeResolver#resolveReturnType()

The following examples show how to use org.springframework.core.GenericTypeResolver#resolveReturnType() . 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: GenericTypeAwarePropertyDescriptor.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Class<?> getPropertyType() {
	if (this.propertyType == null) {
		if (this.readMethod != null) {
			this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
		}
		else {
			MethodParameter writeMethodParam = getWriteMethodParameter();
			if (writeMethodParam != null) {
				this.propertyType = writeMethodParam.getParameterType();
			}
			else {
				this.propertyType = super.getPropertyType();
			}
		}
	}
	return this.propertyType;
}
 
Example 2
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 3
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 4
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 5
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
		Object returnValue, ExtendedModelMap implicitModel) {

	ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
	String attrName = (attr != null ? attr.value() : "");
	if ("".equals(attrName)) {
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
		attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
	}
	implicitModel.addAttribute(attrName, returnValue);
}
 
Example 6
Source File: ResourceInspectorUtil.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Determine the expected type as the returned type of the method.
 * If the return type is a List it will return the generic element type instead of a List.
 * @param resource - resource with methods
 * @param method Method
 * @return Class - type of class it needs.
 */
@SuppressWarnings("rawtypes")
protected static Class determineType(Class resource, Method method)
{
    Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);

    /*
    * The api is consistent that the object passed in must match the object passed out
    * however, operations are different, if the param is supplied  it doesn't have to match
    * the return type.
    * So we need special logic for operations
     */
    Annotation annot = AnnotationUtils.findAnnotation(resolvedMethod, Operation.class);
    if (annot != null)
    {
        return determineOperationType(resource, method);
    }
    else
    {
        Class returnType = GenericTypeResolver.resolveReturnType(resolvedMethod, resource);
        if (List.class.isAssignableFrom(returnType))
        {
            return ResolvableType.forMethodReturnType(method).asCollection().getGeneric(0).resolve();
        }
        return returnType;
    }
}
 
Example 7
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 8
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
		Object returnValue, ExtendedModelMap implicitModel) {

	ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
	String attrName = (attr != null ? attr.value() : "");
	if ("".equals(attrName)) {
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
		attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
	}
	implicitModel.addAttribute(attrName, returnValue);
}
 
Example 9
Source File: GenericTypeAwarePropertyDescriptor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
		@Nullable Method readMethod, @Nullable Method writeMethod, Class<?> propertyEditorClass)
		throws IntrospectionException {

	super(propertyName, null, null);
	this.beanClass = beanClass;

	Method readMethodToUse = (readMethod != null ? BridgeMethodResolver.findBridgedMethod(readMethod) : null);
	Method writeMethodToUse = (writeMethod != null ? BridgeMethodResolver.findBridgedMethod(writeMethod) : null);
	if (writeMethodToUse == null && readMethodToUse != null) {
		// Fallback: Original JavaBeans introspection might not have found matching setter
		// method due to lack of bridge method resolution, in case of the getter using a
		// covariant return type whereas the setter is defined for the concrete property type.
		Method candidate = ClassUtils.getMethodIfAvailable(
				this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null);
		if (candidate != null && candidate.getParameterCount() == 1) {
			writeMethodToUse = candidate;
		}
	}
	this.readMethod = readMethodToUse;
	this.writeMethod = writeMethodToUse;

	if (this.writeMethod != null) {
		if (this.readMethod == null) {
			// Write method not matched against read method: potentially ambiguous through
			// several overloaded variants, in which case an arbitrary winner has been chosen
			// by the JDK's JavaBeans Introspector...
			Set<Method> ambiguousCandidates = new HashSet<>();
			for (Method method : beanClass.getMethods()) {
				if (method.getName().equals(writeMethodToUse.getName()) &&
						!method.equals(writeMethodToUse) && !method.isBridge() &&
						method.getParameterCount() == writeMethodToUse.getParameterCount()) {
					ambiguousCandidates.add(method);
				}
			}
			if (!ambiguousCandidates.isEmpty()) {
				this.ambiguousWriteMethods = ambiguousCandidates;
			}
		}
		this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
		GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
	}

	if (this.readMethod != null) {
		this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
	}
	else if (this.writeMethodParameter != null) {
		this.propertyType = this.writeMethodParameter.getParameterType();
	}

	this.propertyEditorClass = propertyEditorClass;
}
 
Example 10
Source File: GenericTypeAwarePropertyDescriptor.java    From java-technology-stack with MIT License 4 votes vote down vote up
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
		@Nullable Method readMethod, @Nullable Method writeMethod, Class<?> propertyEditorClass)
		throws IntrospectionException {

	super(propertyName, null, null);
	this.beanClass = beanClass;

	Method readMethodToUse = (readMethod != null ? BridgeMethodResolver.findBridgedMethod(readMethod) : null);
	Method writeMethodToUse = (writeMethod != null ? BridgeMethodResolver.findBridgedMethod(writeMethod) : null);
	if (writeMethodToUse == null && readMethodToUse != null) {
		// Fallback: Original JavaBeans introspection might not have found matching setter
		// method due to lack of bridge method resolution, in case of the getter using a
		// covariant return type whereas the setter is defined for the concrete property type.
		Method candidate = ClassUtils.getMethodIfAvailable(
				this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null);
		if (candidate != null && candidate.getParameterCount() == 1) {
			writeMethodToUse = candidate;
		}
	}
	this.readMethod = readMethodToUse;
	this.writeMethod = writeMethodToUse;

	if (this.writeMethod != null) {
		if (this.readMethod == null) {
			// Write method not matched against read method: potentially ambiguous through
			// several overloaded variants, in which case an arbitrary winner has been chosen
			// by the JDK's JavaBeans Introspector...
			Set<Method> ambiguousCandidates = new HashSet<>();
			for (Method method : beanClass.getMethods()) {
				if (method.getName().equals(writeMethodToUse.getName()) &&
						!method.equals(writeMethodToUse) && !method.isBridge() &&
						method.getParameterCount() == writeMethodToUse.getParameterCount()) {
					ambiguousCandidates.add(method);
				}
			}
			if (!ambiguousCandidates.isEmpty()) {
				this.ambiguousWriteMethods = ambiguousCandidates;
			}
		}
		this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
		GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
	}

	if (this.readMethod != null) {
		this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
	}
	else if (this.writeMethodParameter != null) {
		this.propertyType = this.writeMethodParameter.getParameterType();
	}

	this.propertyEditorClass = propertyEditorClass;
}
 
Example 11
Source File: GenericTypeAwarePropertyDescriptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
		Method readMethod, Method writeMethod, Class<?> propertyEditorClass)
		throws IntrospectionException {

	super(propertyName, null, null);

	if (beanClass == null)  {
		throw new IntrospectionException("Bean class must not be null");
	}
	this.beanClass = beanClass;

	Method readMethodToUse = BridgeMethodResolver.findBridgedMethod(readMethod);
	Method writeMethodToUse = BridgeMethodResolver.findBridgedMethod(writeMethod);
	if (writeMethodToUse == null && readMethodToUse != null) {
		// Fallback: Original JavaBeans introspection might not have found matching setter
		// method due to lack of bridge method resolution, in case of the getter using a
		// covariant return type whereas the setter is defined for the concrete property type.
		Method candidate = ClassUtils.getMethodIfAvailable(
				this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null);
		if (candidate != null && candidate.getParameterTypes().length == 1) {
			writeMethodToUse = candidate;
		}
	}
	this.readMethod = readMethodToUse;
	this.writeMethod = writeMethodToUse;

	if (this.writeMethod != null) {
		if (this.readMethod == null) {
			// Write method not matched against read method: potentially ambiguous through
			// several overloaded variants, in which case an arbitrary winner has been chosen
			// by the JDK's JavaBeans Introspector...
			Set<Method> ambiguousCandidates = new HashSet<Method>();
			for (Method method : beanClass.getMethods()) {
				if (method.getName().equals(writeMethodToUse.getName()) &&
						!method.equals(writeMethodToUse) && !method.isBridge() &&
						method.getParameterTypes().length == writeMethodToUse.getParameterTypes().length) {
					ambiguousCandidates.add(method);
				}
			}
			if (!ambiguousCandidates.isEmpty()) {
				this.ambiguousWriteMethods = ambiguousCandidates;
			}
		}
		this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
		GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
	}

	if (this.readMethod != null) {
		this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
	}
	else if (this.writeMethodParameter != null) {
		this.propertyType = this.writeMethodParameter.getParameterType();
	}

	this.propertyEditorClass = propertyEditorClass;
}
 
Example 12
Source File: GenericTypeAwarePropertyDescriptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
		Method readMethod, Method writeMethod, Class<?> propertyEditorClass)
		throws IntrospectionException {

	super(propertyName, null, null);

	if (beanClass == null)  {
		throw new IntrospectionException("Bean class must not be null");
	}
	this.beanClass = beanClass;

	Method readMethodToUse = BridgeMethodResolver.findBridgedMethod(readMethod);
	Method writeMethodToUse = BridgeMethodResolver.findBridgedMethod(writeMethod);
	if (writeMethodToUse == null && readMethodToUse != null) {
		// Fallback: Original JavaBeans introspection might not have found matching setter
		// method due to lack of bridge method resolution, in case of the getter using a
		// covariant return type whereas the setter is defined for the concrete property type.
		Method candidate = ClassUtils.getMethodIfAvailable(
				this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null);
		if (candidate != null && candidate.getParameterTypes().length == 1) {
			writeMethodToUse = candidate;
		}
	}
	this.readMethod = readMethodToUse;
	this.writeMethod = writeMethodToUse;

	if (this.writeMethod != null) {
		if (this.readMethod == null) {
			// Write method not matched against read method: potentially ambiguous through
			// several overloaded variants, in which case an arbitrary winner has been chosen
			// by the JDK's JavaBeans Introspector...
			Set<Method> ambiguousCandidates = new HashSet<Method>();
			for (Method method : beanClass.getMethods()) {
				if (method.getName().equals(writeMethodToUse.getName()) &&
						!method.equals(writeMethodToUse) && !method.isBridge() &&
						method.getParameterTypes().length == writeMethodToUse.getParameterTypes().length) {
					ambiguousCandidates.add(method);
				}
			}
			if (!ambiguousCandidates.isEmpty()) {
				this.ambiguousWriteMethods = ambiguousCandidates;
			}
		}
		this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
		GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
	}

	if (this.readMethod != null) {
		this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
	}
	else if (this.writeMethodParameter != null) {
		this.propertyType = this.writeMethodParameter.getParameterType();
	}

	this.propertyEditorClass = propertyEditorClass;
}