当前位置: 首页>>代码示例>>Java>>正文


Java TypeConverter.convertIfNecessary方法代码示例

本文整理汇总了Java中org.springframework.beans.TypeConverter.convertIfNecessary方法的典型用法代码示例。如果您正苦于以下问题:Java TypeConverter.convertIfNecessary方法的具体用法?Java TypeConverter.convertIfNecessary怎么用?Java TypeConverter.convertIfNecessary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.beans.TypeConverter的用法示例。


在下文中一共展示了TypeConverter.convertIfNecessary方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: afterPropertiesSet

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Look up the JNDI object and store it.
 */
@Override
public void afterPropertiesSet() throws IllegalArgumentException, NamingException {
	super.afterPropertiesSet();

	if (this.proxyInterfaces != null || !this.lookupOnStartup || !this.cache || this.exposeAccessContext) {
		// We need to create a proxy for this...
		if (this.defaultObject != null) {
			throw new IllegalArgumentException(
					"'defaultObject' is not supported in combination with 'proxyInterface'");
		}
		// We need a proxy and a JndiObjectTargetSource.
		this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this);
	}
	else {
		if (this.defaultObject != null && getExpectedType() != null &&
				!getExpectedType().isInstance(this.defaultObject)) {
			TypeConverter converter = (this.beanFactory != null ?
					this.beanFactory.getTypeConverter() : new SimpleTypeConverter());
			try {
				this.defaultObject = converter.convertIfNecessary(this.defaultObject, getExpectedType());
			}
			catch (TypeMismatchException ex) {
				throw new IllegalArgumentException("Default object [" + this.defaultObject + "] of type [" +
						this.defaultObject.getClass().getName() + "] is not of expected type [" +
						getExpectedType().getName() + "] and cannot be converted either", ex);
			}
		}
		// Locate specified JNDI object.
		this.jndiObject = lookupWithFallback();
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:35,代码来源:JndiObjectFactoryBean.java

示例2: resolvePreparedArguments

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(
		String beanName, RootBeanDefinition mbd, BeanWrapper bw, Member methodOrCtor, Object[] argsToResolve) {

	Class<?>[] paramTypes = (methodOrCtor instanceof Method ?
			((Method) methodOrCtor).getParameterTypes() : ((Constructor<?>) methodOrCtor).getParameterTypes());
	TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ?
			this.beanFactory.getCustomTypeConverter() : bw);
	BeanDefinitionValueResolver valueResolver =
			new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
	Object[] resolvedArgs = new Object[argsToResolve.length];
	for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
		Object argValue = argsToResolve[argIndex];
		MethodParameter methodParam = MethodParameter.forMethodOrConstructor(methodOrCtor, argIndex);
		GenericTypeResolver.resolveParameterType(methodParam, methodOrCtor.getDeclaringClass());
		if (argValue instanceof AutowiredArgumentMarker) {
			argValue = resolveAutowiredArgument(methodParam, beanName, null, converter);
		}
		else if (argValue instanceof BeanMetadataElement) {
			argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
		}
		else if (argValue instanceof String) {
			argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
		}
		Class<?> paramType = paramTypes[argIndex];
		try {
			resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
		}
		catch (TypeMismatchException ex) {
			String methodType = (methodOrCtor instanceof Constructor ? "constructor" : "factory method");
			throw new UnsatisfiedDependencyException(
					mbd.getResourceDescription(), beanName, argIndex, paramType,
					"Could not convert " + methodType + " argument value of type [" +
					ObjectUtils.nullSafeClassName(argValue) +
					"] to required type [" + paramType.getName() + "]: " + ex.getMessage());
		}
	}
	return resolvedArgs;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:42,代码来源:ConstructorResolver.java

示例3: resolveValue

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
private Object resolveValue(Method method) {
    Value val = AnnotationUtils.findAnnotation(method, Value.class);
    if (val != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Resolving @Value annotation on %s with key %s",
                    method.getName(), val.value()));
        }
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) getBeanFactory();
        String strValue = beanFactory.resolveEmbeddedValue(val.value());
        BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();

        Object unconvertedResult = resolver.evaluate(strValue, new BeanExpressionContext(beanFactory, null));
        TypeConverter converter = beanFactory.getTypeConverter();
        return converter.convertIfNecessary(unconvertedResult, method.getReturnType());
    }
    String message = "Method %s on interface %s must be annotated with @Value for auto-properties to work!";
    throw new AutoPropertiesException(String.format(message, method.getName(), method.getDeclaringClass().getName()));
}
 
