Java Code Examples for org.springframework.core.ResolvableType#isAssignableFrom()

The following examples show how to use org.springframework.core.ResolvableType#isAssignableFrom() . 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: ApplicationListenerMethodAdapter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private ResolvableType getResolvableType(ApplicationEvent event) {
	ResolvableType payloadType = null;
	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.toClass();
		if (!ApplicationEvent.class.isAssignableFrom(eventClass) &&
				payloadType != null && declaredEventType.isAssignableFrom(payloadType)) {
			return declaredEventType;
		}
		if (eventClass.isInstance(event)) {
			return declaredEventType;
		}
	}
	return null;
}
 
Example 2
Source File: ApplicationListenerMethodAdapter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private ResolvableType getResolvableType(ApplicationEvent event) {
	ResolvableType payloadType = null;
	if (event instanceof PayloadApplicationEvent) {
		PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
		payloadType = payloadEvent.getResolvableType().as(
				PayloadApplicationEvent.class).getGeneric(0);
	}
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass())
				&& payloadType != null) {
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return declaredEventType;
			}
		}
		if (declaredEventType.getRawClass().isAssignableFrom(event.getClass())) {
			return declaredEventType;
		}
	}
	return null;
}
 
Example 3
Source File: GenericApplicationListenerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {
	ResolvableType declaredEventType = resolveDeclaredEventType(listener.getClass());
	if (declaredEventType == null || declaredEventType.isAssignableFrom(ApplicationEvent.class)) {
		Class<?> targetClass = AopUtils.getTargetClass(listener);
		if (targetClass != listener.getClass()) {
			declaredEventType = resolveDeclaredEventType(targetClass);
		}
	}
	return declaredEventType;
}
 
Example 4
Source File: GenericApplicationListenerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {
	ResolvableType declaredEventType = resolveDeclaredEventType(listener.getClass());
	if (declaredEventType == null || declaredEventType.isAssignableFrom(
			ResolvableType.forClass(ApplicationEvent.class))) {
		Class<?> targetClass = AopUtils.getTargetClass(listener);
		if (targetClass != listener.getClass()) {
			declaredEventType = resolveDeclaredEventType(targetClass);
		}
	}
	return declaredEventType;
}
 
Example 5
Source File: GenericConversionService.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Check raw type first...
	if (this.typeInfo.getTargetType() != targetType.getObjectType()) {
		return false;
	}
	// Full check for complex generic type match required?
	ResolvableType rt = targetType.getResolvableType();
	if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
			!this.targetType.hasUnresolvableGenerics()) {
		return false;
	}
	return !(this.converter instanceof ConditionalConverter) ||
			((ConditionalConverter) this.converter).matches(sourceType, targetType);
}
 
Example 6
Source File: ApplicationListenerMethodAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean supportsEventType(ResolvableType eventType) {
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (declaredEventType.isAssignableFrom(eventType)) {
			return true;
		}
		if (PayloadApplicationEvent.class.isAssignableFrom(eventType.toClass())) {
			ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return true;
			}
		}
	}
	return eventType.hasUnresolvableGenerics();
}
 
Example 7
Source File: ApplicationListenerMethodAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean supportsEventType(ResolvableType eventType) {
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (declaredEventType.isAssignableFrom(eventType)) {
			return true;
		}
		else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) {
			ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return true;
			}
		}
	}
	return eventType.hasUnresolvableGenerics();
}
 
Example 8
Source File: StaticListableBeanFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public String[] getBeanNamesForType(@Nullable ResolvableType type) {
	boolean isFactoryType = false;
	if (type != null) {
		Class<?> resolved = type.resolve();
		if (resolved != null && FactoryBean.class.isAssignableFrom(resolved)) {
			isFactoryType = true;
		}
	}
	List<String> matches = new ArrayList<>();
	for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
		String name = entry.getKey();
		Object beanInstance = entry.getValue();
		if (beanInstance instanceof FactoryBean && !isFactoryType) {
			Class<?> objectType = ((FactoryBean<?>) beanInstance).getObjectType();
			if (objectType != null && (type == null || type.isAssignableFrom(objectType))) {
				matches.add(name);
			}
		}
		else {
			if (type == null || type.isInstance(beanInstance)) {
				matches.add(name);
			}
		}
	}
	return StringUtils.toStringArray(matches);
}
 
