01: package org.jicengine.operation;
02:
03: import java.lang.reflect.InvocationTargetException;
04: import java.lang.reflect.Method;
05: import java.lang.reflect.Constructor;
06: import java.lang.reflect.Field;
07:
08: /**
09: * Utilities for manipulating beans through reflection.
10: *
11: * <p>
12: * Copyright (C) 2004 Timo Laitinen
13: * </p>
14: * @author Timo Laitinen
15: * @created 2004-09-20
16: * @since JICE-0.10
17: */
18: public class BeanUtils extends ReflectionUtils {
19: private static final Object[] EMPTY_ARRAY = new Object[0];
20:
21: /**
22: * Sets the value of a property.
23: *
24: * @param instance the instance whose property is set.
25: * @param propertyName the name of the property to be set.
26: * @param value the new value of the property.
27: */
28: public static void setProperty(Object instance,
29: String propertyName, Object value)
30: throws java.lang.NoSuchMethodException,
31: IllegalAccessException, IllegalArgumentException,
32: InvocationTargetException {
33: String setterName = toSetterMethodName(propertyName);
34: invokeMethod(instance, setterName, new Object[] { value });
35: }
36:
37: /**
38: * Transforms a property-name to a corresponding getter-method name.
39: * for example 'name' -> 'getName'
40: */
41: public static String toGetterMethodName(String property) {
42: return "get" + Character.toUpperCase(property.charAt(0))
43: + property.substring(1);
44: }
45:
46: /**
47: * Transforms a property-name to a corresponding setter-method name.
48: * for example 'name' -> 'setName'
49: */
50: public static String toSetterMethodName(String property) {
51: return "set" + Character.toUpperCase(property.charAt(0))
52: + property.substring(1);
53: }
54:
55: /**
56: * Returns the value of a property.
57: * throws NoSuchMethodException if the property doesn't exist.
58: */
59: public static Object getProperty(Object instance,
60: String propertyName)
61: throws java.lang.NoSuchMethodException,
62: InvocationTargetException, IllegalAccessException {
63: String getterName = toGetterMethodName(propertyName);
64: return invokeMethod(instance, getterName, EMPTY_ARRAY);
65: }
66: }
|