开发者ID:mdaniline,项目名称:spring-autoproperties,代码行数:19,代码来源:AutoPropertiesFactoryBean.java

示例4: convertForProperty

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Convert the given value for the specified target property.
 */
private Object convertForProperty(Object value, String propertyName, BeanWrapper bw, TypeConverter converter) {
	if (converter instanceof BeanWrapperImpl) {
		return ((BeanWrapperImpl) converter).convertForProperty(value, propertyName);
	}
	else {
		PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
		MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
		return converter.convertIfNecessary(value, pd.getPropertyType(), methodParam);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:14,代码来源:AbstractAutowireCapableBeanFactory.java

示例5: createInstance

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected Map<Object, Object> createInstance() {
	if (this.sourceMap == null) {
		throw new IllegalArgumentException("'sourceMap' is required");
	}
	Map<Object, Object> result = null;
	if (this.targetMapClass != null) {
		result = BeanUtils.instantiateClass(this.targetMapClass);
	}
	else {
		result = new LinkedHashMap<Object, Object>(this.sourceMap.size());
	}
	Class<?> keyType = null;
	Class<?> valueType = null;
	if (this.targetMapClass != null) {
		keyType = GenericCollectionTypeResolver.getMapKeyType(this.targetMapClass);
		valueType = GenericCollectionTypeResolver.getMapValueType(this.targetMapClass);
	}
	if (keyType != null || valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) {
			Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
			Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
			result.put(convertedKey, convertedValue);
		}
	}
	else {
		result.putAll(this.sourceMap);
	}
	return result;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:33,代码来源:MapFactoryBean.java

示例6: resolvePreparedArguments

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(
		String beanName, RootBeanDefinition mbd, BeanWrapper bw, Member methodOrCtor, Object[] argsToResolve) {

	Class<?>[] paramTypes = (methodOrCtor instanceof Method ?
			((Method) methodOrCtor).getParameterTypes() : ((Constructor<?>) methodOrCtor).getParameterTypes());
	TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ?
			this.beanFactory.getCustomTypeConverter() : bw);
	BeanDefinitionValueResolver valueResolver =
			new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
	Object[] resolvedArgs = new Object[argsToResolve.length];
	for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
		Object argValue = argsToResolve[argIndex];
		MethodParameter methodParam = MethodParameter.forMethodOrConstructor(methodOrCtor, argIndex);
		GenericTypeResolver.resolveParameterType(methodParam, methodOrCtor.getDeclaringClass());
		if (argValue instanceof AutowiredArgumentMarker) {
			argValue = resolveAutowiredArgument(methodParam, beanName, null, converter);
		}
		else if (argValue instanceof BeanMetadataElement) {
			argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
		}
		else if (argValue instanceof String) {
			argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
		}
		Class<?> paramType = paramTypes[argIndex];
		try {
			resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
		}
		catch (TypeMismatchException ex) {
			throw new UnsatisfiedDependencyException(
					mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam),
					"Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) +
					"] to required type [" + paramType.getName() + "]: " + ex.getMessage());
		}
	}
	return resolvedArgs;
}
 
开发者ID:txazo,项目名称:spring,代码行数:40,代码来源:ConstructorResolver.java

示例7: createInstance

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected Map createInstance() {
	if (this.sourceMap == null) {
		throw new IllegalArgumentException("'sourceMap' is required");
	}
	Map result = null;
	if (this.targetMapClass != null) {
		result = (Map) BeanUtils.instantiateClass(this.targetMapClass);
	}
	else {
		result = new LinkedHashMap(this.sourceMap.size());
	}
	Class keyType = null;
	Class valueType = null;
	if (this.targetMapClass != null) {
		keyType = GenericCollectionTypeResolver.getMapKeyType(this.targetMapClass);
		valueType = GenericCollectionTypeResolver.getMapValueType(this.targetMapClass);
	}
	if (keyType != null || valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Map.Entry entry : this.sourceMap.entrySet()) {
			Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
			Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
			result.put(convertedKey, convertedValue);
		}
	}
	else {
		result.putAll(this.sourceMap);
	}
	return result;
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:33,代码来源:MapFactoryBean.java

示例8: checkQualifier

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
protected boolean checkQualifier(
		BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {

	Class<? extends Annotation> type = annotation.annotationType();
	RootBeanDefinition bd = (RootBeanDefinition) bdHolder.getBeanDefinition();

	AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
	if (qualifier == null) {
		qualifier = bd.getQualifier(ClassUtils.getShortName(type));
	}
	if (qualifier == null) {
		// First, check annotation on factory method, if applicable
		Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
		if (targetAnnotation == null) {
			RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
			if (dbd != null) {
				targetAnnotation = getFactoryMethodAnnotation(dbd, type);
			}
		}
		if (targetAnnotation == null) {
			// Look for matching annotation on the target class
			if (getBeanFactory() != null) {
				Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
				if (beanType != null) {
					targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
				}
			}
			if (targetAnnotation == null && bd.hasBeanClass()) {
				targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
			}
		}
		if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
			return true;
		}
	}

	Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
	if (attributes.isEmpty() && qualifier == null) {
		// If no attributes, the qualifier must be present
		return false;
	}
	for (Map.Entry<String, Object> entry : attributes.entrySet()) {
		String attributeName = entry.getKey();
		Object expectedValue = entry.getValue();
		Object actualValue = null;
		// Check qualifier first
		if (qualifier != null) {
			actualValue = qualifier.getAttribute(attributeName);
		}
		if (actualValue == null) {
			// Fall back on bean definition attribute
			actualValue = bd.getAttribute(attributeName);
		}
		if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) &&
				expectedValue instanceof String && bdHolder.matchesName((String) expectedValue)) {
			// Fall back on bean name (or alias) match
			continue;
		}
		if (actualValue == null && qualifier != null) {
			// Fall back on default, but only if the qualifier is present
			actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
		}
		if (actualValue != null) {
			actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
		}
		if (!expectedValue.equals(actualValue)) {
			return false;
		}
	}
	return true;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:75,代码来源:QualifierAnnotationAutowireCandidateResolver.java

示例9: doFindMatchingMethod

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Actually find a method with matching parameter type, i.e. where each
 * argument value is assignable to the corresponding parameter type.
 * @param arguments the argument values to match against method parameters
 * @return a matching method, or {@code null} if none
 */
