001/*
002 * Copyright 2002-2012 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.propertyeditors;
018
019import java.beans.PropertyEditorSupport;
020
021import org.springframework.util.ClassUtils;
022import org.springframework.util.ObjectUtils;
023import org.springframework.util.StringUtils;
024
025/**
026 * Property editor for an array of {@link Class Classes}, to enable
027 * the direct population of a {@code Class[]} property without having to
028 * use a {@code String} class name property as bridge.
029 *
030 * <p>Also supports "java.lang.String[]"-style array class names, in contrast
031 * to the standard {@link Class#forName(String)} method.
032 *
033 * @author Rob Harrop
034 * @author Juergen Hoeller
035 * @since 2.0
036 */
037public class ClassArrayEditor extends PropertyEditorSupport {
038
039        private final ClassLoader classLoader;
040
041
042        /**
043         * Create a default {@code ClassEditor}, using the thread
044         * context {@code ClassLoader}.
045         */
046        public ClassArrayEditor() {
047                this(null);
048        }
049
050        /**
051         * Create a default {@code ClassArrayEditor}, using the given
052         * {@code ClassLoader}.
053         * @param classLoader the {@code ClassLoader} to use
054         * (or pass {@code null} for the thread context {@code ClassLoader})
055         */
056        public ClassArrayEditor(ClassLoader classLoader) {
057                this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
058        }
059
060
061        @Override
062        public void setAsText(String text) throws IllegalArgumentException {
063                if (StringUtils.hasText(text)) {
064                        String[] classNames = StringUtils.commaDelimitedListToStringArray(text);
065                        Class<?>[] classes = new Class<?>[classNames.length];
066                        for (int i = 0; i < classNames.length; i++) {
067                                String className = classNames[i].trim();
068                                classes[i] = ClassUtils.resolveClassName(className, this.classLoader);
069                        }
070                        setValue(classes);
071                }
072                else {
073                        setValue(null);
074                }
075        }
076
077        @Override
078        public String getAsText() {
079                Class<?>[] classes = (Class[]) getValue();
080                if (ObjectUtils.isEmpty(classes)) {
081                        return "";
082                }
083                StringBuilder sb = new StringBuilder();
084                for (int i = 0; i < classes.length; ++i) {
085                        if (i > 0) {
086                                sb.append(",");
087                        }
088                        sb.append(ClassUtils.getQualifiedName(classes[i]));
089                }
090                return sb.toString();
091        }
092
093}