KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > operation > BeanUtils


1 package org.jicengine.operation;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Constructor JavaDoc;
6 import java.lang.reflect.Field JavaDoc;
7
8 /**
9  * 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 JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[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 JavaDoc instance, String JavaDoc propertyName, Object JavaDoc value) throws java.lang.NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc
29     {
30         String JavaDoc setterName = toSetterMethodName(propertyName);
31         invokeMethod(instance, setterName, new Object JavaDoc[]{value});
32     }
33
34     /**
35      * Transforms a property-name to a corresponding getter-method name.
36      * for example 'name' -> 'getName'
37      */

38     public static String JavaDoc toGetterMethodName(String JavaDoc property){
39         return "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
40     }
41
42     /**
43      * Transforms a property-name to a corresponding setter-method name.
44      * for example 'name' -> 'setName'
45      */

46     public static String JavaDoc toSetterMethodName(String JavaDoc property){
47         return "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
48     }
49
50     /**
51      * Returns the value of a property.
52      * throws NoSuchMethodException if the property doesn't exist.
53      */

54     public static Object JavaDoc getProperty(Object JavaDoc instance, String JavaDoc propertyName) throws java.lang.NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
55         String JavaDoc getterName = toGetterMethodName(propertyName);
56         return invokeMethod(instance, getterName, EMPTY_ARRAY);
57     }
58 }
59
Popular Tags