01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings.util;
14:
15: import java.lang.reflect.Method;
16:
17: /**
18: * little helper class to access properties via introspection. Quick
19: * hack, not yet implemneted completely.
20: */
21: public class PropertyAccessor {
22: /**
23: * determines.
24: */
25: public static boolean hasProperty(Object o, String name) {
26: try {
27: Method[] m = o.getClass().getMethods();
28: String setterName = "set" + capitalize(name);
29: for (int i = 0; i < m.length; ++i) {
30: if (m[i].getParameterTypes().length == 1
31: && m[i].getName().equals(setterName)) {
32: // (maybe check, whether there is a getter here).
33: return true;
34: }
35: }
36: } catch (Exception e) {
37: }
38: return false;
39: }
40:
41: public static void setProperty(Object o, String name, Object value) {
42: try {
43: Method[] m = o.getClass().getMethods();
44: String setterName = "set" + capitalize(name);
45: for (int i = 0; i < m.length; ++i) {
46: if (m[i].getParameterTypes().length == 1
47: && m[i].getName().equals(setterName)
48: && (value == null || (m[i].getParameterTypes()[0]
49: .isAssignableFrom(value.getClass())))) {
50: m[i].invoke(o, new Object[] { value });
51: }
52: }
53: } catch (Exception e) {
54: }
55: }
56:
57: public static void getProperty(Object o, String name, Object value) {
58: // not yet implemented.
59: }
60:
61: /**
62: * captialize a string. A String 'foo' becomes 'Foo'. Used to
63: * derive names of getters from the property name.
64: */
65: private static String capitalize(String s) {
66: s = s.trim();
67: return s.substring(0, 1).toUpperCase() + s.substring(1);
68: }
69: }
|