001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.util;
016:
017: import java.beans.PropertyDescriptor;
018: import java.lang.reflect.Method;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.commons.beanutils.PropertyUtils;
023: import org.strecks.exceptions.ApplicationRuntimeException;
024:
025: /**
026: * Utility class with methods mostly related to setting object and property values using reflection
027: * @author Phil Zoio
028: */
029: public class PropertyValueSetter {
030:
031: private static Map<Class, Object> primitiveDefaultsMap = initDefaultsMap();
032:
033: private static Map<Class, Object> initDefaultsMap() {
034: Map<Class, Object> map = new HashMap<Class, Object>();
035: map.put(Boolean.TYPE, Boolean.FALSE);
036: map.put(Byte.TYPE, Byte.valueOf((byte) 0));
037: map.put(Short.TYPE, Short.valueOf((short) 0));
038: map.put(Integer.TYPE, Integer.valueOf(0));
039: map.put(Long.TYPE, Long.valueOf(0L));
040: map.put(Float.TYPE, Float.valueOf(0.0F));
041: map.put(Double.TYPE, Double.valueOf(0.0));
042: return map;
043: }
044:
045: public static void setPropertyValue(Object targetBean,
046: PropertyDescriptor descriptor, Object targetValue) {
047:
048: Assert.notNull(targetBean);
049: Assert.notNull(descriptor);
050:
051: Class type = descriptor.getPropertyType();
052: Method setterMethod = descriptor.getWriteMethod();
053:
054: setPropertyValue(targetBean, setterMethod, type, targetValue);
055:
056: }
057:
058: public static void setPropertyValue(Object targetBean,
059: Method setterMethod, Class type, Object targetValue) {
060:
061: Assert.notNull(targetBean);
062: Assert.notNull(setterMethod);
063: Assert.notNull(type);
064:
065: try {
066:
067: if (targetValue == null && typeIsPrimitive(type)) {
068: Object defaultValue = primitiveDefaultsMap.get(type);
069:
070: setterMethod.invoke(targetBean,
071: new Object[] { defaultValue });
072: } else {
073: setterMethod.invoke(targetBean,
074: new Object[] { targetValue });
075: }
076: } catch (IllegalArgumentException e) {
077: throw new ApplicationRuntimeException(
078: "Application error: could not set '"
079: + targetValue
080: + "' (type "
081: + (targetValue != null ? targetValue
082: .getClass().getName() : "null")
083: + ") using method "
084: + setterMethod.getName() + " of class "
085: + targetBean.getClass().getName()
086: + ": illegal argument supplied", e);
087: } catch (Exception e) {
088: throw new ApplicationRuntimeException(
089: "Application error: could not set " + targetValue
090: + " using method " + setterMethod.getName()
091: + " of class "
092: + targetBean.getClass().getName(), e);
093: }
094:
095: }
096:
097: public static void setPropertyValue(Object targetBean,
098: String beanPropertyName, Class type, Object targetValue) {
099:
100: Assert.notNull(targetBean);
101: Assert.notNull(beanPropertyName);
102: Assert.notNull(type);
103:
104: try {
105:
106: if (targetValue == null && typeIsPrimitive(type)) {
107: targetValue = primitiveDefaultsMap.get(type);
108: }
109:
110: PropertyUtils.setProperty(targetBean, beanPropertyName,
111: targetValue);
112:
113: } catch (IllegalArgumentException e) {
114: throw new ApplicationRuntimeException(
115: "Application error: could not set '"
116: + targetValue
117: + "' (type "
118: + (targetValue != null ? targetValue
119: .getClass().getName() : "null")
120: + ") for property " + beanPropertyName
121: + " of class "
122: + targetBean.getClass().getName()
123: + ": illegal argument supplied", e);
124: } catch (Exception e) {
125: throw new ApplicationRuntimeException(
126: "Application error: could not set " + targetValue
127: + " for property " + beanPropertyName
128: + " of class "
129: + targetBean.getClass().getName(), e);
130: }
131:
132: }
133:
134: private static boolean typeIsPrimitive(Class type) {
135: return type.isPrimitive();
136: }
137:
138: }
|