protected Method doFindMatchingMethod(Object[] arguments) {
	TypeConverter converter = getTypeConverter();
	if (converter != null) {
		String targetMethod = getTargetMethod();
		Method matchingMethod = null;
		int argCount = arguments.length;
		Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
		int minTypeDiffWeight = Integer.MAX_VALUE;
		Object[] argumentsToUse = null;
		for (Method candidate : candidates) {
			if (candidate.getName().equals(targetMethod)) {
				// Check if the inspected method has the correct number of parameters.
				Class<?>[] paramTypes = candidate.getParameterTypes();
				if (paramTypes.length == argCount) {
					Object[] convertedArguments = new Object[argCount];
					boolean match = true;
					for (int j = 0; j < argCount && match; j++) {
						// Verify that the supplied argument is assignable to the method parameter.
						try {
							convertedArguments[j] = converter.convertIfNecessary(arguments[j], paramTypes[j]);
						}
						catch (TypeMismatchException ex) {
							// Ignore -> simply doesn't match.
							match = false;
						}
					}
					if (match) {
						int typeDiffWeight = getTypeDifferenceWeight(paramTypes, convertedArguments);
						if (typeDiffWeight < minTypeDiffWeight) {
							minTypeDiffWeight = typeDiffWeight;
							matchingMethod = candidate;
							argumentsToUse = convertedArguments;
						}
					}
				}
			}
		}
		if (matchingMethod != null) {
			setArguments(argumentsToUse);
			return matchingMethod;
		}
	}
	return null;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:51,代码来源:ArgumentConvertingMethodInvoker.java

示例10: checkQualifier

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
protected boolean checkQualifier(
		BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {

	Class<? extends Annotation> type = annotation.annotationType();
	RootBeanDefinition bd = (RootBeanDefinition) bdHolder.getBeanDefinition();

	AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
	if (qualifier == null) {
		qualifier = bd.getQualifier(ClassUtils.getShortName(type));
	}
	if (qualifier == null) {
		// First, check annotation on factory method, if applicable
		Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
		if (targetAnnotation == null) {
			RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
			if (dbd != null) {
				targetAnnotation = getFactoryMethodAnnotation(dbd, type);
			}
		}
		if (targetAnnotation == null) {
			// Look for matching annotation on the target class
			if (getBeanFactory() != null) {
				try {
					Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
					if (beanType != null) {
						targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
					}
				}
				catch (NoSuchBeanDefinitionException ex) {
					// Not the usual case - simply forget about the type check...
				}
			}
			if (targetAnnotation == null && bd.hasBeanClass()) {
				targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
			}
		}
		if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
			return true;
		}
	}

	Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
	if (attributes.isEmpty() && qualifier == null) {
		// If no attributes, the qualifier must be present
		return false;
	}
	for (Map.Entry<String, Object> entry : attributes.entrySet()) {
		String attributeName = entry.getKey();
		Object expectedValue = entry.getValue();
		Object actualValue = null;
		// Check qualifier first
		if (qualifier != null) {
			actualValue = qualifier.getAttribute(attributeName);
		}
		if (actualValue == null) {
			// Fall back on bean definition attribute
			actualValue = bd.getAttribute(attributeName);
		}
		if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) &&
				expectedValue instanceof String && bdHolder.matchesName((String) expectedValue)) {
			// Fall back on bean name (or alias) match
			continue;
		}
		if (actualValue == null && qualifier != null) {
			// Fall back on default, but only if the qualifier is present
			actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
		}
		if (actualValue != null) {
			actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
		}
		if (!expectedValue.equals(actualValue)) {
			return false;
		}
	}
	return true;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:80,代码来源:QualifierAnnotationAutowireCandidateResolver.java

示例11: doResolveDependency

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
public Object doResolveDependency(DependencyDescriptor descriptor, String beanName,
		Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {

	Class<?> type = descriptor.getDependencyType();
	Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
	if (value != null) {
		if (value instanceof String) {
			String strVal = resolveEmbeddedValue((String) value);
			BeanDefinition bd = (beanName != null && containsBean(beanName) ? getMergedBeanDefinition(beanName) : null);
			value = evaluateBeanDefinitionString(strVal, bd);
		}
		TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
		return (descriptor.getField() != null ?
				converter.convertIfNecessary(value, type, descriptor.getField()) :
				converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));
	}

	Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);
	if (multipleBeans != null) {
		return multipleBeans;
	}

	InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);
	try {
		Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
		if (matchingBeans.isEmpty()) {
			if (descriptor.isRequired()) {
				raiseNoSuchBeanDefinitionException(type, descriptor.getResolvableType().toString(), descriptor);
			}
			return null;
		}
		if (matchingBeans.size() > 1) {
			String primaryBeanName = determineAutowireCandidate(matchingBeans, descriptor);
			if (primaryBeanName == null) {
				if (descriptor.isRequired() || !indicatesMultipleBeans(type)) {
					return descriptor.resolveNotUnique(type, matchingBeans);
				}
				else {
					// In case of an optional Collection/Map, silently ignore a non-unique case:
					// possibly it was meant to be an empty collection of multiple regular beans
					// (before 4.3 in particular when we didn't even look for collection beans).
					return null;
				}
			}
			if (autowiredBeanNames != null) {
				autowiredBeanNames.add(primaryBeanName);
			}
			return matchingBeans.get(primaryBeanName);
		}
		// We have exactly one match.
		Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next();
		if (autowiredBeanNames != null) {
			autowiredBeanNames.add(entry.getKey());
		}
		return entry.getValue();
	}
	finally {
		ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);
	}
}
 
开发者ID:txazo,项目名称:spring,代码行数:61,代码来源:DefaultListableBeanFactory.java

