I'm not sure if I am using generics correctly, but basically I created an Arraylist<? extends ModuleInfo> moduleList
and ModuleInfo m
objects, and tried to call moduleList.add(m)
. However it won't compile and I am getting an error message that seems a bit cryptic to me. The error message and code are below. Anyone else know what is wrong?
void load() {
ArrayList<? extends ModuleInfo> moduleList = new ArrayList();
Iterator<? extends ModuleInfo> iter_m;
ModuleInfo m;
//get modules that depend on this module
//retrieve list of all modules and iterate trough each one
iter_m = Lookup.getDefault().lookupAll(ModuleInfo.class).iterator();
while(iter_m.hasNext()) {
m = iter_m.next();
//loop through modules dependencies and check for a dependency on this module
for(Dependency d : m.getDependencies()) {
//if found, the module to the list
if(d.getName().equals(GmailAuthManager.class.getPackage().getName())) {
moduleList.add(m);
break;
}
}
}
}
Error is a follows:
error: no suitable method found for add(ModuleInfo)
moduleList.add(m);
method ArrayList.add(int,CAP#1) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(CAP#1) is not applicable
(actual argument ModuleInfo cannot be converted to CAP#1 by method invocation conversion)
where CAP#1 is a fresh type-variable:
CAP#1 extends ModuleInfo from capture of ? extends ModuleInfo
See Question&Answers more detail:os