Example 9
Source File: WxMessageReturnValueHandler.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsReturnType(MethodParameter returnType) {
    if (returnType.hasMethodAnnotation(WxMapping.class)) {
        return false;
    }
    ResolvableType resolvableType = ResolvableType.forMethodParameter(returnType);
    ResolvableType arrayType = ResolvableType.forArrayComponent(ResolvableType.forClass(WxMessage.class));
    ResolvableType iterableType = ResolvableType.forClassWithGenerics(Iterable.class, WxMessage.class);
    return arrayType.isAssignableFrom(resolvableType) || iterableType.isAssignableFrom(resolvableType);
}
 
Example 10
Source File: StaticListableBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String[] getBeanNamesForType(ResolvableType type) {
	boolean isFactoryType = false;
	if (type != null) {
		Class<?> resolved = type.resolve();
		if (resolved != null && FactoryBean.class.isAssignableFrom(resolved)) {
			isFactoryType = true;
		}
	}
	List<String> matches = new ArrayList<String>();
	for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
		String name = entry.getKey();
		Object beanInstance = entry.getValue();
		if (beanInstance instanceof FactoryBean && !isFactoryType) {
			Class<?> objectType = ((FactoryBean<?>) beanInstance).getObjectType();
			if (objectType != null && (type == null || type.isAssignableFrom(objectType))) {
				matches.add(name);
			}
		}
		else {
			if (type == null || type.isInstance(beanInstance)) {
				matches.add(name);
			}
		}
	}
	return StringUtils.toStringArray(matches);
}
 
Example 11
Source File: GenericTypeAwareAutowireCandidateResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Match the given dependency type with its generic type information against the given
 * candidate bean definition.
 */
protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	ResolvableType dependencyType = descriptor.getResolvableType();
	if (dependencyType.getType() instanceof Class) {
		// No generic type -> we know it's a Class type-match, so no need to check again.
		return true;
	}

	ResolvableType targetType = null;
	boolean cacheType = false;
	RootBeanDefinition rbd = null;
	if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) {
		rbd = (RootBeanDefinition) bdHolder.getBeanDefinition();
	}
	if (rbd != null) {
		targetType = rbd.targetType;
		if (targetType == null) {
			cacheType = true;
			// First, check factory method return type, if applicable
			targetType = getReturnTypeForFactoryMethod(rbd, descriptor);
			if (targetType == null) {
				RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd);
				if (dbd != null) {
					targetType = dbd.targetType;
					if (targetType == null) {
						targetType = getReturnTypeForFactoryMethod(dbd, descriptor);
					}
				}
			}
		}
	}

	if (targetType == null) {
		// Regular case: straight bean instance, with BeanFactory available.
		if (this.beanFactory != null) {
			Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
			if (beanType != null) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType));
			}
		}
		// Fallback: no BeanFactory set, or no type resolvable through it
		// -> best-effort match against the target class if applicable.
		if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
			Class<?> beanClass = rbd.getBeanClass();
			if (!FactoryBean.class.isAssignableFrom(beanClass)) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass));
			}
		}
	}

	if (targetType == null) {
		return true;
	}
	if (cacheType) {
		rbd.targetType = targetType;
	}
	if (descriptor.fallbackMatchAllowed() &&
			(targetType.hasUnresolvableGenerics() || targetType.resolve() == Properties.class)) {
		// Fallback matches allow unresolvable generics, e.g. plain HashMap to Map<String,String>;
		// and pragmatically also java.util.Properties to any Map (since despite formally being a
		// Map<Object,Object>, java.util.Properties is usually perceived as a Map<String,String>).
		return true;
	}
	// Full check for complex generic type match...
	return dependencyType.isAssignableFrom(targetType);
}
 
Example 12
Source File: SimpleJndiBeanFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
	Class<?> type = getType(name);
	return (type != null && typeToMatch.isAssignableFrom(type));
}
 
Example 13
Source File: GenericTypeAwareAutowireCandidateResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Match the given dependency type with its generic type information against the given
 * candidate bean definition.
 */
protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	ResolvableType dependencyType = descriptor.getResolvableType();
	if (dependencyType.getType() instanceof Class) {
		// No generic type -> we know it's a Class type-match, so no need to check again.
		return true;
	}

	ResolvableType targetType = null;
	boolean cacheType = false;
	RootBeanDefinition rbd = null;
	if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) {
		rbd = (RootBeanDefinition) bdHolder.getBeanDefinition();
	}
	if (rbd != null) {
		targetType = rbd.targetType;
		if (targetType == null) {
			cacheType = true;
			// First, check factory method return type, if applicable
			targetType = getReturnTypeForFactoryMethod(rbd, descriptor);
			if (targetType == null) {
				RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd);
				if (dbd != null) {
					targetType = dbd.targetType;
					if (targetType == null) {
						targetType = getReturnTypeForFactoryMethod(dbd, descriptor);
					}
				}
			}
		}
	}

	if (targetType == null) {
		// Regular case: straight bean instance, with BeanFactory available.
		if (this.beanFactory != null) {
			Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
			if (beanType != null) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType));
			}
		}
		// Fallback: no BeanFactory set, or no type resolvable through it
		// -> best-effort match against the target class if applicable.
		if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
			Class<?> beanClass = rbd.getBeanClass();
			if (!FactoryBean.class.isAssignableFrom(beanClass)) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass));
			}
		}
	}

	if (targetType == null) {
		return true;
	}
	if (cacheType) {
		rbd.targetType = targetType;
	}
	if (descriptor.fallbackMatchAllowed() && targetType.hasUnresolvableGenerics()) {
		return true;
	}
	// Full check for complex generic type match...
	return dependencyType.isAssignableFrom(targetType);
}
 
Example 14
Source File: GenericTypeAwareAutowireCandidateResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Match the given dependency type with its generic type information against the given
 * candidate bean definition.
 */
protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	ResolvableType dependencyType = descriptor.getResolvableType();
	if (dependencyType.getType() instanceof Class) {
		// No generic type -> we know it's a Class type-match, so no need to check again.
		return true;
	}
	ResolvableType targetType = null;
	RootBeanDefinition rbd = null;
	if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) {
		rbd = (RootBeanDefinition) bdHolder.getBeanDefinition();
	}
	if (rbd != null) {
		// First, check factory method return type, if applicable
		targetType = getReturnTypeForFactoryMethod(rbd, descriptor);
		if (targetType == null) {
			RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd);
			if (dbd != null) {
				targetType = getReturnTypeForFactoryMethod(dbd, descriptor);
			}
		}
	}
	if (targetType == null) {
		// Regular case: straight bean instance, with BeanFactory available.
		if (this.beanFactory != null) {
			Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
			if (beanType != null) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType));
			}
		}
		// Fallback: no BeanFactory set, or no type resolvable through it
		// -> best-effort match against the target class if applicable.
		if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
			Class<?> beanClass = rbd.getBeanClass();
			if (!FactoryBean.class.isAssignableFrom(beanClass)) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass));
			}
		}
	}
	if (targetType == null || (descriptor.fallbackMatchAllowed() && targetType.hasUnresolvableGenerics())) {
		return true;
	}
	// Full check for complex generic type match...
	return dependencyType.isAssignableFrom(targetType);
}
 
Example 15
Source File: StaticListableBeanFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
	Class<?> type = getType(name);
	return (type != null && typeToMatch.isAssignableFrom(type));
}
 
Example 16
Source File: StaticListableBeanFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
	Class<?> type = getType(name);
	return (type != null && typeToMatch.isAssignableFrom(type));
}
 
Example 17
Source File: GenericTypeAwareAutowireCandidateResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Match the given dependency type with its generic type information against the given
 * candidate bean definition.
 */
protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
	ResolvableType dependencyType = descriptor.getResolvableType();
	if (dependencyType.getType() instanceof Class) {
		// No generic type -> we know it's a Class type-match, so no need to check again.
		return true;
	}

	ResolvableType targetType = null;
	boolean cacheType = false;
	RootBeanDefinition rbd = null;
	if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) {
		rbd = (RootBeanDefinition) bdHolder.getBeanDefinition();
	}
	if (rbd != null) {
		targetType = rbd.targetType;
		if (targetType == null) {
			cacheType = true;
			// First, check factory method return type, if applicable
			targetType = getReturnTypeForFactoryMethod(rbd, descriptor);
			if (targetType == null) {
				RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd);
				if (dbd != null) {
					targetType = dbd.targetType;
					if (targetType == null) {
						targetType = getReturnTypeForFactoryMethod(dbd, descriptor);
					}
				}
			}
		}
	}

	if (targetType == null) {
		// Regular case: straight bean instance, with BeanFactory available.
		if (this.beanFactory != null) {
			Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
			if (beanType != null) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType));
			}
		}
		// Fallback: no BeanFactory set, or no type resolvable through it
		// -> best-effort match against the target class if applicable.
		if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
			Class<?> beanClass = rbd.getBeanClass();
			if (!FactoryBean.class.isAssignableFrom(beanClass)) {
				targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass));
			}
		}
	}

	if (targetType == null) {
		return true;
	}
	if (cacheType) {
		rbd.targetType = targetType;
	}
	if (descriptor.fallbackMatchAllowed() &&
			(targetType.hasUnresolvableGenerics() || targetType.resolve() == Properties.class)) {
		// Fallback matches allow unresolvable generics, e.g. plain HashMap to Map<String,String>;
		// and pragmatically also java.util.Properties to any Map (since despite formally being a
		// Map<Object,Object>, java.util.Properties is usually perceived as a Map<String,String>).
		return true;
	}
	// Full check for complex generic type match...
	return dependencyType.isAssignableFrom(targetType);
}
 
