1 package com.thoughtworks.xstream.converters.javabean; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.UndeclaredThrowableException ; 6 7 12 public class BeanProperty { 13 14 15 private Class memberClass; 16 17 18 private String propertyName; 19 20 21 private Class type; 22 23 24 protected Method getter; 25 26 27 private Method setter; 28 29 33 public BeanProperty(Class memberClass, String propertyName, Class propertyType) { 34 this.memberClass = memberClass; 35 this.propertyName = propertyName; 36 this.type = propertyType; 37 } 38 39 42 public Class getBeanClass() { 43 return memberClass; 44 } 45 46 51 public Class getType() { 52 return type; 53 } 54 55 58 public String getName() { 59 return propertyName; 60 } 61 62 65 public boolean isReadable() { 66 return (getter != null); 67 } 68 69 72 public boolean isWritable() { 73 return (setter != null); 74 } 75 76 81 public Object get(Object member) throws IllegalArgumentException , IllegalAccessException { 82 if (!isReadable()) 83 throw new IllegalStateException ("Property " + propertyName + " of " + memberClass 84 + " not readable"); 85 86 try { 87 return getter.invoke(member, null); 88 } catch (InvocationTargetException e) { 89 throw new UndeclaredThrowableException (e.getTargetException()); 90 } 91 } 92 93 98 public Object set(Object member, Object newValue) throws IllegalArgumentException , IllegalAccessException { 99 if (!isWritable()) 100 throw new IllegalStateException ("Property " + propertyName + " of " + memberClass 101 + " not writable"); 102 103 try { 104 return setter.invoke(member, new Object [] { newValue }); 105 } catch (InvocationTargetException e) { 106 throw new UndeclaredThrowableException (e.getTargetException()); 107 } 108 } 109 110 113 public void setGetterMethod(Method method) { 114 this.getter = method; 115 116 } 117 118 121 public void setSetterMethod(Method method) { 122 this.setter = method; 123 } 124 } | Popular Tags |