org.springframework.core.GenericCollectionTypeResolver Java Examples

The following examples show how to use org.springframework.core.GenericCollectionTypeResolver. 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: BeanWrapperImpl.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
private void growCollectionIfNecessary(Collection<Object> collection, int index, String name,
		PropertyDescriptor pd, int nestingLevel) {

	if (!this.autoGrowNestedPaths) {
		return;
	}
	int size = collection.size();
	if (index >= size && index < this.autoGrowCollectionLimit) {
		Class<?> elementType = GenericCollectionTypeResolver.getCollectionReturnType(pd.getReadMethod(), nestingLevel);
		if (elementType != null) {
			for (int i = collection.size(); i < index + 1; i++) {
				collection.add(newValue(elementType, name));
			}
		}
	}
}
 
Example #2
Source File: GenericCollectionTypeResolverDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // StringList extends ArrayList<String> 具体化
        // getCollectionType 返回具体化泛型参数类型集合的成员类型 = String
        System.out.println(GenericCollectionTypeResolver.getCollectionType(StringList.class));

        System.out.println(GenericCollectionTypeResolver.getCollectionType(ArrayList.class));

        // 获取字段
        Field field = GenericCollectionTypeResolverDemo.class.getDeclaredField("stringList");
        System.out.println(GenericCollectionTypeResolver.getCollectionFieldType(field));

        field = GenericCollectionTypeResolverDemo.class.getDeclaredField("strings");
        System.out.println(GenericCollectionTypeResolver.getCollectionFieldType(field));
    }
 
Example #3
Source File: ListFactoryBean.java    From blog_demos with 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 #4
Source File: SetFactoryBean.java    From blog_demos with 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 #5
Source File: MapFactoryBean.java    From blog_demos with Apache License 2.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) {
		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;
}
 
Example #6
Source File: RequestPartMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Class<?> getCollectionParameterType(MethodParameter methodParam) {
	Class<?> paramType = methodParam.getNestedParameterType();
	if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){
		Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(methodParam);
		if (valueType != null) {
			return valueType;
		}
	}
	return null;
}
 
Example #7
Source File: RequestParamMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Class<?> getCollectionParameterType(MethodParameter parameter) {
	Class<?> paramType = parameter.getParameterType();
	if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){
		Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(parameter);
		if (valueType != null) {
			return valueType;
		}
	}
	return null;
}
 
Example #8
Source File: ListFactoryBean.java    From spring4-understanding with 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 #9
Source File: SetFactoryBean.java    From spring4-understanding with 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 File: MapFactoryBean.java    From spring4-understanding with Apache License 2.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) {
		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;
}
 
Example #11
Source File: DependencyDescriptor.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the generic element type of the wrapped Collection parameter/field, if any.
 * @return the generic type, or {@code null} if none
 */
public Class<?> getCollectionType() {
	return (this.field != null ?
			GenericCollectionTypeResolver.getCollectionFieldType(this.field, this.nestingLevel) :
			GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter));
}
 
Example #12
Source File: DependencyDescriptor.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the generic key type of the wrapped Map parameter/field, if any.
 * @return the generic type, or {@code null} if none
 */
public Class<?> getMapKeyType() {
	return (this.field != null ?
			GenericCollectionTypeResolver.getMapKeyFieldType(this.field, this.nestingLevel) :
			GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter));
}
 
Example #13
Source File: DependencyDescriptor.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the generic value type of the wrapped Map parameter/field, if any.
 * @return the generic type, or {@code null} if none
 */
public Class<?> getMapValueType() {
	return (this.field != null ?
			GenericCollectionTypeResolver.getMapValueFieldType(this.field, this.nestingLevel) :
			GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter));
}
 
Example #14
Source File: DependencyDescriptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the generic element type of the wrapped Collection parameter/field, if any.
 * @return the generic type, or {@code null} if none
 */
public Class<?> getCollectionType() {
	return (this.field != null ?
			GenericCollectionTypeResolver.getCollectionFieldType(this.field, this.nestingLevel) :
			GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter));
}
 
Example #15
Source File: DependencyDescriptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the generic key type of the wrapped Map parameter/field, if any.
 * @return the generic type, or {@code null} if none
 */
public Class<?> getMapKeyType() {
	return (this.field != null ?
			GenericCollectionTypeResolver.getMapKeyFieldType(this.field, this.nestingLevel) :
			GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter));
}
 
Example #16
Source File: DependencyDescriptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the generic value type of the wrapped Map parameter/field, if any.
 * @return the generic type, or {@code null} if none
 */
public Class<?> getMapValueType() {
	return (this.field != null ?
			GenericCollectionTypeResolver.getMapValueFieldType(this.field, this.nestingLevel) :
			GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter));
}