2

I am working with sonar and lombok and it seems that those two aren't friends with each other.

@Getter
public class MyAwesomeClass {
    private String string1;
    private String string2;
    private String string3;
    ...
}

The example above generates a getter-method for every field in the class, thanks to the @Getter annotation, but sonar warns me that those fields are "Singular Fields", which are only used in one method, which isn't the case because of the generated getter-methods.

It is obvious that sonar doesn't take lombok into account. My question is if there is a possibility to make those warnings disappear, for example with a @SuppressWarnings annotation?

Magnilex
  • 11,584
  • 9
  • 62
  • 84
muffin
  • 1,456
  • 4
  • 21
  • 44

2 Answers2

3

SonarQuebe 5.0.X add the lombok patch, whick works for lombok's Getter.

For SonarQuebe 4.5.X, which use sonar-java-2.4, you can find the patch here: https://github.com/SonarSource/sonar-java/compare/2.4...liudongmiao:patch-lombok-2.4

You can download the binary from here too: https://github.com/liudongmiao/sonar-java/releases/tag/2.4-lombok

liudongmiao
  • 455
  • 4
  • 7
1

Even if I really wouldn't clutter code because of a code analyzis tool, I gues you could use the NOSONAR comment. Your code would look like this:

@Getter
public class MyAwesomeClass {
    private String string1; // NOSONAR
    private String string2; // NOSONAR
    private String string3; // NOSONAR
}

Not very elegant in my opinion, but it is actually a Sonar feature. The analyzis should then be ignored for those lines.

Another, possibly better solution, would be to disable the specific rule. I guess the top answer for this question can help you with that.

Both solutions come with some drawbacks. Using // NOSONAR would disable all rule checking for that specific line. On the other hand, by disabling the rule, you wouldn't only get rid of the false positives caused by Lombok, but also the real problems.

Community
  • 1
  • 1
Magnilex
  • 11,584
  • 9
  • 62
  • 84