SonarQube Java
  1. SonarQube Java
  2. SONARJAVA-82

Constructor for a class named get* will cause java-squid to fail sonar analyze

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2
    • Component/s: Squid
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      When your don't follow naming convention for classes and create one with a name starting with get the Java AST scan will fail:

      public final class getError {
      
        private String test;
      
        public getError() {
          test = "darn";
        }
      }
      
              ... 23 more
      Caused by: java.lang.IllegalStateException
              at com.google.common.base.Preconditions.checkState(Preconditions.java:129)
              at org.sonar.java.ast.visitors.MethodHelper.getReturnType(MethodHelper.java:78)
              at org.sonar.java.ast.visitors.AccessorVisitor.isValidGetter(AccessorVisitor.java:68)
              at org.sonar.java.ast.visitors.AccessorVisitor.isAccessor(AccessorVisito
      

        Activity

        Hide
        Christer Grönbladh added a comment -

        this affects
        version=1.0
        groupId=org.codehaus.sonar-plugins.java
        artifactId=sonar-squid-java-plugin

        also 1.1-SNAPSHOT from today

        Show
        Christer Grönbladh added a comment - this affects version=1.0 groupId=org.codehaus.sonar-plugins.java artifactId=sonar-squid-java-plugin also 1.1-SNAPSHOT from today
        Hide
        Christer Grönbladh added a comment - - edited

        I updated getReturnType in org.sonar.java.ast.visitors.MethodHelper to handle this by adding a check if it's a constructor

          public AstNode getReturnType() {
            final AstNode typeNode = getName().previousAstNode();
            if(!isConstructor()){
              Preconditions.checkState(typeNode.is(JavaKeyword.VOID, grammar.type));
            }
            return typeNode;
          }
        
        Show
        Christer Grönbladh added a comment - - edited I updated getReturnType in org.sonar.java.ast.visitors.MethodHelper to handle this by adding a check if it's a constructor public AstNode getReturnType() { final AstNode typeNode = getName().previousAstNode(); if(!isConstructor()){ Preconditions.checkState(typeNode.is(JavaKeyword.VOID, grammar.type)); } return typeNode; }
        Hide
        Evgeny Mandrikov added a comment -

        @Christer even if your solution works, I would prefer to completely exclude constructors from AccessorVisitor since they always not accessors - https://github.com/SonarSource/sonar-java/commit/dee1b84a4bd73890094c2f2dab8142a03caf1e42 But thank you for your investigations and proposed solution!

        Show
        Evgeny Mandrikov added a comment - @Christer even if your solution works, I would prefer to completely exclude constructors from AccessorVisitor since they always not accessors - https://github.com/SonarSource/sonar-java/commit/dee1b84a4bd73890094c2f2dab8142a03caf1e42 But thank you for your investigations and proposed solution!
        Hide
        Freddy Mallet added a comment -

        Manually tested!

        Show
        Freddy Mallet added a comment - Manually tested!

          People

          • Assignee:
            Evgeny Mandrikov
            Reporter:
            Christer Grönbladh
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: