001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.xbean.propertyeditor; 018 019import java.beans.PropertyEditor; 020import java.beans.PropertyEditorManager; 021import java.lang.reflect.Array; 022import java.util.List; 023import java.util.ListIterator; 024 025/** 026 * Adapter for editing array types. 027 * 028 * @version $Rev: 6687 $ $Date: 2005-12-28T21:08:56.733437Z $ 029 */ 030public final class PrototypeArrayConverter extends AbstractCollectionConverter { 031 private static final PropertyEditor MOCK_EDITOR = new StringEditor(); 032 033 public PrototypeArrayConverter(Class type) { 034 super(type, MOCK_EDITOR); 035 if (!type.isArray()) { 036 throw new IllegalArgumentException("type is not an array " + type.getSimpleName()); 037 } 038 if (type.getComponentType().isArray()) { 039 throw new IllegalArgumentException("type is a multi-dimensional array " + type.getSimpleName()); 040 } 041 } 042 043 @Override 044 protected PropertyEditor getEditor() { 045 return PropertyEditorManager.findEditor(getType()); 046 } 047 048 protected Object createCollection(final List list) { 049 final Object array = Array.newInstance(getType().getComponentType(), list.size()); 050 for (final ListIterator iterator = list.listIterator(); iterator.hasNext();) { 051 final Object item = iterator.next(); 052 int index = iterator.previousIndex(); 053 Array.set(array, index, item); 054 } 055 return array; 056 } 057}