Java Code Examples for org.springframework.beans.factory.config.DependencyDescriptor#fallbackMatchAllowed()

The following examples show how to use org.springframework.beans.factory.config.DependencyDescriptor#fallbackMatchAllowed() . 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: 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 2
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 3
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 4
Source File: GenericTypeAwareAutowireCandidateResolver.java    From blog_demos 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 5
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);
}