Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine Correct Method Signature During Runtime
    text
    copied!<p>I am using the following the following class <code>org.apache.poi.hssf.usermodel.HSSFCell</code>, with a list of the following methods:</p> <pre><code>void setCellValue(boolean value) void setCellValue(java.util.Calendar value) void setCellValue(java.util.Date value) void setCellValue(double value) void setCellValue(HSSFRichTextString value) void setCellValue(java.util.Calendar value) void setCellValue(HSSFRichTextString value) </code></pre> <p>Take note that, there are no methods with Object as method parameter.</p> <p>Now, I have no way to determine my value class type during compiled time. I can only determine my value class type during runtime. Hence, how I can determine the correct method to be called, if I do not know the method signature during compiled time?</p> <p>My code is below:</p> <pre><code>final int rowCount = tableModel.getRowCount(); for (int i = 0; i &lt; rowCount; i++) { final HSSFRow row = sheet.createRow(i + 1); for (int j = 0; j &lt; columnCount; j++) { final Object object = tableModel.getValueAt(i, j); final Class myClass = tableModel.getColumnClass(j); // How to perform casting during compiled time, and invoke // the setCellValue with correct signature? if (object != null) { row.createCell(j).setCellValue(??); // Does not accept Object! } } } </code></pre> <p>Perhaps ugly if...else with <code>instanceof</code> would resolve my problem. However, if I do not want the ugly if...else with <code>instanceof</code>, is there any better way to do so?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload