
org.sonar.l10n.java.rules.squid.S1452.html Maven / Gradle / Ivy
Using a wildcard as a return type implicitly means that the return parameter should be considered as read-only but without any way to enforce this contract.
Let's take the example of method returning a "List<? extends Animal>". Is it possible on this list to add a Dog, a Cat, ... we simply don't know. The consumer of a method should not have to deal with such disruptive questions.
Noncompliant Code Example
List<? extends Animal> getAnimals(){...}
Compliant solution
//First Solution: the list can contain any kind of animals
List<Animal> getAnimals(){...}
//Second Solution: the list can contain only one type of animal
List<E> getAnimals(){...} //Where E extends Animal in the signature of the nesting class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy