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