View Javadoc

1   package org.apache.commons.betwixt;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // todo: add updater
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 }