Josef Reichardt
Josef Reichardt

Reputation: 4296

SonarQube @NoArgsConstructor (Project Lombok)

I'm trying to use Project Lombok in my code and I'm using SonarQube and SonarLint (in IntelliJ IDEA).

When I create a new class with a private field like this:

public class Test {
    private String testfield;
}

then I get the correct warning from SonarQube/SonarLint "Remove this unused private field" (squid:S1068)... (So I know that the basic configuration is working.)

When I add the @Getter @Setter annotations to the field:

public class Test {
    @Getter @Setter
    private String testfield;
}

then the warning disapears (correctly!). (So I know that SonarQube basically understands Lombok annotations.)

And now when I create another Class like this

public class Test2 {
    public static void doSomething() {
        System.out.println("hey");
    }
}

I get the warning "Add a private constructor" (squid:S1118)... (this is again correct and expected)

But when I now add the @NoArgsConstructor annotation like this

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Test2 {
    public static void doSomething() {
        System.out.println("hey");
    }
}

the warning still remains.

Is this annotation not supported by SonarQube? I've found this JIRA issue (and many discussions on stackoverflow, github, ...), and I think it should work.

(I'm using IntellIJ 2016.1.3; SonarLint 2.2; lombok 1.16.8; SonarQube 5.5; SonarQube Java Plugin 3.13.1)

Any Help?

Upvotes: 4

Views: 5287

Answers (1)

zapl
zapl

Reputation: 63955

Edit: As of SonarQube Java 3.14, in "[SONARJAVA-1642] - Filter issues related to Lombok annotations with a dedicated issue filter" there is support for annotating your classes with @lombok.experimental.UtilityClass without tripping that warning. @NoArgsConstructor may need a pull request.

(source diff)


Lombok knows the @NoArgsConstructor but it only uses it to determine if a type is unused or not. It's not considered when checking for constructors of utility classes.

+  private static final Set<String> USED_TYPES_ANNOTATIONS = new HashSet<String>(Arrays.asList(
+    "lombok.Getter",
+    "lombok.Setter",
+    "lombok.Data",
+    "lombok.Value",
+    "lombok.Builder",
+    "lombok.ToString",
+    "lombok.EqualsAndHashCode",
+    "lombok.AllArgsConstructor",
+    "lombok.NoArgsConstructor",
+    "lombok.RequiredArgsConstructor"));

pull-request for the ticket.

Upvotes: 2

Related Questions