0

I want clean Sonar issue after create new code in java 8.

public class Argument<T> {
    ...
    public T getValue() {
        return parameterType.transform(group.getValues());
    }
    ...
}

My code:

 List<Argument<?>> args = expression.match(text);
 return args == null ? null : args.stream().map(arg -> arg.getValue()).collect(Collectors.toList());

Sonar say:

Lambdas should be replaced with method references. Method/constructor references are more compact and readable than using lambdas, and are therefore preferred. Similarly, null checks can be replaced with references to the Objects::isNull and Objects::nonNull methods.

I want change map(arg -> arg.getValue()) by map(T::getValue()) but is it wrong compilation ().

Naman
  • 27,789
  • 26
  • 218
  • 353
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154

1 Answers1

4

Lambdas should be replaced with method references

change

.map(arg -> arg.getValue())

to

.map(Argument::getValue)

As for:

Similarly, null checks can be replaced with references to the Objects::isNull and Objects::nonNull methods

I've not used Sonar before but if it prefers the use of Objects.isNull and Objects.nonNull for null checks then you'd need to do:

return Objects.isNull(args) ? null : args.stream()
                .map(Argument::getValue)
                .collect(Collectors.toList());
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126