Java Code Examples for org.springframework.beans.TypeConverter

The following examples show how to use org.springframework.beans.TypeConverter. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: DefaultListableBeanFactory.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,
		@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (Optional.class == descriptor.getDependencyType()) {
		return createOptionalDependency(descriptor, requestingBeanName);
	}
	else if (ObjectFactory.class == descriptor.getDependencyType() ||
			ObjectProvider.class == descriptor.getDependencyType()) {
		return new DependencyObjectProvider(descriptor, requestingBeanName);
	}
	else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
		return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(
				descriptor, requestingBeanName);
		if (result == null) {
			result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example 2
Source Project: java-technology-stack   Source File: DefaultListableBeanFactory.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,
		@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (Optional.class == descriptor.getDependencyType()) {
		return createOptionalDependency(descriptor, requestingBeanName);
	}
	else if (ObjectFactory.class == descriptor.getDependencyType() ||
			ObjectProvider.class == descriptor.getDependencyType()) {
		return new DependencyObjectProvider(descriptor, requestingBeanName);
	}
	else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
		return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(
				descriptor, requestingBeanName);
		if (result == null) {
			result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example 3
@Override
public Object resolveArgument(MethodParameter parameter, Channel channel, Object object) throws Exception {
    PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
    String name = ann.name();
    if (name.isEmpty()) {
        name = parameter.getParameterName();
        if (name == null) {
            throw new IllegalArgumentException(
                    "Name for argument type [" + parameter.getNestedParameterType().getName() +
                            "] not available, and parameter name information not found in class file either.");
        }
    }
    Map<String, String> uriTemplateVars = channel.attr(URI_TEMPLATE).get();
    Object arg = (uriTemplateVars != null ? uriTemplateVars.get(name) : null);
    TypeConverter typeConverter = beanFactory.getTypeConverter();
    return typeConverter.convertIfNecessary(arg, parameter.getParameterType());
}
 
Example 4
private Object convertIfNecessary(Method method, Object value) {
	Class<?>[] paramTypes = method.getParameterTypes();
	Object[] arguments = new Object[paramTypes.length];

	TypeConverter converter = beanFactory.getTypeConverter();

	if (arguments.length == 1) {
		return converter.convertIfNecessary(value, paramTypes[0],
				new MethodParameter(method, 0));
	}

	for (int i = 0; i < arguments.length; i++) {
		arguments[i] = converter.convertIfNecessary(value, paramTypes[i],
				new MethodParameter(method, i));
	}

	return arguments;
}
 
Example 5
Source Project: nacos-spring-project   Source File: ObjectUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Object convertIfNecessary(ConfigurableListableBeanFactory beanFactory,
		Method method, Object value) {
	Class<?>[] paramTypes = method.getParameterTypes();
	Object[] arguments = new Object[paramTypes.length];

	TypeConverter converter = beanFactory.getTypeConverter();

	if (arguments.length == 1) {
		return converter.convertIfNecessary(value, paramTypes[0],
				new MethodParameter(method, 0));
	}

	for (int i = 0; i < arguments.length; i++) {
		arguments[i] = converter.convertIfNecessary(value, paramTypes[i],
				new MethodParameter(method, i));
	}

	return arguments;
}
 
Example 6
Source Project: blog_demos   Source File: DefaultListableBeanFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
		Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (descriptor.getDependencyType().equals(ObjectFactory.class)) {
		return new DependencyObjectFactory(descriptor, beanName);
	}
	else if (descriptor.getDependencyType().equals(javaxInjectProviderClass)) {
		return new DependencyProviderFactory().createDependencyProvider(descriptor, beanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName);
		if (result == null) {
			result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example 7
@Override
public Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,
		Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {

	descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
	if (javaUtilOptionalClass == descriptor.getDependencyType()) {
		return new OptionalDependencyFactory().createOptionalDependency(descriptor, requestingBeanName);
	}
	else if (ObjectFactory.class == descriptor.getDependencyType() ||
			ObjectProvider.class == descriptor.getDependencyType()) {
		return new DependencyObjectProvider(descriptor, requestingBeanName);
	}
	else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
		return new Jsr330ProviderFactory().createDependencyProvider(descriptor, requestingBeanName);
	}
	else {
		Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(
				descriptor, requestingBeanName);
		if (result == null) {
			result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
		}
		return result;
	}
}
 
Example 8
Source Project: spring-analysis-note   Source File: JndiObjectFactoryBean.java    License: MIT License 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example 9
Source Project: blog_demos   Source File: SetFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Set<Object> createInstance() {
	if (this.sourceSet == null) {
		throw new IllegalArgumentException("'sourceSet' is required");
	}
	Set<Object> result = null;
	if (this.targetSetClass != null) {
		result = BeanUtils.instantiateClass(this.targetSetClass);
	}
	else {
		result = new LinkedHashSet<Object>(this.sourceSet.size());
	}
	Class<?> valueType = null;
	if (this.targetSetClass != null) {
		valueType = GenericCollectionTypeResolver.getCollectionType(this.targetSetClass);
	}
	if (valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Object elem : this.sourceSet) {
			result.add(converter.convertIfNecessary(elem, valueType));
		}
	}
	else {
		result.addAll(this.sourceSet);
	}
	return result;
}
 
Example 10
Source Project: spring-analysis-note   Source File: AbstractBeanFactory.java    License: MIT License 5 votes vote down vote up
@Override
public TypeConverter getTypeConverter() {
	TypeConverter customConverter = getCustomTypeConverter();
	if (customConverter != null) {
		return customConverter;
	}
	else {
		// Build default TypeConverter, registering custom editors.
		SimpleTypeConverter typeConverter = new SimpleTypeConverter();
		typeConverter.setConversionService(getConversionService());
		registerCustomEditors(typeConverter);
		return typeConverter;
	}
}
 
Example 11
Source Project: spring-analysis-note   Source File: BeanDefinitionValueResolver.java    License: MIT License 5 votes vote down vote up
/**
 * Create a BeanDefinitionValueResolver for the given BeanFactory and BeanDefinition.
 * @param beanFactory the BeanFactory to resolve against
 * @param beanName the name of the bean that we work on
 * @param beanDefinition the BeanDefinition of the bean that we work on
 * @param typeConverter the TypeConverter to use for resolving TypedStringValues
 */
public BeanDefinitionValueResolver(
		AbstractBeanFactory beanFactory, String beanName, BeanDefinition beanDefinition, TypeConverter typeConverter) {

	this.beanFactory = beanFactory;
	this.beanName = beanName;
	this.beanDefinition = beanDefinition;
	this.typeConverter = typeConverter;
}
 
Example 12
Source Project: blog_demos   Source File: ListFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected List<Object> createInstance() {
	if (this.sourceList == null) {
		throw new IllegalArgumentException("'sourceList' is required");
	}
	List<Object> result = null;
	if (this.targetListClass != null) {
		result = BeanUtils.instantiateClass(this.targetListClass);
	}
	else {
		result = new ArrayList<Object>(this.sourceList.size());
	}
	Class<?> valueType = null;
	if (this.targetListClass != null) {
		valueType = GenericCollectionTypeResolver.getCollectionType(this.targetListClass);
	}
	if (valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Object elem : this.sourceList) {
			result.add(converter.convertIfNecessary(elem, valueType));
		}
	}
	else {
		result.addAll(this.sourceList);
	}
	return result;
}
 
Example 13
Source Project: spring-analysis-note   Source File: AbstractFactoryBean.java    License: MIT License 5 votes vote down vote up
/**
 * Obtain a bean type converter from the BeanFactory that this bean
 * runs in. This is typically a fresh instance for each call,
 * since TypeConverters are usually <i>not</i> thread-safe.
 * <p>Falls back to a SimpleTypeConverter when not running in a BeanFactory.
 * @see ConfigurableBeanFactory#getTypeConverter()
 * @see org.springframework.beans.SimpleTypeConverter
 */
protected TypeConverter getBeanTypeConverter() {
	BeanFactory beanFactory = getBeanFactory();
	if (beanFactory instanceof ConfigurableBeanFactory) {
		return ((ConfigurableBeanFactory) beanFactory).getTypeConverter();
	}
	else {
		return new SimpleTypeConverter();
	}
}
 
Example 14
Source Project: spring-analysis-note   Source File: SetFactoryBean.java    License: MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Set<Object> createInstance() {
	if (this.sourceSet == null) {
		throw new IllegalArgumentException("'sourceSet' is required");
	}
	Set<Object> result = null;
	if (this.targetSetClass != null) {
		result = BeanUtils.instantiateClass(this.targetSetClass);
	}
	else {
		result = new LinkedHashSet<>(this.sourceSet.size());
	}
	Class<?> valueType = null;
	if (this.targetSetClass != null) {
		valueType = ResolvableType.forClass(this.targetSetClass).asCollection().resolveGeneric();
	}
	if (valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Object elem : this.sourceSet) {
			result.add(converter.convertIfNecessary(elem, valueType));
		}
	}
	else {
		result.addAll(this.sourceSet);
	}
	return result;
}
 
