Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jdaemon/util/BeanAccessor.java


1   /*
2    * BeanAccessor.java
3    *
4    *  Copyright (C) 2002 [Author Name]
5    *  This program is distributed under the terms of the Lesser GNU General Public
6    *  License (v2 or later) per the included COPYING.txt file, or see www.fsf.org.
7    *
8    * Created on July 25, 2002
9    */
10  
11  package org.jdaemon.util;
12  
13  import java.lang.reflect.*;
14  import java.util.HashMap;
15  import java.util.Iterator;
16  
17  /**
18   * Accessor which allows items stored in a bean-like object to be given symbolic names.
19   * Every symbolic name will have to have a corresponding 'get' method. For example, an attribute
20   * <I>AccountName</I> will have to have a corresponding <I>getAccountName</I> method.
21   *
22   * @author Jan Stanger
23   */
24  public class BeanAccessor implements Accessor {
25      
26      private Class my_class;
27      private HashMap get_methods = new HashMap();
28      
29      /** Creates a new instance of BeanAccessor.
30       *
31       *  All no-arg methods which have 'get' as the first three letters of the method
32       *  name are undestood to be attribute accessors. The attribute name is taken to 
33       *  be the method name less the 'get' prefix. 
34       *
35       *  @param my_class Class of object for which this accessor is to work
36       */
37      public BeanAccessor(Class my_class) {
38          
39          this.my_class = my_class;
40          
41          Method[] methods = my_class.getMethods();
42          
43          for (int i = 0; i < methods.length; i++) {
44              Method method = methods[i];
45              if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
46                  get_methods.put(method.getName().substring(3), method);
47              }
48          }
49      }
50      
51      /** Creates a new instance of BeanAccessor.
52       *
53       *  This constuctor searches the given class for an accessor method for every attribute
54       *  specified in the <I>attributes</I> array. If there is no such method for any
55       *  attribute in the array, an exception is thrown.
56       *
57       *  @param my_class Class of object for which this accessor is to work
58       *  @param attributes List of desired attributes
59       *  @throws NoSuchMethodException if an accessor for any given attribute does not exist in <I>my_class</I>
60       */
61      public BeanAccessor(Class my_class, String[] attributes) throws java.lang.NoSuchMethodException {
62          this.my_class = my_class;
63          
64          for (int i = 0; i < attributes.length; i++) {
65              get_methods.put(attributes[i], my_class.getMethod("get" + attributes[i], null));
66          }
67      }
68      
69      /**
70       * Retrieve the attribute value corresponding to an attribute name 
71       * from specific data object. 
72       *
73       * @param name The name of the attribute
74       * @param obj  The object representation a row of data (has to have a 'get'-
75       *             method corresponding to the attribute name)
76       * @return Attribute value as Object
77       */
78      public Object getAttribute(String name, Object obj) {
79          Object retVal = null;
80          
81          //Using reflection to call 'get'-method corresponding to the attribute name
82          try {
83              Method m = (Method)get_methods.get(name);
84              if (m == null) throw new IllegalArgumentException("No method get"+name+" in class " + my_class);
85              retVal = m.invoke(obj, null);
86          }
87          //TO-DO: Implement better exception handling!
88          catch (IllegalAccessException iae) {
89              iae.printStackTrace();
90          }
91          catch (InvocationTargetException ite) {
92              ite.printStackTrace();
93          }
94          
95          return retVal;
96      }    
97      
98      /** Return an iterator over all the attributes understood by this Accessor.
99       *
100      * An attribute name is understood by this accessor if a get method matching the
101      * attribute name exists in the class specified in the constructor. 
102      *
103      * @return An Iterator over all understood attribute names
104      */
105     public Iterator getAttributeNames() {
106         return get_methods.keySet().iterator();
107     }
108 
109    /** Not currently supported.
110     *
111     * @param name the name of the required attribute
112     * @param bean the object in which the attribute is to be set
113     * @param value the new value of the the attribute 
114     */
115     public void setAttribute(String name, Object bean, Object value) {
116         throw new UnsupportedOperationException("BeanAccessor does not currently support setAttribute");
117     }      
118 }