2012-11-20 76 views
2

InstantiationException我寫我自己的客戶轉換器:推土機,自定義轉換器

public class MyFancyCustomConverter extends DozerConverter<Integer, AnObject> 
{ 
    public MyFancyCustomConverter(Class<Integer> prototypeA, Class<AnObject> prototypeB) 
    { 
     super(prototypeA, prototypeB); 
    } 

    @Override 
    public AnObject convertTo(Integer source, AnObject destination) 
    { 
     // TODO: do something 
     return null; 
    } 

    @Override 
    public Integer convertFrom(AnObject source, Integer destination) 
    { 
     // TODO: do something 
     return 0; 
    } 
} 

而且我mapping.xml:

<mapping> 
    <class-a>java.lang.Integer</class-a> 
    <class-b>xyz.AnObject</class-b> 
    <field custom-converter="xyz.MyFancyCustomConverter" custom-converter-param="hello"> 
     <a>this</a> 
     <b key="my.key">this</b> 
    </field> 
</mapping> 

但我得到這個異常:

組織。 dozer.MappingException:java.lang.InstantiationException:xyz.MyFancyCustomConverter

任何想法我什麼做錯了嗎?我想這是因爲MyFancyCustomConverter沒有默認轉換器。但我不能添加一個,因爲DozerConverter沒有一個......

+0

請添加完整的堆棧跟蹤 – Rytek

回答

10
public MyFancyCustomConverter(Class<Integer> prototypeA, Class<AnObject> prototypeB) 
{ 
    super(prototypeA, prototypeB); 
} 

應該

public MyFancyCustomConverter() 
{ 
    super(Integer.class, AnObject.class); 
} 

超類需要知道兩班運行時類型,而且由於類型擦除,需要傳入類型令牌。