Example usage for org.springframework.core ResolvableType as

List of usage examples for org.springframework.core ResolvableType as

Introduction

In this page you can find the example usage for org.springframework.core ResolvableType as.

Prototype

public ResolvableType as(Class<?> type) 

Source Link

Document

Return this type as a ResolvableType of the specified class.

Usage

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

/**
 * This implementation attempts to query the FactoryBean's generic parameter metadata
 * if present to determine the object type. If not present, i.e. the FactoryBean is
 * declared as a raw type, checks the FactoryBean's {@code getObjectType} method
 * on a plain instance of the FactoryBean, without bean properties applied yet.
 * If this doesn't return a type yet, a full creation of the FactoryBean is
 * used as fallback (through delegation to the superclass's implementation).
 * <p>The shortcut check for a FactoryBean is only applied in case of a singleton
 * FactoryBean. If the FactoryBean instance itself is not kept as singleton,
 * it will be fully created to check the type of its exposed object.
 *///from  ww w  . java  2s .c  o  m
@Override
@Nullable
protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
    if (mbd.getInstanceSupplier() != null) {
        ResolvableType targetType = mbd.targetType;
        if (targetType != null) {
            Class<?> result = targetType.as(FactoryBean.class).getGeneric().resolve();
            if (result != null) {
                return result;
            }
        }
        if (mbd.hasBeanClass()) {
            Class<?> result = GenericTypeResolver.resolveTypeArgument(mbd.getBeanClass(), FactoryBean.class);
            if (result != null) {
                return result;
            }
        }
    }

    String factoryBeanName = mbd.getFactoryBeanName();
    String factoryMethodName = mbd.getFactoryMethodName();

    if (factoryBeanName != null) {
        if (factoryMethodName != null) {
            // Try to obtain the FactoryBean's object type from its factory method declaration
            // without instantiating the containing bean at all.
            BeanDefinition fbDef = getBeanDefinition(factoryBeanName);
            if (fbDef instanceof AbstractBeanDefinition) {
                AbstractBeanDefinition afbDef = (AbstractBeanDefinition) fbDef;
                if (afbDef.hasBeanClass()) {
                    Class<?> result = getTypeForFactoryBeanFromMethod(afbDef.getBeanClass(), factoryMethodName);
                    if (result != null) {
                        return result;
                    }
                }
            }
        }
        // If not resolvable above and the referenced factory bean doesn't exist yet,
        // exit here - we don't want to force the creation of another bean just to
        // obtain a FactoryBean's object type...
        if (!isBeanEligibleForMetadataCaching(factoryBeanName)) {
            return null;
        }
    }

    // Let's obtain a shortcut instance for an early getObjectType() call...
    FactoryBean<?> fb = (mbd.isSingleton() ? getSingletonFactoryBeanForTypeCheck(beanName, mbd)
            : getNonSingletonFactoryBeanForTypeCheck(beanName, mbd));

    if (fb != null) {
        // Try to obtain the FactoryBean's object type from this early stage of the instance.
        Class<?> result = getTypeForFactoryBean(fb);
        if (result != null) {
            return result;
        } else {
            // No type found for shortcut FactoryBean instance:
            // fall back to full creation of the FactoryBean instance.
            return super.getTypeForFactoryBean(beanName, mbd);
        }
    }

    if (factoryBeanName == null && mbd.hasBeanClass()) {
        // No early bean instantiation possible: determine FactoryBean's type from
        // static factory method signature or from class inheritance hierarchy...
        if (factoryMethodName != null) {
            return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName);
        } else {
            return GenericTypeResolver.resolveTypeArgument(mbd.getBeanClass(), FactoryBean.class);
        }
    }

    return null;
}

From source file:org.springframework.context.event.ApplicationListenerMethodAdapter.java

@Override
public boolean supportsEventType(ResolvableType eventType) {
    for (ResolvableType declaredEventType : this.declaredEventTypes) {
        if (declaredEventType.isAssignableFrom(eventType)) {
            return true;
        }//  w  ww  .  ja  v a2 s  .  c  o m
        Class<?> eventClass = eventType.getRawClass();
        if (eventClass != null && PayloadApplicationEvent.class.isAssignableFrom(eventClass)) {
            ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
            if (declaredEventType.isAssignableFrom(payloadType)) {
                return true;
            }
        }
    }
    return eventType.hasUnresolvableGenerics();
}

From source file:org.springframework.context.event.ApplicationListenerMethodAdapter.java

@Nullable
private ResolvableType getResolvableType(ApplicationEvent event) {
    ResolvableType payloadType = null;/* w w  w .  j  av a  2s .co  m*/
    if (event instanceof PayloadApplicationEvent) {
        PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
        ResolvableType eventType = payloadEvent.getResolvableType();
        if (eventType != null) {
            payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
        }
    }
    for (ResolvableType declaredEventType : this.declaredEventTypes) {
        Class<?> eventClass = declaredEventType.getRawClass();
        if ((eventClass == null || !ApplicationEvent.class.isAssignableFrom(eventClass))
                && payloadType != null) {
            if (declaredEventType.isAssignableFrom(payloadType)) {
                return declaredEventType;
            }
        }
        if (declaredEventType.getRawClass().isInstance(event)) {
            return declaredEventType;
        }
    }
    return null;
}