01: /**
02: * L2FProd.com Common Components 7.3 License.
03: *
04: * Copyright 2005-2007 L2FProd.com
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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: */package com.l2fprod.common.beans;
18:
19: import java.lang.reflect.Method;
20:
21: /**
22: * BeanUtils. <br>
23: *
24: */
25: public class BeanUtils {
26:
27: private BeanUtils() {
28: }
29:
30: public static Method getReadMethod(Class clazz, String propertyName) {
31: Method readMethod = null;
32: String base = capitalize(propertyName);
33:
34: // Since there can be multiple setter methods but only one getter
35: // method, find the getter method first so that you know what the
36: // property type is. For booleans, there can be "is" and "get"
37: // methods. If an "is" method exists, this is the official
38: // reader method so look for this one first.
39: try {
40: readMethod = clazz.getMethod("is" + base, null);
41: } catch (Exception getterExc) {
42: try {
43: // no "is" method, so look for a "get" method.
44: readMethod = clazz.getMethod("get" + base, null);
45: } catch (Exception e) {
46: // no is and no get, we will return null
47: }
48: }
49:
50: return readMethod;
51: }
52:
53: public static Method getWriteMethod(Class clazz,
54: String propertyName, Class propertyType) {
55: Method writeMethod = null;
56: String base = capitalize(propertyName);
57:
58: Class params[] = { propertyType };
59: try {
60: writeMethod = clazz.getMethod("set" + base, params);
61: } catch (Exception e) {
62: // no write method
63: }
64:
65: return writeMethod;
66: }
67:
68: private static String capitalize(String s) {
69: if (s.length() == 0) {
70: return s;
71: } else {
72: char chars[] = s.toCharArray();
73: chars[0] = Character.toUpperCase(chars[0]);
74: return String.valueOf(chars);
75: }
76: }
77:
78: }
|