|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BeanUtils.java | 16,7% | 30,6% | 75% | 27% |
|
||||||||||||||
| 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 | 1 |
public static void writeProperty(Object bean, String propName, String propValue){ |
| 42 |
|
|
| 43 | 1 |
Class clazz = bean.getClass(); |
| 44 | 1 |
String key = clazz.getName() + "." + propName;
|
| 45 |
|
|
| 46 | 1 |
Method writeMethod = (Method)writeMethods.get( key ); |
| 47 |
|
|
| 48 | 1 |
if( writeMethod == null ){ |
| 49 |
|
|
| 50 | 1 |
writeMethod = getWriteMethod( clazz, propName ); |
| 51 |
|
|
| 52 | 0 |
if( writeMethod == null ){ |
| 53 | 0 |
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 | 0 |
return;
|
| 56 |
} |
|
| 57 |
|
|
| 58 | 0 |
writeMethods.put( key, writeMethod ); |
| 59 |
} |
|
| 60 |
|
|
| 61 | 0 |
try{
|
| 62 | 0 |
writeMethod.invoke(bean, new Object[]{
|
| 63 |
convertStringToType(propValue, writeMethod.getParameterTypes()[0] ) } ); |
|
| 64 |
} |
|
| 65 |
catch(IllegalAccessException e){
|
|
| 66 | 0 |
throw new UncheckedBeansException(e); |
| 67 |
} |
|
| 68 |
catch(InvocationTargetException e){
|
|
| 69 | 0 |
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 | 0 |
public static Object convertStringToType( String value, Class type ){ |
| 80 |
|
|
| 81 | 0 |
if( !isPrimitiveType(type) )
|
| 82 | 0 |
throw new IllegalArgumentException("Type [" + type.getName() + "] is not a Java Primitive type or String."); |
| 83 |
|
|
| 84 | 0 |
if( type == String.class ) |
| 85 | 0 |
return value;
|
| 86 |
|
|
| 87 | 0 |
if( type == byte.class || type == Byte.class ) |
| 88 | 0 |
return Byte.valueOf(value);
|
| 89 | 0 |
if( type == short.class || type == Short.class ) |
| 90 | 0 |
return Short.valueOf(value);
|
| 91 | 0 |
if( type == int.class || type == Integer.class ) |
| 92 | 0 |
return Integer.valueOf(value);
|
| 93 | 0 |
if( type == long.class || type == Long.class ) |
| 94 | 0 |
return Long.valueOf(value);
|
| 95 |
|
|
| 96 | 0 |
if( type == float.class || type == Float.class ) |
| 97 | 0 |
return Float.valueOf(value);
|
| 98 | 0 |
if( type == double.class || type == Double.class ) |
| 99 | 0 |
return Double.valueOf(value);
|
| 100 |
|
|
| 101 | 0 |
if( type == boolean.class || type == Boolean.class ) |
| 102 | 0 |
return Boolean.valueOf(value);
|
| 103 |
|
|
| 104 | 0 |
if( type == char.class || type == Character.class ){ |
| 105 | 0 |
if( value != null && value.length() > 0 ) |
| 106 | 0 |
return new Character( value.toCharArray()[0] ); |
| 107 |
} |
|
| 108 |
|
|
| 109 | 0 |
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 | 1 |
public static Method getWriteMethod( Class clazz, String propName ){ |
| 121 |
|
|
| 122 | 1 |
try{
|
| 123 | 1 |
BeanInfo beanInfo = Introspector.getBeanInfo( clazz ); |
| 124 |
|
|
| 125 | 1 |
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); |
| 126 |
|
|
| 127 | 2 |
for (int i = 0; i < propertyDescriptors.length; i++) { |
| 128 | 2 |
if( propertyDescriptors[i].getName().equals(propName) ){
|
| 129 | 1 |
Method writeMethod = propertyDescriptors[i].getWriteMethod(); |
| 130 | 1 |
if( writeMethod != null ){ |
| 131 | 0 |
if( isPrimitiveType( propertyDescriptors[i].getPropertyType() ) )
|
| 132 | 0 |
return writeMethod;
|
| 133 |
} |
|
| 134 |
} |
|
| 135 |
} |
|
| 136 |
|
|
| 137 |
// no write method found for property or not a primitive type.
|
|
| 138 | 0 |
return null; |
| 139 |
} |
|
| 140 |
catch(IntrospectionException e){
|
|
| 141 | 0 |
throw new UncheckedBeansException(e); |
| 142 |
} |
|
| 143 |
} |
|
| 144 |
|
|
| 145 | 1 |
public static boolean isPrimitiveType( Class clazz ){ |
| 146 |
|
|
| 147 | 1 |
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 | 1 |
return true; |
| 153 |
|
|
| 154 | 0 |
return false; |
| 155 |
} |
|
| 156 |
} |
|
| 157 |
|
|
||||||||||