001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.config; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.beans.BeanUtils; 023import org.springframework.beans.TypeConverter; 024import org.springframework.core.ResolvableType; 025import org.springframework.lang.Nullable; 026 027/** 028 * Simple factory for shared List instances. Allows for central setup 029 * of Lists via the "list" element in XML bean definitions. 030 * 031 * @author Juergen Hoeller 032 * @since 09.12.2003 033 * @see SetFactoryBean 034 * @see MapFactoryBean 035 */ 036public class ListFactoryBean extends AbstractFactoryBean<List<Object>> { 037 038 @Nullable 039 private List<?> sourceList; 040 041 @SuppressWarnings("rawtypes") 042 @Nullable 043 private Class<? extends List> targetListClass; 044 045 046 /** 047 * Set the source List, typically populated via XML "list" elements. 048 */ 049 public void setSourceList(List<?> sourceList) { 050 this.sourceList = sourceList; 051 } 052 053 /** 054 * Set the class to use for the target List. Can be populated with a fully 055 * qualified class name when defined in a Spring application context. 056 * <p>Default is a {@code java.util.ArrayList}. 057 * @see java.util.ArrayList 058 */ 059 @SuppressWarnings("rawtypes") 060 public void setTargetListClass(@Nullable Class<? extends List> targetListClass) { 061 if (targetListClass == null) { 062 throw new IllegalArgumentException("'targetListClass' must not be null"); 063 } 064 if (!List.class.isAssignableFrom(targetListClass)) { 065 throw new IllegalArgumentException("'targetListClass' must implement [java.util.List]"); 066 } 067 this.targetListClass = targetListClass; 068 } 069 070 071 @Override 072 @SuppressWarnings("rawtypes") 073 public Class<List> getObjectType() { 074 return List.class; 075 } 076 077 @Override 078 @SuppressWarnings("unchecked") 079 protected List<Object> createInstance() { 080 if (this.sourceList == null) { 081 throw new IllegalArgumentException("'sourceList' is required"); 082 } 083 List<Object> result = null; 084 if (this.targetListClass != null) { 085 result = BeanUtils.instantiateClass(this.targetListClass); 086 } 087 else { 088 result = new ArrayList<>(this.sourceList.size()); 089 } 090 Class<?> valueType = null; 091 if (this.targetListClass != null) { 092 valueType = ResolvableType.forClass(this.targetListClass).asCollection().resolveGeneric(); 093 } 094 if (valueType != null) { 095 TypeConverter converter = getBeanTypeConverter(); 096 for (Object elem : this.sourceList) { 097 result.add(converter.convertIfNecessary(elem, valueType)); 098 } 099 } 100 else { 101 result.addAll(this.sourceList); 102 } 103 return result; 104 } 105 106}