View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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  
17  package net.sf.ehcache.hibernate.management.impl;
18  
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Method;
21  
22  /***
23   * Reflective utilities for dealing with backward-incompatible change to statistics types in Hibernate 3.5.
24   *
25   * @author gkeim
26   */
27  public class BeanUtils {
28    /***
29     * Return the named getter method on the bean or null if not found.
30     * 
31     * @param bean
32     * @param propertyName
33     * @return the named getter method
34     */
35    private static Method getMethod(Object bean, String propertyName) {
36      StringBuilder sb = new StringBuilder("get").append(Character.toUpperCase(propertyName.charAt(0)));
37      if (propertyName.length() > 1) {
38        sb.append(propertyName.substring(1));
39      }
40      String getterName = sb.toString();
41      for (Method m : bean.getClass().getMethods()) {
42        if (getterName.equals(m.getName()) && m.getParameterTypes().length == 0) {
43          return m;
44        }
45      }
46      return null;
47    }
48  
49    /***
50     * Return the named field on the bean or null if not found.
51     * 
52     * @param bean
53     * @param propertyName
54     * @return the named field
55     */
56    private static Field getField(Object bean, String propertyName) {
57      for (Field f : bean.getClass().getDeclaredFields()) {
58        if (propertyName.equals(f.getName())) {
59          return f;
60        }
61      }
62      return null;
63    }
64    
65    private static void validateArgs(Object bean, String propertyName) {
66      if (bean == null) {
67        throw new IllegalArgumentException("bean is null");
68      }
69      if (propertyName == null) {
70        throw new IllegalArgumentException("propertyName is null");
71      }
72      if (propertyName.trim().length() == 0) {
73        throw new IllegalArgumentException("propertyName is empty");
74      }
75    }
76    
77    /***
78     * Retrieve a named bean property value.
79     * 
80     * @param bean bean
81     * @param propertyName
82     * @return the property value
83     */
84    public static Object getBeanProperty(Object bean, String propertyName) {
85      validateArgs(bean, propertyName);
86      
87      // try getters first
88      Method getter = getMethod(bean, propertyName);
89      if (getter != null) {
90        try {
91          return getter.invoke(bean);
92        } catch (Exception e) {
93          /***/
94        }
95      }
96      
97      // then try fields
98      Field field = getField(bean, propertyName);
99      if (field != null) {
100       try {
101         field.setAccessible(true);
102         return field.get(bean);
103       } catch (Exception e) {
104         /***/
105       }
106     }
107         
108     return null;
109   }
110     
111   /***
112    * Retrieve a Long bean property value.
113    * 
114    * @param bean bean
115    * @param propertyName
116    * @return long value
117    * @throws NoSuchFieldException
118    */
119   public static long getLongBeanProperty(final Object bean, final String propertyName) throws NoSuchFieldException {
120     validateArgs(bean, propertyName);
121     Object o = getBeanProperty(bean, propertyName);
122     if (o == null) {
123       throw new NoSuchFieldException(propertyName);
124     } else if (!(o instanceof Number)) {
125       throw new IllegalArgumentException(propertyName + " not an Number");
126     }
127     return ((Number)o).longValue();
128   }
129 }