tags:

views:

270

answers:

2

I'm getting an InstantiationException when trying to map Date -> Calendar.

Simple test follows:

    @Test
    public void testConversion()
    {
     GregorianCalendar cal = new GregorianCalendar(2009, 2, 3);
     Date sourceValue = cal.getTime();
     DozerBeanMapper mapper = new DozerBeanMapper();
     Object result = mapper.map(sourceValue, Calendar.class);
    }

According to the docs, this is supported out of the box (even though Calendar is abstract). Anyone got experience with this and able to point out what I'm doing wrong?

+1  A: 

You are right. This throws InstantionException (I consider this a bug in dozer. Will you file it in their bug tracking system?).

However. It works when you convert the Date <--> Calendar values not on the root level. This test works for me (dozer 5.1):

 public static class Source {
  private Date value;
  public void setValue(Date value) {
   this.value = value;
  }
  public Date getValue() {
   return value;
  }
 }

 public static class Target {
  private Calendar value;
  public void setValue(Calendar value) {
   this.value = value;
  }
  public Calendar getValue() {
   return value;
  }
 }


    @Test
    public void testConversion()
    {
        final GregorianCalendar cal = new GregorianCalendar(2009, 2, 3);
        Source source = new Source(){{ setValue(cal.getTime());}};

        DozerBeanMapper mapper = new DozerBeanMapper();
        Target result = (Target) mapper.map(source, Target.class);
        assertEquals(cal.getTimeInMillis(), result.getValue().getTimeInMillis());
    }
Grzegorz Oledzki
Marty Pitt
A: 

If you change Calendar.class to GregorianCalendar.class the test works.

Superfilin
I guess OP is totally aware of it.
Grzegorz Oledzki
That's why I upvoted your answer ;)
Superfilin
Thanks - but the intent not to have to change destination class in order to do the conversion. Ideally, I'd like to just apply my conversion code abstractly, with not a care in the world about what the destination class is...
Marty Pitt