1 package com.thoughtworks.xstream.converters.javabean; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Modifier ; 6 import java.util.Iterator ; 7 8 import com.thoughtworks.xstream.converters.reflection.ObjectAccessException; 9 10 21 public class BeanProvider { 22 23 protected PropertyDictionary propertyDictionary = new PropertyDictionary(); 26 27 protected static final Object [] NO_PARAMS = new Object [0]; 28 29 public Object newInstance(Class type) { 30 try { 31 return getDefaultConstrutor(type).newInstance(NO_PARAMS); 32 } catch (InstantiationException e) { 33 throw new ObjectAccessException("Cannot construct " + type.getName(), e); 34 } catch (IllegalAccessException e) { 35 throw new ObjectAccessException("Cannot construct " + type.getName(), e); 36 } catch (InvocationTargetException e) { 37 if (e.getTargetException() instanceof RuntimeException ) { 38 throw (RuntimeException ) e.getTargetException(); 39 } else if (e.getTargetException() instanceof Error ) { 40 throw (Error ) e.getTargetException(); 41 } else { 42 throw new ObjectAccessException("Constructor for " + type.getName() 43 + " threw an exception", e); 44 } 45 } 46 } 47 48 79 public void visitSerializableProperties(Object object, Visitor visitor) { 80 for (Iterator iterator = propertyDictionary.serializablePropertiesFor(object.getClass()); iterator 81 .hasNext();) { 82 BeanProperty property = (BeanProperty) iterator.next(); 83 Object value = null; 84 try { 85 value = property.get(object); 86 } catch (IllegalArgumentException e) { 87 throw new ObjectAccessException("Could not get property " + property.getClass() 88 + "." + property.getName(), e); 89 } catch (IllegalAccessException e) { 90 throw new ObjectAccessException("Could not get property " + property.getClass() 91 + "." + property.getName(), e); 92 } 93 visitor.visit(property.getName(), property.getType(), value); 94 } 95 } 96 97 public void writeProperty(Object object, String propertyName, Object value) { 98 BeanProperty property = propertyDictionary.property(object.getClass(), propertyName); 99 try { 100 property.set(object, value); 101 } catch (IllegalArgumentException e) { 102 throw new ObjectAccessException("Could not set property " + object.getClass() + "." 103 + property.getName(), e); 104 } catch (IllegalAccessException e) { 105 throw new ObjectAccessException("Could not set property " + object.getClass() + "." 106 + property.getName(), e); 107 } 108 } 109 110 public Class getPropertyType(Object object, String name) { 111 return propertyDictionary.property(object.getClass(), name).getType(); 112 } 113 114 public boolean propertyDefinedInClass(String name, Class type) { 115 return propertyDictionary.property(type, name) != null; 116 } 117 118 121 public boolean canInstantiate(Class type) { 122 return getDefaultConstrutor(type) != null; 123 } 124 125 130 protected Constructor getDefaultConstrutor(Class type) { 131 Constructor [] constructors = type.getConstructors(); 132 for (int i = 0; i < constructors.length; i++) { 133 Constructor c = constructors[i]; 134 if (c.getParameterTypes().length == 0 && Modifier.isPublic(c.getModifiers())) 135 return c; 136 } 137 return null; 138 } 139 140 interface Visitor { 141 void visit(String name, Class type, Object value); 142 } 143 144 } | Popular Tags |