KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > propertydescriptor > FieldPropertyDescriptor


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.propertydescriptor;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
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 /**
26  * @author garsombke.franz
27  */

28 public class FieldPropertyDescriptor implements DozerPropertyDescriptorIF {
29
30   private final Field JavaDoc field;
31
32   public FieldPropertyDescriptor(Class JavaDoc bean, String JavaDoc fieldName, boolean isAccessible) throws NoSuchFieldException JavaDoc {
33     field = getFieldFromBean(bean, fieldName);
34     //Allow access to private instance var's that dont have public setter. "is-accessible=true" must be explicitly specified in
35
//mapping file to allow this access. setAccessible indicates intent to bypass field protections.
36
field.setAccessible(isAccessible);
37   }
38
39   public Class JavaDoc getPropertyType(Class JavaDoc clazz) {
40     return field.getType();
41   }
42
43   public void setPropertyValue(Object JavaDoc bean, Object JavaDoc value, Hint hint, ClassMap classMap) {
44     try {
45       field.set(bean, value);
46     } catch (Exception JavaDoc e) {
47       throw new MappingException(e);
48     }
49   }
50
51   public Object JavaDoc getPropertyValue(Object JavaDoc bean) {
52     Object JavaDoc o = null;
53     try {
54       o = field.get(bean);
55     } catch (Exception JavaDoc e) {
56       throw new MappingException(e);
57     }
58     return o;
59   }
60
61   public String JavaDoc getReadMethodName(Class JavaDoc clazz) {
62    return "get" + field.getName();
63   }
64
65   public String JavaDoc getWriteMethodName(Class JavaDoc clazz) {
66     return "set" + field.getName();
67   }
68
69   public Method JavaDoc getReadMethod(Class JavaDoc clazz) {
70     throw new UnsupportedOperationException JavaDoc();
71   }
72
73   public Method JavaDoc getWriteMethod(Class JavaDoc clazz) {
74     throw new UnsupportedOperationException JavaDoc();
75   }
76   
77   private Field JavaDoc getFieldFromBean(Class JavaDoc clazz, String JavaDoc fieldName) throws NoSuchFieldException JavaDoc {
78     try {
79       return clazz.getDeclaredField(fieldName);
80     } catch (NoSuchFieldException JavaDoc e) {
81       if (clazz.getSuperclass() != null) {
82         return getFieldFromBean(clazz.getSuperclass(), fieldName);
83       }
84       throw e;
85     }
86   }
87   
88 }
89
Popular Tags