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

The following examples show how to use org.springframework.core.ResolvableType#getType() . 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: DeployerContextUtils.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
public static Type findType(BeanFactory beanFactory, String name) {
	ConfigurableListableBeanFactory registry = (ConfigurableListableBeanFactory) beanFactory;
	AbstractBeanDefinition definition = (AbstractBeanDefinition) registry.getBeanDefinition(name);

	Object source = definition.getSource();

	Type param = null;
	if (source instanceof MethodMetadata) {
		param = findBeanType(definition, ((MethodMetadata) source).getDeclaringClassName(), ((MethodMetadata) source).getMethodName());
	}
	else if (source instanceof Resource) {
		param = registry.getType(name);
	}
	else {
		ResolvableType type = (ResolvableType) getField(definition, "targetType");
		if (type != null) {
			param = type.getType();
		}
	}
	return param;
}
 
Example 2
Source File: GenericConversionService.java    From spring-analysis-note 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 3
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 4
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 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 5
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 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: 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 7
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 8
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 9
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 10
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);
}