1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 21 import org.springframework.util.ClassUtils; 22 import org.springframework.util.StringUtils; 23 24 35 public class ClassArrayEditor extends PropertyEditorSupport { 36 37 private final ClassLoader classLoader; 38 39 40 44 public ClassArrayEditor() { 45 this(null); 46 } 47 48 54 public ClassArrayEditor(ClassLoader classLoader) { 55 this.classLoader = classLoader != null 56 ? classLoader : ClassUtils.getDefaultClassLoader(); 57 } 58 59 60 public void setAsText(String text) throws IllegalArgumentException { 61 if (StringUtils.hasText(text)) { 62 String [] classNames = StringUtils.commaDelimitedListToStringArray(text); 63 Class [] classes = new Class [classNames.length]; 64 for (int i = 0; i < classNames.length; i++) { 65 String className = classNames[i].trim(); 66 classes[i] = ClassUtils.resolveClassName(className, this.classLoader); 67 } 68 setValue(classes); 69 } 70 else { 71 setValue(null); 72 } 73 } 74 75 public String getAsText() { 76 Class [] classes = (Class []) getValue(); 77 if (classes == null || classes.length == 0) { 78 return ""; 79 } 80 return toCommaDelimitedString(classes); 81 } 82 83 84 private static String toCommaDelimitedString(Class [] classes) { 85 StringBuffer buffer = new StringBuffer (); 86 for (int i = 0; i < classes.length; ++i) { 87 if (i > 0) { 88 buffer.append(","); 89 } 90 buffer.append(ClassUtils.getQualifiedName(classes[i])); 91 } 92 return buffer.toString(); 93 } 94 95 } 96 | Popular Tags |