Package org.strecks.converter.handler

Source Code of org.strecks.converter.handler.DefaultConversionHandler

package org.strecks.converter.handler;

import java.lang.reflect.Type;

import org.strecks.converter.Converter;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.exceptions.ConversionException;
import org.strecks.exceptions.ConversionRuntimeException;
import org.strecks.util.PropertyValueGetter;
import org.strecks.util.ReflectHelper;

/**
* Implementation of <code>ConversionHandler</code>. Provides rigourous trapping of
* <code>ClassCastExceptions</code>
* @author Phil Zoio
*/
public class DefaultConversionHandler implements ConversionHandler
{

  /**
   * Handles conversion from the source property where the possibility exists that the type of the
   * source property to be converted may be incompatible with the <code>Converter</code>
   * parameterization type
   */
  @SuppressWarnings("unchecked")
  public Object getAndConvertInwards(Object source, String sourcePropertyName, Converter converter)
      throws ConversionRuntimeException
  {
    Object propertyValue = PropertyValueGetter.getPropertyValue(source, sourcePropertyName);
    try
    {
      Object targetValue = converter.toTargetType(propertyValue);
      return targetValue;
    }
    catch (ClassCastException e)
    {
      Class<?> sourceType = PropertyValueGetter.getPropertyTypeSilently(source, sourcePropertyName);

      Type[] genericTypes = ReflectHelper.getGenericTypes(converter.getClass(), Converter.class);
      String description = ReflectHelper.getTypeArrayDescription(genericTypes);

      throw new ApplicationRuntimeException("Mismatch between parameterization type of converter: "
          + converter.getClass().getName() + "(" + description
          + ")), and source type of property being converted: " + source.getClass().getName() + ", property "
          + sourcePropertyName + " (" + sourceType + ")", e);
    }
    catch (ConversionException e)
    {
      throw new ConversionRuntimeException(e);
    }
  }

  /**
   * Handles conversion from the target property where the possibility exists that the type of the
   * source property to be converted may be incompatible with the <code>Converter</code>
   * parameterization type
   */
  @SuppressWarnings("unchecked")
  public Object getAndConvertOutwards(Object targetBean, String propertyName, Converter converter)
      throws ConversionRuntimeException
  {
    Object targetValue = PropertyValueGetter.getPropertyValue(targetBean, propertyName);

    try
    {
      Object propertyValue = converter.toSourceType(targetValue);
      return propertyValue;
    }
    catch (ClassCastException e)
    {
      Class<?> sourceType = PropertyValueGetter.getPropertyTypeSilently(targetBean, propertyName);

      Type[] genericTypes = ReflectHelper.getGenericTypes(converter.getClass(), Converter.class);
      String description = ReflectHelper.getTypeArrayDescription(genericTypes);

      throw new ApplicationRuntimeException("Mismatch between parameterization type of converter: "
          + converter.getClass().getName() + "(" + description
          + ")), and source type of property being converted: " + targetBean.getClass().getName()
          + ", property " + propertyName + " (" + sourceType + ")", e);
    }
    catch (ConversionException e)
    {
      throw new ConversionRuntimeException(e);
    }
  }
}
TOP

Related Classes of org.strecks.converter.handler.DefaultConversionHandler

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.