1 package org.apache.commons.betwixt;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.beans.PropertyDescriptor;
20 import java.lang.reflect.Method;
21
22 import org.apache.commons.beanutils.DynaProperty;
23 import org.apache.commons.betwixt.expression.DynaBeanExpression;
24 import org.apache.commons.betwixt.expression.Expression;
25 import org.apache.commons.betwixt.expression.MethodExpression;
26 import org.apache.commons.betwixt.expression.MethodUpdater;
27 import org.apache.commons.betwixt.expression.Updater;
28
29 /***
30 * Betwixt-centric view of a bean (or pseudo-bean) property.
31 * This object decouples the way that the (possibly pseudo) property introspection
32 * is performed from the results of that introspection.
33 *
34 * @author Robert Burrell Donkin
35 * @since 0.5
36 */
37 public class BeanProperty {
38
39 /*** The bean name for the property (not null) */
40 private String propertyName;
41 /*** The type of this property (not null) */
42 private Class propertyType;
43 /*** The Expression used to read values of this property (possibly null) */
44 private Expression propertyExpression;
45 /*** The Updater used to write values of this property (possibly null) */
46 private Updater propertyUpdater;
47
48 /***
49 * Construct a BeanProperty.
50 * @param propertyName not null
51 * @param propertyType not null
52 * @param propertyExpression the Expression used to read the property,
53 * null if the property is not readable
54 * @param propertyUpdater the Updater used to write the property,
55 * null if the property is not writable
56 */
57 public BeanProperty (
58 String propertyName,
59 Class propertyType,
60 Expression propertyExpression,
61 Updater propertyUpdater) {
62 this.propertyName = propertyName;
63 this.propertyType = propertyType;
64 this.propertyExpression = propertyExpression;
65 this.propertyUpdater = propertyUpdater;
66 }
67
68 /***
69 * Constructs a BeanProperty from a <code>PropertyDescriptor</code>.
70 * @param descriptor not null
71 */
72 public BeanProperty(PropertyDescriptor descriptor) {
73 this.propertyName = descriptor.getName();
74 this.propertyType = descriptor.getPropertyType();
75
76 Method readMethod = descriptor.getReadMethod();
77 if ( readMethod != null ) {
78 this.propertyExpression = new MethodExpression( readMethod );
79 }
80
81 Method writeMethod = descriptor.getWriteMethod();
82 if ( writeMethod != null ) {
83 this.propertyUpdater = new MethodUpdater( writeMethod );
84 }
85 }
86
87 /***
88 * Constructs a BeanProperty from a <code>DynaProperty</code>
89 * @param dynaProperty not null
90 */
91 public BeanProperty(DynaProperty dynaProperty) {
92 this.propertyName = dynaProperty.getName();
93 this.propertyType = dynaProperty.getType();
94 this.propertyExpression = new DynaBeanExpression( propertyName );
95
96 }
97
98 /***
99 * Gets the bean name for this property.
100 * Betwixt will map this to an xml name.
101 * @return the bean name for this property, not null
102 */
103 public String getPropertyName() {
104 return propertyName;
105 }
106
107 /***
108 * Gets the type of this property.
109 * @return the property type, not null
110 */
111 public Class getPropertyType() {
112 return propertyType;
113 }
114
115 /***
116 * Gets the expression used to read this property.
117 * @return the expression to be used to read this property
118 * or null if this property is not readable.
119 */
120 public Expression getPropertyExpression() {
121 return propertyExpression;
122 }
123
124 /***
125 * Gets the updater used to write to this properyty.
126 * @return the Updater to the used to write to this property
127 * or null if this property is not writable.
128 */
129 public Updater getPropertyUpdater() {
130 return propertyUpdater;
131 }
132 }