1 16 package net.sf.dozer.util.mapping.fieldmap; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 21 import net.sf.dozer.util.mapping.propertydescriptor.DozerPropertyDescriptorIF; 22 import net.sf.dozer.util.mapping.propertydescriptor.PropertyDescriptorFactory; 23 import net.sf.dozer.util.mapping.util.MapperConstants; 24 import net.sf.dozer.util.mapping.util.MappingUtils; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 35 public abstract class FieldMap implements Cloneable { 36 private static final Log log = LogFactory.getLog(FieldMap.class); 37 38 private Field sourceField; 39 private Field destField; 40 private Hint sourceTypeHint; 41 private Hint destinationTypeHint; 42 private String type; 43 private boolean copyByReference; 44 private boolean copyByReferenceOveridden; 45 private String mapId; 46 private String customConverter; 47 48 private final MappingUtils mappingUtils = new MappingUtils(); 49 50 private DozerPropertyDescriptorIF getSourcePropertyDescriptor(Class sourceClass) throws NoSuchFieldException { 51 return PropertyDescriptorFactory.getPropertyDescriptor(sourceField, sourceClass); 52 } 53 54 private DozerPropertyDescriptorIF getDestinationPropertyDescriptor(Class destClass) throws NoSuchFieldException { 55 return PropertyDescriptorFactory.getPropertyDescriptor(destField, destClass); 56 } 57 58 public String getSourceFieldReadMethodName(Class sourceClass) throws NoSuchFieldException { 59 return getSourcePropertyDescriptor(sourceClass).getReadMethodName(sourceClass); 60 } 61 62 public String getDestFieldReadMethodName(Class destClass) throws NoSuchFieldException { 63 return getDestinationPropertyDescriptor(destClass).getReadMethodName(destClass); 64 } 65 66 public Class getDestHintType(Class sourceClass) { 67 if (getDestinationTypeHint() != null) { 68 if (getSourceTypeHint() != null) { 69 return getDestinationTypeHint().getHint(sourceClass, getSourceTypeHint().getHints()); 70 } else { 71 return getDestinationTypeHint().getHint(); 72 } 73 } else { 74 return sourceClass; 75 } 76 } 77 78 public Class getDestFieldType(Class destClass) throws NoSuchMethodException , ClassNotFoundException , 79 NoSuchFieldException { 80 return getDestinationPropertyDescriptor(destClass).getPropertyType(destClass); 81 } 82 83 public Class getSourceFieldType(Class srcClass) throws NoSuchMethodException , ClassNotFoundException , 84 NoSuchFieldException { 85 return getSourcePropertyDescriptor(srcClass).getPropertyType(srcClass); 86 } 87 88 public Method getDestFieldWriteMethod(Class destClass) throws NoSuchMethodException , ClassNotFoundException , 89 NoSuchFieldException { 90 return getDestinationPropertyDescriptor(destClass).getWriteMethod(destClass); 91 } 92 93 public Object getSrcFieldValue(Object srcObj) throws IllegalAccessException , InvocationTargetException , 94 NoSuchMethodException , ClassNotFoundException , NoSuchFieldException { 95 if (isSourceSelfReferencing()) { 97 return srcObj; 98 } 99 return getSourcePropertyDescriptor(srcObj.getClass()).getPropertyValue(srcObj); 100 } 101 102 public void writeDestinationValue(Object destObj, Object destFieldValue, ClassMap classMap) 103 throws IllegalAccessException , InvocationTargetException , InstantiationException , NoSuchMethodException , 104 ClassNotFoundException , NoSuchFieldException { 105 if (log.isDebugEnabled()) { 106 log.debug("Getting ready to invoke write method on the destination object. Dest Obj: " 107 + mappingUtils.getClassNameWithoutPackage(destObj.getClass()) + ", Dest value: " + destFieldValue); 108 } 109 DozerPropertyDescriptorIF propDescriptor = getDestinationPropertyDescriptor(destObj.getClass()); 110 propDescriptor.setPropertyValue(destObj, destFieldValue, getDestinationTypeHint(), classMap); 111 } 112 113 public Object getDestinationObject(Object destObj) throws IllegalAccessException , InvocationTargetException , 114 InstantiationException , NoSuchFieldException { 115 return getDestinationPropertyDescriptor(destObj.getClass()).getPropertyValue(destObj); 116 } 117 118 public Object doesFieldExist(Object destObj, Class destClass) throws IllegalAccessException , 119 InvocationTargetException , InstantiationException , NoSuchMethodException , ClassNotFoundException , NoSuchFieldException { 120 Object field = null; 121 if (!isGenericFieldMap()) { 122 } else { 124 if (destClass == null) { 127 destClass = destObj.getClass(); 128 } 129 field = getDestinationObject(destObj); 130 } 131 return field; 132 } 133 134 public String getDestKey() { 135 String key; 136 if (getDestField().getKey() != null) { 137 key = getDestField().getKey(); 138 } else { 139 key = getSourceField().getName(); 140 } 141 return key; 142 } 143 144 public String getSourceKey() { 145 String key; 146 if (getSourceField().getKey() != null) { 147 key = getSourceField().getKey(); 148 } else { 149 key = getDestField().getName(); 150 } 151 return key; 152 } 153 154 public Hint getDestinationTypeHint() { 155 return destinationTypeHint; 156 } 157 158 public void setDestinationTypeHint(Hint destHint) { 159 this.destinationTypeHint = destHint; 160 } 161 162 public Hint getSourceTypeHint() { 163 return sourceTypeHint; 164 } 165 166 public void setSourceTypeHint(Hint sourceHint) { 167 this.sourceTypeHint = sourceHint; 168 } 169 170 public Field getDestField() { 171 return destField; 172 } 173 174 public void setDestField(Field destField) { 175 this.destField = destField; 176 } 177 178 public Field getSourceField() { 179 return sourceField; 180 } 181 182 public void setSourceField(Field sourceField) { 183 this.sourceField = sourceField; 184 } 185 186 public Object clone() throws CloneNotSupportedException { 187 return super.clone(); 188 } 189 190 public String getType() { 191 return type; 192 } 193 194 public void setType(String type) { 195 this.type = type; 196 } 197 198 public boolean getCopyByReference() { 199 return copyByReference; 200 } 201 202 public void setCopyByReference(boolean copyByReference) { 203 this.copyByReference = copyByReference; 204 this.copyByReferenceOveridden = true; 205 } 206 207 211 protected boolean isSourceSelfReferencing() { 212 return sourceField.getName().equals(MapperConstants.SELF_KEYWORD); 213 } 214 215 public boolean getCopyByReferenceOveridden() { 216 return copyByReferenceOveridden; 217 } 218 219 public void setCopyByReferenceOveridden(boolean copyByReferenceOveridden) { 220 this.copyByReferenceOveridden = copyByReferenceOveridden; 221 } 222 223 public String getMapId() { 224 return mapId; 225 } 226 227 public void setMapId(String mapId) { 228 this.mapId = mapId; 229 } 230 231 public boolean isGenericFieldMap() { 232 return this instanceof GenericFieldMap ? true : false; 233 } 234 235 public String getCustomConverter() { 236 return customConverter; 237 } 238 239 public void setCustomConverter(String customConverter) { 240 this.customConverter = customConverter; 241 } 242 } | Popular Tags |