Package org.strecks.util

Source Code of org.strecks.util.PropertyValueSetter

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.strecks.exceptions.ApplicationRuntimeException;

/**
* Utility class with methods mostly related to setting object and property values using reflection
* @author Phil Zoio
*/
public class PropertyValueSetter
{

  private static Map<Class, Object> primitiveDefaultsMap = initDefaultsMap();

  private static Map<Class, Object> initDefaultsMap()
  {
    Map<Class, Object> map = new HashMap<Class, Object>();
    map.put(Boolean.TYPE, Boolean.FALSE);
    map.put(Byte.TYPE, Byte.valueOf((byte) 0));
    map.put(Short.TYPE, Short.valueOf((short) 0));
    map.put(Integer.TYPE, Integer.valueOf(0));
    map.put(Long.TYPE, Long.valueOf(0L));
    map.put(Float.TYPE, Float.valueOf(0.0F));
    map.put(Double.TYPE, Double.valueOf(0.0));
    return map;
  }

  public static void setPropertyValue(Object targetBean, PropertyDescriptor descriptor, Object targetValue)
  {

    Assert.notNull(targetBean);
    Assert.notNull(descriptor);

    Class type = descriptor.getPropertyType();
    Method setterMethod = descriptor.getWriteMethod();

    setPropertyValue(targetBean, setterMethod, type, targetValue);

  }

  public static void setPropertyValue(Object targetBean, Method setterMethod, Class type, Object targetValue)
  {

    Assert.notNull(targetBean);
    Assert.notNull(setterMethod);
    Assert.notNull(type);

    try
    {

      if (targetValue == null && typeIsPrimitive(type))
      {
        Object defaultValue = primitiveDefaultsMap.get(type);

        setterMethod.invoke(targetBean, new Object[]
        {
          defaultValue
        });
      }
      else
      {
        setterMethod.invoke(targetBean, new Object[]
        {
          targetValue
        });
      }
    }
    catch (IllegalArgumentException e)
    {
      throw new ApplicationRuntimeException("Application error: could not set '" + targetValue + "' (type "
          + (targetValue != null ? targetValue.getClass().getName() : "null") + ") using method "
          + setterMethod.getName() + " of class " + targetBean.getClass().getName()
          + ": illegal argument supplied", e);
    }
    catch (Exception e)
    {
      throw new ApplicationRuntimeException("Application error: could not set " + targetValue + " using method "
          + setterMethod.getName() + " of class " + targetBean.getClass().getName(), e);
    }

  }

  public static void setPropertyValue(Object targetBean, String beanPropertyName, Class type, Object targetValue)
  {

    Assert.notNull(targetBean);
    Assert.notNull(beanPropertyName);
    Assert.notNull(type);

    try
    {

      if (targetValue == null && typeIsPrimitive(type))
      {
        targetValue = primitiveDefaultsMap.get(type);
      }

      PropertyUtils.setProperty(targetBean, beanPropertyName, targetValue);

    }
    catch (IllegalArgumentException e)
    {
      throw new ApplicationRuntimeException(
          "Application error: could not set '" + targetValue + "' (type "
              + (targetValue != null ? targetValue.getClass().getName() : "null") + ") for property "
              + beanPropertyName + " of class " + targetBean.getClass().getName()
              + ": illegal argument supplied", e);
    }
    catch (Exception e)
    {
      throw new ApplicationRuntimeException("Application error: could not set " + targetValue + " for property "
          + beanPropertyName + " of class " + targetBean.getClass().getName(), e);
    }

  }

  private static boolean typeIsPrimitive(Class type)
  {
    return type.isPrimitive();
  }

}
TOP

Related Classes of org.strecks.util.PropertyValueSetter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.