示例12: checkQualifier

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Match the given qualifier annotation against the candidate bean definition.
 */
protected boolean checkQualifier(
		BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {

	Class<? extends Annotation> type = annotation.annotationType();
	RootBeanDefinition bd = (RootBeanDefinition) bdHolder.getBeanDefinition();

	AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
	if (qualifier == null) {
		qualifier = bd.getQualifier(ClassUtils.getShortName(type));
	}
	if (qualifier == null) {
		// First, check annotation on factory method, if applicable
		Annotation targetAnnotation = getFactoryMethodAnnotation(bd, type);
		if (targetAnnotation == null) {
			RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
			if (dbd != null) {
				targetAnnotation = getFactoryMethodAnnotation(dbd, type);
			}
		}
		if (targetAnnotation == null) {
			// Look for matching annotation on the target class
			if (this.beanFactory != null) {
				Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
				if (beanType != null) {
					targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
				}
			}
			if (targetAnnotation == null && bd.hasBeanClass()) {
				targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
			}
		}
		if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
			return true;
		}
	}

	Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
	if (attributes.isEmpty() && qualifier == null) {
		// If no attributes, the qualifier must be present
		return false;
	}
	for (Map.Entry<String, Object> entry : attributes.entrySet()) {
		String attributeName = entry.getKey();
		Object expectedValue = entry.getValue();
		Object actualValue = null;
		// Check qualifier first
		if (qualifier != null) {
			actualValue = qualifier.getAttribute(attributeName);
		}
		if (actualValue == null) {
			// Fall back on bean definition attribute
			actualValue = bd.getAttribute(attributeName);
		}
		if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) &&
				expectedValue instanceof String && bdHolder.matchesName((String) expectedValue)) {
			// Fall back on bean name (or alias) match
			continue;
		}
		if (actualValue == null && qualifier != null) {
			// Fall back on default, but only if the qualifier is present
			actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
		}
		if (actualValue != null) {
			actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
		}
		if (!expectedValue.equals(actualValue)) {
			return false;
		}
	}
	return true;
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:75,代码来源:QualifierAnnotationAutowireCandidateResolver.java

示例13: doFindMatchingMethod

import org.springframework.beans.TypeConverter; //导入方法依赖的package包/类
/**
 * Actually find a method with matching parameter type, i.e. where each
 * argument value is assignable to the corresponding parameter type.
 * @param arguments the argument values to match against method parameters
 * @return a matching method, or {@code null} if none
 */
protected Method doFindMatchingMethod(Object[] arguments) {
	TypeConverter converter = getTypeConverter();
	if (converter != null) {
		String targetMethod = getTargetMethod();
		Method matchingMethod = null;
		int argCount = arguments.length;
		Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
		int minTypeDiffWeight = Integer.MAX_VALUE;
		Object[] argumentsToUse = null;
		for (Method candidate : candidates) {
			if (candidate.getName().equals(targetMethod)) {
				// Check if the inspected method has the correct number of parameters.
				Class[] paramTypes = candidate.getParameterTypes();
				if (paramTypes.length == argCount) {
					Object[] convertedArguments = new Object[argCount];
					boolean match = true;
					for (int j = 0; j < argCount && match; j++) {
						// Verify that the supplied argument is assignable to the method parameter.
						try {
							convertedArguments[j] = converter.convertIfNecessary(arguments[j], paramTypes[j]);
						}
						catch (TypeMismatchException ex) {
							// Ignore -> simply doesn't match.
							match = false;
						}
					}
					if (match) {
						int typeDiffWeight = getTypeDifferenceWeight(paramTypes, convertedArguments);
						if (typeDiffWeight < minTypeDiffWeight) {
							minTypeDiffWeight = typeDiffWeight;
							matchingMethod = candidate;
							argumentsToUse = convertedArguments;
						}
					}
				}
			}
		}
		if (matchingMethod != null) {
			setArguments(argumentsToUse);
			return matchingMethod;
		}
	}
	return null;
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:51,代码来源:ArgumentConvertingMethodInvoker.java


注:本文中的org.springframework.beans.TypeConverter.convertIfNecessary方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。