Vaadin 7 ComboBox universal converter

Hello!

I have many combo box which have different classes for model and perpresentation object. To convert from model to representation we can use specific Converter. I want to write universal converter (the code below). The problem is that ComboBox::getType method return instead of perpesentation type the Object.class object. As the result implementation of
ObjectToObjectConverter::convertToPresentation lost imformation about presentation type.

Any hints?

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.vaadin.data.util.converter.Converter;

public class ObjectToObjectConverter implements Converter<Object, Object> {

  private static final long serialVersionUID = 1L;

  private static interface ConverterInterface extends Serializable {
    Object convert(Object from);
  }

  private static class ConverterKey implements Serializable {
    private static final long serialVersionUID = 1L;
    final Class<?> from;
    final Class<?> to;

    public ConverterKey(Class<?> from, Class<?> to) {
      super();
      this.from = from;
      this.to = to;
    }

    @Override
    public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((from == null) ? 0 : from.hashCode());
      result = prime * result + ((to == null) ? 0 : to.hashCode());
      return result;
    }

    @Override
    public boolean equals(Object obj) {
      if (this == obj)
        return true;
      if (obj == null)
        return false;
      if (getClass() != obj.getClass())
        return false;
      ConverterKey other = (ConverterKey) obj;
      if (from == null) {
        if (other.from != null)
          return false;
      } else if (!from.equals(other.from))
        return false;
      if (to == null) {
        if (other.to != null)
          return false;
      } else if (!to.equals(other.to))
        return false;
      return true;
    }
  }

  private static Map<ConverterKey, ConverterInterface> converterMap = new HashMap<>();
  static {
    converterMap.put(new ConverterKey(BigDecimal.class, Long.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return ((BigDecimal) from).longValue();
      }
    });
    converterMap.put(new ConverterKey(BigDecimal.class, Integer.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return ((BigDecimal) from).intValueExact();
      }
    });
    converterMap.put(new ConverterKey(BigDecimal.class, BigDecimal.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return from;
      }
    });
    converterMap.put(new ConverterKey(BigDecimal.class, Object.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return from;
      }
    });
    converterMap.put(new ConverterKey(Long.class, Long.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return from;
      }
    });
    converterMap.put(new ConverterKey(Long.class, Object.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return from;
      }
    });
    converterMap.put(new ConverterKey(Long.class, BigDecimal.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return BigDecimal.valueOf((Long) from);
      }
    });
    converterMap.put(new ConverterKey(Integer.class, Integer.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return from;
      }
    });
    converterMap.put(new ConverterKey(Integer.class, Object.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return from;
      }
    });
    converterMap.put(new ConverterKey(Integer.class, BigDecimal.class), new ConverterInterface() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object convert(Object from) {
        return BigDecimal.valueOf((Integer) from);
      }
    });
  }

  @Override
  public Object convertToModel(Object value, Class<? extends Object> targetType, Locale locale)
      throws com.vaadin.data.util.converter.Converter.ConversionException {
    if (value == null)
      return null;
    ConverterInterface converter = converterMap.get(new ConverterKey(value.getClass(), targetType));
    if (converter == null) {
      throw new IllegalArgumentException();
    }
    return converter.convert(value);
  }

  @Override
  public Object convertToPresentation(Object value, Class<? extends Object> targetType, Locale locale)
      throws com.vaadin.data.util.converter.Converter.ConversionException {
    if (value == null)
      return null;
    ConverterInterface converter = converterMap.get(new ConverterKey(value.getClass(), targetType));
    if (converter == null) {
      throw new IllegalArgumentException();
    }
    return converter.convert(value);
  }

  @Override
  public Class<Object> getModelType() {
    return Object.class;
  }

  @Override
  public Class<Object> getPresentationType() {
    return Object.class;
  }

}

A ComboBox is a (Abstract)Field where any type of value can stored. Since there is no type paramterisation in the Vaadin 7 data model, the type is Object.class. You need to inspect the value that is actually stored, i.e. the one returned by getValue()