Example 15
Source Project: spring-analysis-note   Source File: MethodInvokingBean.java    License: MIT License 5 votes vote down vote up
/**
 * Obtain the TypeConverter from the BeanFactory that this bean runs in,
 * if possible.
 * @see ConfigurableBeanFactory#getTypeConverter()
 */
@Override
protected TypeConverter getDefaultTypeConverter() {
	if (this.beanFactory != null) {
		return this.beanFactory.getTypeConverter();
	}
	else {
		return super.getDefaultTypeConverter();
	}
}
 
Example 16
Source Project: java-technology-stack   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Return the underlying TypeConverter of this binder's BindingResult.
 */
protected TypeConverter getTypeConverter() {
	if (getTarget() != null) {
		return getInternalBindingResult().getPropertyAccessor();
	}
	else {
		return getSimpleTypeConverter();
	}
}
 
Example 17
/**
 * 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);
	}
}
 
Example 18
Source Project: blog_demos   Source File: MethodInvokingFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Obtain the TypeConverter from the BeanFactory that this bean runs in,
 * if possible.
 * @see ConfigurableBeanFactory#getTypeConverter()
 */
@Override
protected TypeConverter getDefaultTypeConverter() {
	if (this.beanFactory != null) {
		return this.beanFactory.getTypeConverter();
	}
	else {
		return super.getDefaultTypeConverter();
	}
}
 