Example 18
Source File: StaticListableBeanFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
	Class<?> type = getType(name);
	return (type != null && typeToMatch.isAssignableFrom(type));
}
 
Example 19
Source File: AbstractBeanFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);

	// Check manually registered singletons.
	Object beanInstance = getSingleton(beanName, false);
	if (beanInstance != null) {
		if (beanInstance instanceof FactoryBean) {
			if (!BeanFactoryUtils.isFactoryDereference(name)) {
				Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance);
				return (type != null && typeToMatch.isAssignableFrom(type));
			}
			else {
				return typeToMatch.isInstance(beanInstance);
			}
		}
		else {
			return (!BeanFactoryUtils.isFactoryDereference(name) && typeToMatch.isInstance(beanInstance));
		}
	}
	else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
		// null instance registered
		return false;
	}

	else {
		// No singleton instance found -> check bean definition.
		BeanFactory parentBeanFactory = getParentBeanFactory();
		if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
			// No bean definition found in this factory -> delegate to parent.
			return parentBeanFactory.isTypeMatch(originalBeanName(name), typeToMatch);
		}

		// Retrieve corresponding bean definition.
		RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

		Class<?> classToMatch = typeToMatch.getRawClass();
		Class<?>[] typesToMatch = (FactoryBean.class == classToMatch ?
				new Class<?>[] {classToMatch} : new Class<?>[] {FactoryBean.class, classToMatch});

		// Check decorated bean definition, if any: We assume it'll be easier
		// to determine the decorated bean's type than the proxy's type.
		BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
		if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
			RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
			Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd, typesToMatch);
			if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
				return typeToMatch.isAssignableFrom(targetClass);
			}
		}

		Class<?> beanType = predictBeanType(beanName, mbd, typesToMatch);
		if (beanType == null) {
			return false;
		}

		// Check bean class whether we're dealing with a FactoryBean.
		if (FactoryBean.class.isAssignableFrom(beanType)) {
			if (!BeanFactoryUtils.isFactoryDereference(name)) {
				// If it's a FactoryBean, we want to look at what it creates, not the factory class.
				beanType = getTypeForFactoryBean(beanName, mbd);
				if (beanType == null) {
					return false;
				}
			}
		}
		else if (BeanFactoryUtils.isFactoryDereference(name)) {
			// Special case: A SmartInstantiationAwareBeanPostProcessor returned a non-FactoryBean
			// type but we nevertheless are being asked to dereference a FactoryBean...
			// Let's check the original bean class and proceed with it if it is a FactoryBean.
			beanType = predictBeanType(beanName, mbd, FactoryBean.class);
			if (beanType == null || !FactoryBean.class.isAssignableFrom(beanType)) {
				return false;
			}
		}

		return typeToMatch.isAssignableFrom(beanType);
	}
}
 
Example 20
Source File: AbstractApplicationEventMulticaster.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Filter a listener early through checking its generically declared event
 * type before trying to instantiate it.
 * <p>If this method returns {@code true} for a given listener as a first pass,
 * the listener instance will get retrieved and fully evaluated through a
 * {@link #supportsEvent(ApplicationListener,ResolvableType, Class)}  call afterwards.
 * @param listenerType the listener's type as determined by the BeanFactory
 * @param eventType the event type to check
 * @return whether the given listener should be included in the candidates
 * for the given event type
 */
protected boolean supportsEvent(Class<?> listenerType, ResolvableType eventType) {
	if (GenericApplicationListener.class.isAssignableFrom(listenerType) ||
			SmartApplicationListener.class.isAssignableFrom(listenerType)) {
		return true;
	}
	ResolvableType declaredEventType = GenericApplicationListenerAdapter.resolveDeclaredEventType(listenerType);
	return (declaredEventType == null || declaredEventType.isAssignableFrom(eventType));
}