Enum - private constructor - Remove this unused private "ClassName" constructor

java

(Roman) #1

Hi!,

I now had time to create a small example for the problem described in https://groups.google.com/d/msg/sonarqube/373G8hehJL4/cw6mtslIAAAJ

  • versions used (SonarQube, Scanner, Plugin, and any relevant extension)
    sonarqube: 7.1.0.11001
    SonarJava 5.3

  • error observed
    Remove this unused private “ClassName” constructor.
    But enums should probably be an exception to squid:UnusedPrivateMethod.

  • steps to reproduce

    package com.example;
    
    public enum Parameters
    {
      
      A("a", helper.a, Boolean.FALSE)
      , B("b", helper.b, Boolean.FALSE)
      , C("c", helper.b, Boolean.FALSE)
      ;
    
      private final String param;
      private final String param2;
      private final Boolean param3;
    
      private Parameters(final String paramName, final String param2, final Boolean param3)
      {
          this.param = paramName;
          this.param2 = param2;
          this.param3 = param3;
      }
      
    }
    

  • potential workaround

(Michael Gumowski) #4

Hello Roman,

Thanks for the followup from the old mailing list!

In your example, can you specify what is the helper object?

Without its declaration, it seems to me that as is this code won’t compile. If it does not compile, then the analyzer is not able to rely on bytecode to resolve types and references for objects outside the current file… leading to an unresolved constructor.

Cheers,
Michael


(Roman) #6

hi Michael,

thanks for the analysis. I think I found the issue. I did not provide a valid path to the sonar.java.binaries / did not compile the classes properly. So your hint concerning the bytecode seems correct. For completeness an updated example:

Parameters:

package com.example;

import com.other.Helper;

public enum Parameters
{
    
    A("a", Helper.a, Boolean.FALSE)
    , B("b", Helper.b, Boolean.FALSE)
    , C("c", Helper.b, Boolean.FALSE)
    ;

    private final String param;
    private final String param2;
    private final Boolean param3;

    private Parameters(final String paramName, final String param2, final Boolean param3)
    {
        this.param = paramName;
        this.param2 = param2;
        this.param3 = param3;
    }
    
}

Helper:

package com.other;

public class Helper {
	public static final String a="a";
	public static final String b="b";
	
	
}

(Michael Gumowski) #7

Hey Roman,

Just to be sure and to close the topic, did providing correctly the bytecode for your analysis removed the FP?
Can we mark the topic as resolved?

Cheers,
Michael


(Roman) #8

Hi michael,
Yes, providing the bytecode solved the problem in the provided example. I didn’t have time to test with the real code, but i suppose it’s the same and we can close this topic.

Thanks for your support
Roman