1 16 package net.sf.dozer.util.mapping.propertydescriptor; 17 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Method ; 20 21 import net.sf.dozer.util.mapping.MappingException; 22 import net.sf.dozer.util.mapping.fieldmap.ClassMap; 23 import net.sf.dozer.util.mapping.fieldmap.Hint; 24 25 28 public class FieldPropertyDescriptor implements DozerPropertyDescriptorIF { 29 30 private final Field field; 31 32 public FieldPropertyDescriptor(Class bean, String fieldName, boolean isAccessible) throws NoSuchFieldException { 33 field = getFieldFromBean(bean, fieldName); 34 field.setAccessible(isAccessible); 37 } 38 39 public Class getPropertyType(Class clazz) { 40 return field.getType(); 41 } 42 43 public void setPropertyValue(Object bean, Object value, Hint hint, ClassMap classMap) { 44 try { 45 field.set(bean, value); 46 } catch (Exception e) { 47 throw new MappingException(e); 48 } 49 } 50 51 public Object getPropertyValue(Object bean) { 52 Object o = null; 53 try { 54 o = field.get(bean); 55 } catch (Exception e) { 56 throw new MappingException(e); 57 } 58 return o; 59 } 60 61 public String getReadMethodName(Class clazz) { 62 return "get" + field.getName(); 63 } 64 65 public String getWriteMethodName(Class clazz) { 66 return "set" + field.getName(); 67 } 68 69 public Method getReadMethod(Class clazz) { 70 throw new UnsupportedOperationException (); 71 } 72 73 public Method getWriteMethod(Class clazz) { 74 throw new UnsupportedOperationException (); 75 } 76 77 private Field getFieldFromBean(Class clazz, String fieldName) throws NoSuchFieldException { 78 try { 79 return clazz.getDeclaredField(fieldName); 80 } catch (NoSuchFieldException e) { 81 if (clazz.getSuperclass() != null) { 82 return getFieldFromBean(clazz.getSuperclass(), fieldName); 83 } 84 throw e; 85 } 86 } 87 88 } 89 | Popular Tags |