1 package jfun.yan.util.deserializer; 2 3 import java.beans.PropertyEditor ; 4 import java.lang.reflect.Constructor ; 5 6 13 public class PropertyEditorDeserializer implements Deserializer, java.io.Serializable { 14 private final DefaultConstructedThreadLocalSerializable editor; 15 private final Class editor_class; 16 public Class getEditorClass(){ 17 return editor_class; 18 } 19 public PropertyEditorDeserializer(final Class type) { 20 if(!PropertyEditor .class.isAssignableFrom(type)){ 21 throw new IllegalArgumentException ("PropertyEditor expected, "+ 22 type+" encountered"); 23 } 24 this.editor_class = type; 25 this.editor = new DefaultConstructedThreadLocalSerializable(type); 26 } 27 private static Constructor getDefaultConstructor(final Class type){ 28 try{ 29 return type.getConstructor(null); 30 } 31 catch(NoSuchMethodException e){ 32 throw new IllegalArgumentException ("default constructor of "+type 33 + " not found."); 34 } 35 } 36 public Object deserialize(String str) throws Exception { 37 final PropertyEditor editor = (PropertyEditor )this.editor.get(); 38 editor.setAsText(str); 39 return editor.getValue(); 40 } 41 public boolean equals(Object obj) { 42 if(obj instanceof PropertyEditorDeserializer){ 43 final PropertyEditorDeserializer other = (PropertyEditorDeserializer)obj; 44 return editor_class.equals(other.editor_class); 45 } 46 else return false; 47 } 48 public int hashCode() { 49 return editor_class.hashCode(); 50 } 51 public String toString() { 52 return editor_class.getName(); 53 } 54 } 55 | Popular Tags |