Example usage for org.springframework.core GenericTypeResolver resolveReturnTypeArgument

List of usage examples for org.springframework.core GenericTypeResolver resolveReturnTypeArgument

Introduction

In this page you can find the example usage for org.springframework.core GenericTypeResolver resolveReturnTypeArgument.

Prototype

@Nullable
public static Class<?> resolveReturnTypeArgument(Method method, Class<?> genericIfc) 

Source Link

Document

Resolve the single type argument of the given generic interface against the given target method which is assumed to return the given interface or an implementation of it.

Usage

From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java

/**
 * Introspect the factory method signatures on the given bean class,
 * trying to find a common {@code FactoryBean} object type declared there.
 * @param beanClass the bean class to find the factory method on
 * @param factoryMethodName the name of the factory method
 * @return the common {@code FactoryBean} object type, or {@code null} if none
 *//*w w  w  .ja  v a2s .c om*/
@Nullable
private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, final String factoryMethodName) {

    /**
     * Holder used to keep a reference to a {@code Class} value.
     */
    class Holder {

        @Nullable
        Class<?> value = null;
    }

    final Holder objectType = new Holder();

    // CGLIB subclass methods hide generic parameters; look at the original user class.
    Class<?> fbClass = ClassUtils.getUserClass(beanClass);

    // Find the given factory method, taking into account that in the case of
    // @Bean methods, there may be parameters present.
    ReflectionUtils.doWithMethods(fbClass, method -> {
        if (method.getName().equals(factoryMethodName)
                && FactoryBean.class.isAssignableFrom(method.getReturnType())) {
            Class<?> currentType = GenericTypeResolver.resolveReturnTypeArgument(method, FactoryBean.class);
            if (currentType != null) {
                objectType.value = ClassUtils.determineCommonAncestor(currentType, objectType.value);
            }
        }
    });

    return (objectType.value != null && Object.class != objectType.value ? objectType.value : null);
}