RK3
RK3

Reputation: 1269

SonarQube UnusedPrivate constructor

SonarQube is throwing an error for my below code.
I have an enum that accepts String. I want users to use the valueOf method of my enum. Example: MyEnum.valueOf("good"). Hence I had to create a constructor which takes String as a parameter. But SonarQube is not letting me get away with it.
It is currently throwing me an error asking me to remove the unused private constructor. Removing the constructor is not possible as it will raise a compilation error for not having a constructor with String as parameter.

How can I make SonarQube ignore this or is there any alternative solution for my coding?

Below is my code.

public enum MyEnum {
    GOOD("good"), 
    BAD("bad"), 
    BETTER("better");

    private MyEnum(String value){//asks me to remove this. But I can't do that

    }
}

Upvotes: 4

Views: 3149

Answers (1)

neXus
neXus

Reputation: 2223

The squid:UnusedPrivateMethod raises this issue, but its documentation mentions:

This rule doesn't raise any issue on annotated methods.

So a simple workaround is to annotate the constructor with @SuppressWarnings("unused")

public enum MyEnum {
    GOOD("good"), 
    BAD("bad"), 
    BETTER("better");

    @SuppressWarnings("unused")
    private MyEnum(String value){
        // ...
    }
}

Upvotes: 5

Related Questions