Issue with squid:s1118 - Utility classes should not have public constructors - in private classes

395 views
Skip to first unread message

s53...@gmail.com

unread,
Nov 11, 2017, 10:26:26 AM11/11/17
to SonarQube
Hello,

I'm using the Initialization-on-demand-holder-pattern for thread-safe singletons.

public class MySingleton
{
   private MySingleton()
   {
     // use getInstance()
   }

   private static class InitializationOnDemandHolderMySingleton {
      static final MySingleton INSTANCE = new MySingleton();
   }

   public static MySingleton getInstance()
   {
      return InitializationOnDemandHolderMySingleton.INSTANCE;
   }

SonarQube marks the private static class to add a private constructor to hide the implicit public one.

But a private constructor in a private class doesn't make any difference. The class MySingleton is the only one to which the private static class is visible - and MySingleton is the only class that would ever be able to initialize that static class (but never will) - no matter if the constructor is private or public.

So, why is this marked as an issue? I'd suggest that private classes won't be marked with issues of squid:s1118 as private constructors won't change anything here (except adding useless code) - or am I missing something?

Thanks, cheers,
Stefan.

Nicolas Peru

unread,
Nov 23, 2017, 5:58:12 AM11/23/17
to s53...@gmail.com, SonarQube
Hi Stefan, 

indeed this is a false positve as far as I can tell. Ticket created to handle the issue : https://jira.sonarsource.com/browse/SONARJAVA-2562

Thanks for the reproducer.

Cheers, 

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/cf9f3705-e9b1-44e6-9edf-1769b327c31c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas Peru | SonarSource
Reply all
Reply to author
Forward
0 new messages