Java Code Examples for org.springframework.beans.factory.BeanFactory#isTypeMatch()

The following examples show how to use org.springframework.beans.factory.BeanFactory#isTypeMatch() . 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: RegionTemplateAutoConfiguration.java    From spring-boot-data-geode with Apache License 2.0 5 votes vote down vote up
private boolean isBeanWithGemfireTemplateDependency(@NonNull BeanFactory beanFactory,
		@NonNull BeanDefinition beanDefinition) {

	Predicate<Object> isGemfireTemplate = value -> value instanceof RuntimeBeanReference
		? beanFactory.isTypeMatch(((RuntimeBeanReference) value).getBeanName(), GemfireOperations.class)
		: value instanceof GemfireOperations;

	boolean match = beanDefinition.getConstructorArgumentValues().getGenericArgumentValues().stream()
		.map(ConstructorArgumentValues.ValueHolder::getValue)
		.anyMatch(isGemfireTemplate);

	match |= match || beanDefinition.getPropertyValues().getPropertyValueList().stream()
		.map(PropertyValue::getValue)
		.anyMatch(isGemfireTemplate);

	match |= match || Optional.of(beanDefinition)
		.filter(AnnotatedBeanDefinition.class::isInstance)
		.map(AnnotatedBeanDefinition.class::cast)
		.map(AnnotatedBeanDefinition::getFactoryMethodMetadata)
		.filter(StandardMethodMetadata.class::isInstance)
		.map(StandardMethodMetadata.class::cast)
		.map(StandardMethodMetadata::getIntrospectedMethod)
		.map(method -> Arrays.stream(ArrayUtils.nullSafeArray(method.getParameterTypes(), Class.class))
			.filter(Objects::nonNull)
			.anyMatch(GemfireOperations.class::isAssignableFrom)
		).orElse(false);

	return match;
}
 
Example 2
Source File: CompositeBeanFactory.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Override
public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
	for (BeanFactory f : factories) {
		try {
			boolean b = f.isTypeMatch(name, targetType);
			if (b) {
				return b;
			}	
		} catch (BeansException e) {
			LOG.info("bean exception", e);
		}
	}
	return false;
}
 
Example 3
Source File: AbstractBeanFactory.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
	String beanName = transformedBeanName(name);
	Class<?> typeToMatch = (targetType != null ? targetType : Object.class);

	// 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 && ClassUtils.isAssignable(typeToMatch, type));
			}
			else {
				return ClassUtils.isAssignableValue(typeToMatch, beanInstance);
			}
		}
		else {
			return !BeanFactoryUtils.isFactoryDereference(name) &&
					ClassUtils.isAssignableValue(typeToMatch, 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), targetType);
		}

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

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

		// 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 4
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 5
Source File: BeanUtils.java    From spring-context-support with Apache License 2.0 2 votes vote down vote up
/**
 * Is Bean Present or not by the specified name and class
 *
 * @param beanFactory {@link BeanFactory}
 * @param beanName    The bean name
 * @param beanClass   The bean class
 * @return If present , return <code>true</code> , or <code>false</code>
 * @since 1.0.6
 */
public static boolean isBeanPresent(BeanFactory beanFactory, String beanName, Class<?> beanClass)
        throws NullPointerException {
    return beanFactory.containsBean(beanName) && beanFactory.isTypeMatch(beanName, beanClass);
}