Java Generics in arguments -
i have following interface:
public interface iradiobuttongroup<t> { list<iradiobutton<t>> getbuttons(); }
now create method
protected object getdefaultradiobuttonvalue(iradiobuttongroup<?> field) { list<iradiobutton<?>> buttons = field.getbuttons();
now java complaining:
type mismatch: cannot convert list<iradiobutton<capture#1-of ?>> list<iradiobutton<?>>
any suggestions?
the unbounded wildcard parameter type of method means that, accept iradiobuttongroup
of unknown type. compiler doesn't know type come, @ compilation time, compiler generate , assign each wildcards placeholder, because although doesn't know type coming, sure there has single type replace ?
. , placeholder capture#1-of ?
see in error message.
basically, trying assign list<iradiobutton<cap#1-of-?>>
list<iradiobutton<?>>
, , not valid, in similar way how list<list<string>>
cannot assigned list<list<?>>
.
one known way solve these issues use capture helpers. create generic method, , delegate call method. generic method infer type parameter , able type safe operation. see this brian goetz's article more details.
so solve issue, provide method in below code:
protected object getdefaultradiobuttonvalue(iradiobuttongroup<?> field) { return getdefaultradiobuttonvaluehelper(field); } private <t> object getdefaultradiobuttonvaluehelper(iradiobuttongroup<t> field) { list<iradiobutton<t>> buttons = field.getbuttons(); // write logic of original method here }
Comments
Post a Comment