Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3162

NPE in S3067 when using instance initializer

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.13.1
    • Fix Version/s: 6.0
    • Component/s: Rules
    • Labels:

      Description

      The following snippet will raise an NPE in SynchronizationOnGetClassCheck 

       

      public class GetClassInNonFinalClassInInitializerKillsSonarJava {
        private static boolean flag;
        {
          synchronized (getClass()) {
            if (!flag) {
              flag = true;
            }
          }
        }
      }

       

      stacktrace

      
      Caused by: java.lang.NullPointerExceptionCaused by: java.lang.NullPointerException at org.sonar.java.checks.synchronization.SynchronizationOnGetClassCheck.isEnclosingClassFinal(SynchronizationOnGetClassCheck.java:68) at org.sonar.java.checks.synchronization.SynchronizationOnGetClassCheck.visitNode(SynchronizationOnGetClassCheck.java:59) at org.sonar.java.model.VisitorsBridge$ScannerRunner.lambda$visit$7(VisitorsBridge.java:295) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:298) at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280) at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:302) at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280) at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:302) at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:280) 

      The reason is that we are trying to find the enclosing method, which doesn't exist in instance initializer.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                michael.gumowski Michael Gumowski
                Reporter:
                tibor.blenessy Tibor Blenessy
              • Votes:
                1 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: