1 /*
2 * Replica is published under the terms
3 * of the Apache Software License.
4 */
5 package replica.utils;
6
7 import java.beans.BeanInfo;
8 import java.beans.IntrospectionException;
9 import java.beans.Introspector;
10 import java.beans.PropertyDescriptor;
11 import java.lang.reflect.InvocationTargetException;
12 import java.lang.reflect.Method;
13 import java.util.Hashtable;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16
17 /***
18 * Generic class that contain several static helper methods to work with beans.
19 *
20 * It also uses cache for faster lookups.
21 *
22 * @author Pedro Costa
23 * @author Helder Silva
24 * @since 17/Jan/2004
25 */
26 public abstract class BeanUtils {
27
28 private static Logger logger = Logger.getLogger(BeanUtils.class.getName());
29
30 // the key is fullClassName.propertyName, the value is propertyName write method
31 static Hashtable writeMethods = new Hashtable();
32
33 /***
34 * Method to write a property in a bean class. It looks for a
35 * @param bean
36 * @param propName
37 * @param propValue
38 * @throws UncheckedBeansException if there is a problem acessing the bean.
39 * The root cause Exception can be recovered using the getCause Method.
40 */
41 public static void writeProperty(Object bean, String propName, String propValue){
42
43 Class clazz = bean.getClass();
44 String key = clazz.getName() + "." + propName;
45
46 Method writeMethod = (Method)writeMethods.get( key );
47
48 if( writeMethod == null ){
49
50 writeMethod = getWriteMethod( clazz, propName );
51
52 if( writeMethod == null ){
53 logger.log(Level.WARNING, "No write method found for property ["+propName+"] on class ["+clazz.getName()+
54 "] or property is not a Java primitive type or String type.");
55 return;
56 }
57
58 writeMethods.put( key, writeMethod );
59 }
60
61 try{
62 writeMethod.invoke(bean, new Object[]{
63 convertStringToType(propValue, writeMethod.getParameterTypes()[0] ) } );
64 }
65 catch(IllegalAccessException e){
66 throw new UncheckedBeansException(e);
67 }
68 catch(InvocationTargetException e){
69 throw new UncheckedBeansException(e);
70 }
71 }
72
73 /***
74 * Converts a string value to a primitive type value (or String).
75 * @param value the string
76 * @param type the new value type
77 * @return the converted value
78 */
79 public static Object convertStringToType( String value, Class type ){
80
81 if( !isPrimitiveType(type) )
82 throw new IllegalArgumentException("Type [" + type.getName() + "] is not a Java Primitive type or String.");
83
84 if( type == String.class )
85 return value;
86
87 if( type == byte.class || type == Byte.class )
88 return Byte.valueOf(value);
89 if( type == short.class || type == Short.class )
90 return Short.valueOf(value);
91 if( type == int.class || type == Integer.class )
92 return Integer.valueOf(value);
93 if( type == long.class || type == Long.class )
94 return Long.valueOf(value);
95
96 if( type == float.class || type == Float.class )
97 return Float.valueOf(value);
98 if( type == double.class || type == Double.class )
99 return Double.valueOf(value);
100
101 if( type == boolean.class || type == Boolean.class )
102 return Boolean.valueOf(value);
103
104 if( type == char.class || type == Character.class ){
105 if( value != null && value.length() > 0 )
106 return new Character( value.toCharArray()[0] );
107 }
108
109 return null;
110 }
111
112 /***
113 * @return the write method for a property with the given name.
114 * It only returns write methods for Java primitive types or String.
115 * If no method is found in this condition, returns null.
116 * @throws UncheckedBeansException if an IntrospectionException is catched.
117 * Calling methods can choose to catch this or not. The root cause can be retrieved
118 * from this exception using the getCause method.
119 */
120 public static Method getWriteMethod( Class clazz, String propName ){
121
122 try{
123 BeanInfo beanInfo = Introspector.getBeanInfo( clazz );
124
125 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
126
127 for (int i = 0; i < propertyDescriptors.length; i++) {
128 if( propertyDescriptors[i].getName().equals(propName) ){
129 Method writeMethod = propertyDescriptors[i].getWriteMethod();
130 if( writeMethod != null ){
131 if( isPrimitiveType( propertyDescriptors[i].getPropertyType() ) )
132 return writeMethod;
133 }
134 }
135 }
136
137 // no write method found for property or not a primitive type.
138 return null;
139 }
140 catch(IntrospectionException e){
141 throw new UncheckedBeansException(e);
142 }
143 }
144
145 public static boolean isPrimitiveType( Class clazz ){
146
147 if( clazz == byte.class || clazz == Byte.class || clazz == short.class || clazz == Short.class ||
148 clazz == int.class || clazz == Integer.class || clazz == long.class || clazz == Long.class ||
149 clazz == float.class || clazz == Float.class || clazz == double.class || clazz == Double.class ||
150 clazz == char.class || clazz == Character.class || clazz == boolean.class || clazz == Boolean.class ||
151 clazz == String.class )
152 return true;
153
154 return false;
155 }
156 }
This page was automatically generated by Maven