01: /*
02: * Copyright 2005-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.propertydescriptor;
17:
18: import java.beans.PropertyDescriptor;
19: import java.lang.reflect.Method;
20:
21: import net.sf.dozer.util.mapping.fieldmap.HintContainer;
22: import net.sf.dozer.util.mapping.util.MappingUtils;
23: import net.sf.dozer.util.mapping.util.ReflectionUtils;
24:
25: /**
26: *
27: * Internal class used to read and write values for fields that follow the java bean spec and have corresponding
28: * getter/setter methods for the field that are name accordingly. If the field does not have the necessary
29: * getter/setter, an exception will be thrown. Only intended for internal use.
30: *
31: * @author garsombke.franz
32: * @author tierney.matt
33: */
34: public class JavaBeanPropertyDescriptor extends
35: GetterSetterPropertyDescriptor {
36: private PropertyDescriptor pd;
37:
38: public JavaBeanPropertyDescriptor(Class clazz, String fieldName,
39: boolean isIndexed, int index,
40: HintContainer srcDeepIndexHintContainer,
41: HintContainer destDeepIndexHintContainer) {
42: super (clazz, fieldName, isIndexed, index,
43: srcDeepIndexHintContainer, destDeepIndexHintContainer);
44: }
45:
46: public Method getWriteMethod() throws NoSuchMethodException {
47: Method writeMethod = getPropertyDescriptor(
48: destDeepIndexHintContainer).getWriteMethod();
49: if (writeMethod == null) {
50: throw new NoSuchMethodException(
51: "Unable to determine write method for Field: "
52: + fieldName + " in Class: " + clazz);
53: }
54:
55: return writeMethod;
56: }
57:
58: protected String getSetMethodName() throws NoSuchMethodException {
59: return getWriteMethod().getName();
60: }
61:
62: protected Method getReadMethod() throws NoSuchMethodException {
63: Method result = getPropertyDescriptor(srcDeepIndexHintContainer)
64: .getReadMethod();
65: if (result == null) {
66: throw new NoSuchMethodException(
67: "Unable to determine read method for Field: "
68: + fieldName + " in Class: " + clazz);
69: }
70: return result;
71: }
72:
73: private PropertyDescriptor getPropertyDescriptor(
74: HintContainer deepIndexHintContainer) {
75: if (pd == null) {
76: pd = ReflectionUtils.findPropertyDescriptor(clazz,
77: fieldName, deepIndexHintContainer);
78: if (pd == null) {
79: MappingUtils.throwMappingException("Property: "
80: + fieldName + " not found in Class: " + clazz);
81: }
82: }
83: return pd;
84: }
85:
86: }
|