Example 19
/**
 * Abstract method defining "autowire by type" (bean properties by type) behavior.
 * <p>This is like PicoContainer default, in which there must be exactly one bean
 * of the property type in the bean factory. This makes bean factories simple to
 * configure for small namespaces, but doesn't work as well as standard Spring
 * behavior for bigger applications.
 * @param beanName the name of the bean to autowire by type
 * @param mbd the merged bean definition to update through autowiring
 * @param bw the BeanWrapper from which we can obtain information about the bean
 * @param pvs the PropertyValues to register wired objects with
 */
protected void autowireByType(
		String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {

	TypeConverter converter = getCustomTypeConverter();
	if (converter == null) {
		converter = bw;
	}

	Set<String> autowiredBeanNames = new LinkedHashSet<>(4);
	String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
	for (String propertyName : propertyNames) {
		try {
			PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName);
			// Don't try autowiring by type for type Object: never makes sense,
			// even if it technically is a unsatisfied, non-simple property.
			if (Object.class != pd.getPropertyType()) {
				MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
				// Do not allow eager init for type matching in case of a prioritized post-processor.
				boolean eager = !PriorityOrdered.class.isInstance(bw.getWrappedInstance());
				DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager);
				Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);
				if (autowiredArgument != null) {
					pvs.add(propertyName, autowiredArgument);
				}
				for (String autowiredBeanName : autowiredBeanNames) {
					registerDependentBean(autowiredBeanName, beanName);
					if (logger.isTraceEnabled()) {
						logger.trace("Autowiring by type from bean name '" + beanName + "' via property '" +
								propertyName + "' to bean named '" + autowiredBeanName + "'");
					}
				}
				autowiredBeanNames.clear();
			}
		}
		catch (BeansException ex) {
			throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex);
		}
	}
}
 
