0

Let say I have a java class called Input with a Generic for its type T

class Input<T>

And I have a class Work that have a list of inputs

And this class wants o return its list of input, each of them may be of different type

If I declare it as:

Collection<Input<?>> inputs ();

With this declaration Sonar will complain of the method returning wildcard type.

If I remove the wildcard it will complain of using the interface without specifying the generics.

As I do have almost all rules activated, if using the @SupressWarnings it will also complain about it.

I'd rather prefer a more properly design solution than using a // NOSONAR or any other method to avoid the warning.

I have seen a lot of articles similar to this one but none so specific or with a viable solution.

Any ideas?

  • 2
    What, exactly, is the message Sonar gives you about this declaration? – Louis Wasserman Oct 26 '20 at 00:49
  • 1
    Is it this rule: [Generic wildcard types should not be used in return types](https://rules.sonarsource.com/java/RSPEC-1452)? – Hulk Oct 26 '20 at 01:08
  • Possible solutions will depend on what you want to do with the result - what is the purpose of the wildcard? Perhaps you can come up with a better interface that is not parameterized with a type you don't know at that point? – Hulk Oct 26 '20 at 01:11
  • See also https://stackoverflow.com/questions/22815023/generic-wildcard-types-should-not-be-used-in-return-parameters?noredirect=1&lq=1 – Hulk Oct 26 '20 at 01:16
  • Answering to the comments: - The message is "Generic wildcard types should not be used in return types" – Iván Gómez Martín Oct 27 '20 at 04:30

0 Answers0