vendredi 28 août 2015

Convert via reflection retrieved object to string (how to iterate over multiple class types?)

I have a getterMethod that returns an type which I retrieve using:

getterMethod.getReturnType()

I need to cast this return value to string. Depending on the type of the returned value, I either need to just use .toString() method on the object, sometimes I need to do more work like using a stringformat for dates etc..

I couldn't help myself but going this way:

Integer i = 1;
if(i.getClass().isInstance(getterMethod.getReturnType())){
    // integer to string
}

But I have lots of possible types, what would be a good and fast approach to resolve this?





Aucun commentaire:

Enregistrer un commentaire