View Javadoc

1   /***
2    * Copyright 2006 Joseph M. Ferner
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 com.fernsroth.easyio.util;
17  
18  import java.lang.reflect.Field;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import com.fernsroth.easyio.exception.GetterNotFoundException;
26  import com.fernsroth.easyio.exception.SetterNotFoundException;
27  
28  /***
29   * 
30   * @author Joseph M. Ferner (Near Infinity Corporation)
31   */
32  public final class BeanUtils {
33      /***
34       * hidden constructor.
35       */
36      private BeanUtils() {
37          // empty
38      }
39  
40      /***
41       * gets a named value from an object.
42       * @param obj the object to get the value from.
43       * @param field the field to set.
44       * @return the value of the named field.
45       * @throws GetterNotFoundException 
46       */
47      public static Object getValue(Object obj, Field field)
48              throws GetterNotFoundException {
49          Class<?> clazz = obj.getClass();
50  
51          // get getter
52          Method method = findGetterMethod(clazz, field.getName());
53          try {
54              if (method != null) {
55                  return method.invoke(obj, new Object[] {});
56              } else {
57                  return field.get(obj);
58              }
59          } catch (IllegalArgumentException e) {
60              throw new GetterNotFoundException(clazz, field.getName(), e);
61          } catch (IllegalAccessException e) {
62              throw new GetterNotFoundException(clazz, field.getName(), e);
63          } catch (InvocationTargetException e) {
64              throw new GetterNotFoundException(clazz, field.getName(), e);
65          }
66      }
67  
68      /***
69       * finds a getter method in a class.
70       * @param clazz the class to get the metter method from.
71       * @param name the name of the field.
72       * @return the getter method. null, if not found.
73       */
74      public static Method findGetterMethod(Class<?> clazz, String name) {
75          for (Method method : clazz.getMethods()) {
76              String methodName = method.getName();
77              if (methodName.startsWith("get")) {
78                  if (methodName.substring("get".length()).equalsIgnoreCase(name)) {
79                      return method;
80                  }
81              }
82          }
83          return null;
84      }
85  
86      /***
87       * gets a list of all the fields including base classes.
88       * @param clazz the class to read.
89       * @return the fields.
90       */
91      public static List<Field> getAllFields(Class<?> clazz) {
92          List<Field> fields = new ArrayList<Field>();
93          Class<?> superclass = clazz.getSuperclass();
94          if (superclass != null && superclass != Object.class) {
95              fields.addAll(getAllFields(superclass));
96          }
97          fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
98          return fields;
99      }
100 
101     /***
102      * sets a value into an object.
103      * @param obj the object to the value into.
104      * @param field the field name.
105      * @param val the value.
106      * @throws SetterNotFoundException  
107      */
108     public static void setValue(Object obj, Field field, Object val)
109             throws SetterNotFoundException {
110         Class<?> clazz = obj.getClass();
111 
112         // get setter
113         Method method = findSetterMethod(clazz, field.getName());
114         try {
115             if (method != null) {
116                 method.invoke(obj, new Object[] { val });
117             } else {
118                 field.set(obj, val);
119             }
120         } catch (IllegalArgumentException e) {
121             throw new SetterNotFoundException(clazz, field.getName(), e);
122         } catch (IllegalAccessException e) {
123             throw new SetterNotFoundException(clazz, field.getName(), e);
124         } catch (InvocationTargetException e) {
125             throw new SetterNotFoundException(clazz, field.getName(), e);
126         }
127     }
128 
129     /***
130      * finds a setter method in the class.
131      * @param clazz the class to search.
132      * @param name the name of the field.
133      * @return the setter method.
134      */
135     public static Method findSetterMethod(Class<?> clazz, String name) {
136         for (Method method : clazz.getMethods()) {
137             String methodName = method.getName();
138             if (methodName.startsWith("set")) {
139                 if (methodName.substring("set".length()).equalsIgnoreCase(name)) {
140                     return method;
141                 }
142             }
143         }
144         return null;
145     }
146 
147 }