In our current code, we have a class called MasterCodeView
:
public class MasterCodeView implements Serializable {
private String code;
private String description;
//getters and setters
}
Then, in each of our business objects, we have one or more String[][]
attributes:
public static final String[][] CONNECTIONMODEDESCRIPTIONS = {
{ CONNECTIONMODE_PASSIVE + "", MasterCodeDescriptionKeys.MC_FTPCONNECTIONMODE_PASSIVE },
{ CONNECTIONMODE_ACTIVE + "", MasterCodeDescriptionKeys.MC_FTPCONNECTIONMODE_ACTIVE }
};
These MasterCodeViews are used for i18n of certain object-specific data, for use in radio button labels, grid table cells,... We have around 60 of these.
The translation from the String[][]
to List<MasterCodeView>
is done using a MasterCodeServiceImpl
singleton:
private List<MasterCodeView> connectionModeDescriptions = new ArrayList<MasterCodeView>();
// We have a bunch of these that are just plain List, but those are being
// replaced with Generic lists like above whenever possible to remove warnings.
private MasterCodeServiceImpl() {
Object[][] tmp = {
...
{ connectionModeDescriptions, FTP.CONNECTIONMODEDESCRIPTIONS },
... // over 60 lines in total
} ;
for (int i = 0; i < tmp.length; i++) {
List<MasterCodeView> list = (List<MasterCodeView>)tmp[i][0];
String[][] descriptions = (String[][])tmp[i][1];
for (int j = 0; j < descriptions.length; j++) {
MasterCodeView masterCodeView = new MasterCodeView();
masterCodeView.setCode(descriptions[j][0]);
masterCodeView.setDescription(descriptions[j][1]);
list.add(masterCodeView);
}
}
}
As you can see, this takes a 2D array of Objects, which means that there's a message about Unchecked Conversion from Object to List<MasterCodeView>
on the first line within the For loop. I would like to get rid of this error message. However, I want to do this without having to edit the 60 line mapping array to a new formatting, and without having to change anything about the business objects or the MasterCodeView
class.
I preferably only want to change the Object[][]
to something else and if needed the for loop. Is this possible?
,String[][]>`, but that would require rewriting the entire initialization array, I think
– Nzall Jun 29 '16 at 10:01