I have a method in a class which takes 'List' type parameter. When I run the spotbugs check it is returning below error (overly concrete parameter). Though the given method uses list it is complaining about concrete parameter. Could some one please let me know what I am missing here.
.TestClass.transform(List): 1st parameter 'dtos' could be declared as java.lang.Iterable instead
[com.TestClass] At TestClass.java:[line 146] OCP_OVERLY_CONCRETE_PARAMETER
public void transform(final List<Dto1> dtos) { //line 145
for (final Dto1 dto : dtos) { //line 146
Dto2 dto2 = convert(dto);
..
}
}
Though the given method uses list it is complaining about concrete parameter.
No, it is complaining about an overly concrete parameter. List
is an interface, but the way method uses it does not depend on it being a List
, specifically. That is what the tool means.
Could some one please let me know what I am missing here.
The diagnostic message already tells you:
.TestClass.transform(List): 1st parameter 'dtos' could be declared as java.lang.Iterable instead
That is, declaring the parameter as Iterable<Dto1>
instead of List<Dto1>
would accept a wider range of arguments without changing the behavior for any of those that match the narrower type.
In that case, another generalization should be possible, too: instead of an exact type parameter for that Iterable
, you could use a bounded wildcard: Iterable<? extends Dto1>
. That is the broadest you can make the parameter type that still supports all the current behavior of the method. In other words:
public void transform(final Iterable<? extends Dto1> dtos) {
for (final Dto1 dto : dtos) {
Dto2 dto2 = convert(dto);
// ...
}
}