jeudi 25 juin 2020

Casting argument before using reflection to invoke a method

I try to using reflection to set a value from A model to B model

   // Get value in Board model using dynamic getter
   Object lvBoardValue;
   // Value after invoke getter is (Double)
   lvBoardValue = lvBoardPropDesc.getReadMethod().invoke(pBoardModel);
   // Return type of getter in Instrument  => (int)
   Class lvInstReturnType = lvInstrumentPropDesc.getReadMethod().getReturnType();
   // Return type of getter in Board => (double)
   Class lvBoardReturnType = lvBoardPropDesc.getReadMethod().getReturnType();
                
   // set that value for instrument (This setter need int value)
   lvInstrumentPropDesc.getWriteMethod().invoke(pBaseModel, (int) lvBoardValue);

The problem is I want to set a Double value (lvBoardValue) by invoking another setter which is need int value. I googled a lot the way to cast from Double to int, but have no clue.

I have tried:

lvInstrumentPropDesc.getWriteMethod().invoke(pBaseModel, ((Double) lvBoardValue).intValue());

It should work by casting like that, but it cannot meet the requirement of setting dynamically if I could have a return type

Have anyone already face this situation?. Please give me a suggestion. Thank you in advance.





Aucun commentaire:

Enregistrer un commentaire