Example 20
/**
 * Create a BeanDefinitionValueResolver for the given BeanFactory and BeanDefinition.
 * @param beanFactory the BeanFactory to resolve against
 * @param beanName the name of the bean that we work on
 * @param beanDefinition the BeanDefinition of the bean that we work on
 * @param typeConverter the TypeConverter to use for resolving TypedStringValues
 */
public BeanDefinitionValueResolver(
		AbstractBeanFactory beanFactory, String beanName, BeanDefinition beanDefinition, TypeConverter typeConverter) {

	this.beanFactory = beanFactory;
	this.beanName = beanName;
	this.beanDefinition = beanDefinition;
	this.typeConverter = typeConverter;
}
 
Example 21
Source Project: java-technology-stack   Source File: ListFactoryBean.java    License: MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected List<Object> createInstance() {
	if (this.sourceList == null) {
		throw new IllegalArgumentException("'sourceList' is required");
	}
	List<Object> result = null;
	if (this.targetListClass != null) {
		result = BeanUtils.instantiateClass(this.targetListClass);
	}
	else {
		result = new ArrayList<>(this.sourceList.size());
	}
	Class<?> valueType = null;
	if (this.targetListClass != null) {
		valueType = ResolvableType.forClass(this.targetListClass).asCollection().resolveGeneric();
	}
	if (valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Object elem : this.sourceList) {
			result.add(converter.convertIfNecessary(elem, valueType));
		}
	}
	else {
		result.addAll(this.sourceList);
	}
	return result;
}
 
Example 22
Source Project: java-technology-stack   Source File: AbstractFactoryBean.java    License: MIT License 5 votes vote down vote up
/**
 * Obtain a bean type converter from the BeanFactory that this bean
 * runs in. This is typically a fresh instance for each call,
 * since TypeConverters are usually <i>not</i> thread-safe.
 * <p>Falls back to a SimpleTypeConverter when not running in a BeanFactory.
 * @see ConfigurableBeanFactory#getTypeConverter()
 * @see org.springframework.beans.SimpleTypeConverter
 */
protected TypeConverter getBeanTypeConverter() {
	BeanFactory beanFactory = getBeanFactory();
	if (beanFactory instanceof ConfigurableBeanFactory) {
		return ((ConfigurableBeanFactory) beanFactory).getTypeConverter();
	}
	else {
		return new SimpleTypeConverter();
	}
}
 
Example 23
Source Project: java-technology-stack   Source File: SetFactoryBean.java    License: MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Set<Object> createInstance() {
	if (this.sourceSet == null) {
		throw new IllegalArgumentException("'sourceSet' is required");
	}
	Set<Object> result = null;
	if (this.targetSetClass != null) {
		result = BeanUtils.instantiateClass(this.targetSetClass);
	}
	else {
		result = new LinkedHashSet<>(this.sourceSet.size());
	}
	Class<?> valueType = null;
	if (this.targetSetClass != null) {
		valueType = ResolvableType.forClass(this.targetSetClass).asCollection().resolveGeneric();
	}
	if (valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Object elem : this.sourceSet) {
			result.add(converter.convertIfNecessary(elem, valueType));
		}
	}
	else {
		result.addAll(this.sourceSet);
	}
	return result;
}
 
Example 24
Source Project: java-technology-stack   Source File: MapFactoryBean.java    License: MIT License 5 votes vote down vote up
@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<>(this.sourceMap.size());
	}
	Class<?> keyType = null;
	Class<?> valueType = null;
	if (this.targetMapClass != null) {
		ResolvableType mapType = ResolvableType.forClass(this.targetMapClass).asMap();
		keyType = mapType.resolveGeneric(0);
		valueType = mapType.resolveGeneric(1);
	}
	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;
}
 
