KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > javabean > BeanProperty


1 package com.thoughtworks.xstream.converters.javabean;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
6
7 /**
8  * Provide access to a bean property.
9  *
10  * @author <a HREF="mailto:andrea.aime@aliceposta.it">Andrea Aime</a>
11  */

12 public class BeanProperty {
13
14     /** the target class */
15     private Class JavaDoc memberClass;
16
17     /** the property name */
18     private String JavaDoc propertyName;
19
20     /** the property type */
21     private Class JavaDoc type;
22
23     /** the getter */
24     protected Method JavaDoc getter;
25
26     /** the setter */
27     private Method JavaDoc setter;
28
29     /**
30      * Creates a new {@link BeanProperty}that gets the specified property from
31      * the specified class.
32      */

33     public BeanProperty(Class JavaDoc memberClass, String JavaDoc propertyName, Class JavaDoc propertyType) {
34         this.memberClass = memberClass;
35         this.propertyName = propertyName;
36         this.type = propertyType;
37     }
38
39     /**
40      * Gets the base class that this getter accesses.
41      */

42     public Class JavaDoc getBeanClass() {
43         return memberClass;
44     }
45
46     /**
47      * Returns the property type
48      *
49      * @return
50      */

51     public Class JavaDoc getType() {
52         return type;
53     }
54
55     /**
56      * Gets the name of the property that this getter extracts.
57      */

58     public String JavaDoc getName() {
59         return propertyName;
60     }
61
62     /**
63      * Gets whether this property can get get.
64      */

65     public boolean isReadable() {
66         return (getter != null);
67     }
68
69     /**
70      * Gets whether this property can be set.
71      */

72     public boolean isWritable() {
73         return (setter != null);
74     }
75
76     /**
77      * Gets the value of this property for the specified Object.
78      * @throws IllegalAccessException
79      * @throws IllegalArgumentException
80      */

81     public Object JavaDoc get(Object JavaDoc member) throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc {
82         if (!isReadable())
83             throw new IllegalStateException JavaDoc("Property " + propertyName + " of " + memberClass
84                     + " not readable");
85
86         try {
87             return getter.invoke(member, null);
88         } catch (InvocationTargetException JavaDoc e) {
89             throw new UndeclaredThrowableException JavaDoc(e.getTargetException());
90         }
91     }
92
93     /**
94      * Sets the value of this property for the specified Object.
95      * @throws IllegalAccessException
96      * @throws IllegalArgumentException
97      */

98     public Object JavaDoc set(Object JavaDoc member, Object JavaDoc newValue) throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc {
99         if (!isWritable())
100             throw new IllegalStateException JavaDoc("Property " + propertyName + " of " + memberClass
101                     + " not writable");
102
103         try {
104             return setter.invoke(member, new Object JavaDoc[] { newValue });
105         } catch (InvocationTargetException JavaDoc e) {
106             throw new UndeclaredThrowableException JavaDoc(e.getTargetException());
107         }
108     }
109
110     /**
111      * @param method
112      */

113     public void setGetterMethod(Method JavaDoc method) {
114         this.getter = method;
115
116     }
117
118     /**
119      * @param method
120      */

121     public void setSetterMethod(Method JavaDoc method) {
122         this.setter = method;
123     }
124 }
Popular Tags