dimanche 30 août 2015

Java Dynamic Casting

I am using Apache POI to yield Excel reports, and since I would like each column to be compatible with its datatype (date, number, integer ...), I made up a static method to return an Object that can be "casted".

Since I am prone to reducing code as much as possible, I chose to do the following:

Object castableValue = Engine.formattingValue(value); cell.setCellValue((castableValue.getClass().cast(castableValue)));

However, this is not accepted at compile time by POI library, while I am sure it would work at run time.

I have to use if else for four datatypes (Double, Integer, Calendar, and String).

Any suggestions to achieve this dynamic solution ?





Aucun commentaire:

Enregistrer un commentaire