KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > fieldmap > FieldMap


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.fieldmap;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
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 /**
30  * @author garsombke.franz
31  * @author sullins.ben
32  * @author tierney.matt
33  *
34  */

35 public abstract class FieldMap implements Cloneable JavaDoc {
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 JavaDoc type;
43   private boolean copyByReference;
44   private boolean copyByReferenceOveridden;
45   private String JavaDoc mapId;
46   private String JavaDoc customConverter;
47
48   private final MappingUtils mappingUtils = new MappingUtils();
49   
50   private DozerPropertyDescriptorIF getSourcePropertyDescriptor(Class JavaDoc sourceClass) throws NoSuchFieldException JavaDoc {
51     return PropertyDescriptorFactory.getPropertyDescriptor(sourceField, sourceClass);
52   }
53   
54   private DozerPropertyDescriptorIF getDestinationPropertyDescriptor(Class JavaDoc destClass) throws NoSuchFieldException JavaDoc {
55     return PropertyDescriptorFactory.getPropertyDescriptor(destField, destClass);
56   }
57
58   public String JavaDoc getSourceFieldReadMethodName(Class JavaDoc sourceClass) throws NoSuchFieldException JavaDoc {
59     return getSourcePropertyDescriptor(sourceClass).getReadMethodName(sourceClass);
60   }
61
62   public String JavaDoc getDestFieldReadMethodName(Class JavaDoc destClass) throws NoSuchFieldException JavaDoc {
63     return getDestinationPropertyDescriptor(destClass).getReadMethodName(destClass);
64   }
65
66   public Class JavaDoc getDestHintType(Class JavaDoc 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 JavaDoc getDestFieldType(Class JavaDoc destClass) throws NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc,
79       NoSuchFieldException JavaDoc {
80     return getDestinationPropertyDescriptor(destClass).getPropertyType(destClass);
81   }
82   
83   public Class JavaDoc getSourceFieldType(Class JavaDoc srcClass) throws NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc,
84       NoSuchFieldException JavaDoc {
85     return getSourcePropertyDescriptor(srcClass).getPropertyType(srcClass);
86   }
87
88   public Method JavaDoc getDestFieldWriteMethod(Class JavaDoc destClass) throws NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc,
89       NoSuchFieldException JavaDoc {
90     return getDestinationPropertyDescriptor(destClass).getWriteMethod(destClass);
91   }
92
93   public Object JavaDoc getSrcFieldValue(Object JavaDoc srcObj) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
94       NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc, NoSuchFieldException JavaDoc {
95     // this is mainly for Maps...we cant use the selfdescriptor...so we have to cheat and use this
96
if (isSourceSelfReferencing()) {
97       return srcObj;
98     }
99     return getSourcePropertyDescriptor(srcObj.getClass()).getPropertyValue(srcObj);
100   }
101
102   public void writeDestinationValue(Object JavaDoc destObj, Object JavaDoc destFieldValue, ClassMap classMap)
103       throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, InstantiationException JavaDoc, NoSuchMethodException JavaDoc,
104       ClassNotFoundException JavaDoc, NoSuchFieldException JavaDoc {
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 JavaDoc getDestinationObject(Object JavaDoc destObj) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
114       InstantiationException JavaDoc, NoSuchFieldException JavaDoc {
115     return getDestinationPropertyDescriptor(destObj.getClass()).getPropertyValue(destObj);
116   }
117   
118   public Object JavaDoc doesFieldExist(Object JavaDoc destObj, Class JavaDoc destClass) throws IllegalAccessException JavaDoc,
119       InvocationTargetException JavaDoc, InstantiationException JavaDoc, NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc, NoSuchFieldException JavaDoc {
120     Object JavaDoc field = null;
121     if (!isGenericFieldMap()) {
122       //then do no validation
123
} else {
124       // call the getXX method to see if the field is already instantiated
125
// for deep mapping need the 'real' destination class.
126
if (destClass == null) {
127         destClass = destObj.getClass();
128       }
129       field = getDestinationObject(destObj);
130     }
131     return field;
132   }
133   
134   public String JavaDoc getDestKey() {
135     String JavaDoc 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 JavaDoc getSourceKey() {
145     String JavaDoc 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
187     return super.clone();
188   }
189
190   public String JavaDoc getType() {
191     return type;
192   }
193
194   public void setType(String JavaDoc 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   /**
208    * Return true if is self referencing. Is considered self referencing where no other sources are specified, i.e., no
209    * source properties or #CDATA in the xml def.
210    */

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 JavaDoc getMapId() {
224     return mapId;
225   }
226
227   public void setMapId(String JavaDoc mapId) {
228     this.mapId = mapId;
229   }
230   
231   public boolean isGenericFieldMap() {
232     return this instanceof GenericFieldMap ? true : false;
233   }
234
235   public String JavaDoc getCustomConverter() {
236     return customConverter;
237   }
238
239   public void setCustomConverter(String JavaDoc customConverter) {
240     this.customConverter = customConverter;
241   }
242 }
Popular Tags