Example 25
Source Project: java-technology-stack   Source File: MethodInvokingBean.java    License: MIT License 5 votes vote down vote up
/**
 * Obtain the TypeConverter from the BeanFactory that this bean runs in,
 * if possible.
 * @see ConfigurableBeanFactory#getTypeConverter()
 */
@Override
protected TypeConverter getDefaultTypeConverter() {
	if (this.beanFactory != null) {
		return this.beanFactory.getTypeConverter();
	}
	else {
		return super.getDefaultTypeConverter();
	}
}
 
Example 26
Source Project: lams   Source File: MethodInvokingBean.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain the TypeConverter from the BeanFactory that this bean runs in,
 * if possible.
 * @see ConfigurableBeanFactory#getTypeConverter()
 */
@Override
protected TypeConverter getDefaultTypeConverter() {
	if (this.beanFactory != null) {
		return this.beanFactory.getTypeConverter();
	}
	else {
		return super.getDefaultTypeConverter();
	}
}
 
Example 27
/**
 * Return the TypeConverter used for argument type conversion.
 * <p>Can be cast to {@link org.springframework.beans.PropertyEditorRegistry}
 * if direct access to the underlying PropertyEditors is desired
 * (provided that the present TypeConverter actually implements the
 * PropertyEditorRegistry interface).
 */
@Nullable
public TypeConverter getTypeConverter() {
	if (this.typeConverter == null && this.useDefaultConverter) {
		this.typeConverter = getDefaultTypeConverter();
	}
	return this.typeConverter;
}
 
Example 28
Source Project: lams   Source File: SetFactoryBean.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Set<Object> createInstance() {
	if (this.sourceSet == null) {
		throw new IllegalArgumentException("'sourceSet' is required");
	}
	Set<Object> result = null;
	if (this.targetSetClass != null) {
		result = BeanUtils.instantiateClass(this.targetSetClass);
	}
	else {
		result = new LinkedHashSet<Object>(this.sourceSet.size());
	}
	Class<?> valueType = null;
	if (this.targetSetClass != null) {
		valueType = ResolvableType.forClass(this.targetSetClass).asCollection().resolveGeneric();
	}
	if (valueType != null) {
		TypeConverter converter = getBeanTypeConverter();
		for (Object elem : this.sourceSet) {
			result.add(converter.convertIfNecessary(elem, valueType));
		}
	}
	else {
		result.addAll(this.sourceSet);
	}
	return result;
}
 
Example 29
@Override
public Object resolveArgument(MethodParameter parameter, Channel channel, Object object) throws Exception {
    RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
    String name = ann.name();
    if (name.isEmpty()) {
        name = parameter.getParameterName();
        if (name == null) {
            throw new IllegalArgumentException(
                    "Name for argument type [" + parameter.getNestedParameterType().getName() +
                            "] not available, and parameter name information not found in class file either.");
        }
    }

    if (!channel.hasAttr(REQUEST_PARAM)) {
        QueryStringDecoder decoder = new QueryStringDecoder(((FullHttpRequest) object).uri());
        channel.attr(REQUEST_PARAM).set(decoder.parameters());
    }

    Map<String, List<String>> requestParams = channel.attr(REQUEST_PARAM).get();
    List<String> arg = (requestParams != null ? requestParams.get(name) : null);
    TypeConverter typeConverter = beanFactory.getTypeConverter();
    if (arg == null) {
        if ("\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n".equals(ann.defaultValue())) {
            return null;
        }else {
            return typeConverter.convertIfNecessary(ann.defaultValue(), parameter.getParameterType());
        }
    }
    if (List.class.isAssignableFrom(parameter.getParameterType())) {
        return typeConverter.convertIfNecessary(arg, parameter.getParameterType());
    } else {
        return typeConverter.convertIfNecessary(arg.get(0), parameter.getParameterType());
    }
}
 
Example 30
Source Project: lams   Source File: MapFactoryBean.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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) {
		ResolvableType mapType = ResolvableType.forClass(this.targetMapClass).asMap();
		keyType = mapType.resolveGeneric(0);
		valueType = mapType.resolveGeneric(1);
	}
	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;
}