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

Rule "Unused private fields" should not log violation on fields having the @Getter (Lombok) and @Produces annotations

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.8
    • Component/s: Rules
    • Labels:
      None

      Description

      this weekend I tried http://projectlombok.org/ and it is amazing.

      Short: it is code generation in the IDE, just say @Getter on a field and
      there will be a getter (not visible in code, but in the outlines)

      (The editor is filled with annotation and the AST is filled with automatically created code constructs)

      But how to handle this?

      there are some problems with warnings of unused fields etc.

      here the example

      lomboked-code
      package de.lgohlke.qdox;
      
      import lombok.EqualsAndHashCode;
      import lombok.Getter;
      
      import com.thoughtworks.qdox.model.**JavaMethod;
      
      @EqualsAndHashCode
      public class JavaMethodHashed
      {
       @Getter
       private final JavaMethod method;
      
       private final String     signature;
       private final String     clazz;
      
       public JavaMethodHashed(final JavaMethod method)
       {
         this.method = method;
         this.signature = method.**getDeclarationSignature(true);
         this.clazz = method.getParentClass().**getFullyQualifiedName();
       }
      }
      

      it really would be expressed without lombok with this

      delomboked-code
      package de.lgohlke.qdox;
      
      import com.thoughtworks.qdox.model.**JavaMethod;
      
      public class JavaMethodHashed {
             private final JavaMethod method;
             private final String signature;
             private final String clazz;
      
             public JavaMethodHashed(final JavaMethod method) {
                     this.method = method;
                     this.signature = method.**getDeclarationSignature(true);
                     this.clazz = method.getParentClass().**
      getFullyQualifiedName();
             }
      
             @java.lang.Override
             @java.lang.SuppressWarnings("**all")
             public boolean equals(final java.lang.Object o) {
                     if (o == this) return true;
                     if (o == null) return false;
                     if (o.getClass() != this.getClass()) return false;
                     final JavaMethodHashed other = (JavaMethodHashed)o;
                     if (this.getMethod() == null ? other.getMethod() != null :
      !this.getMethod().equals(**other.getMethod())) return false;
                     if (this.signature == null ? other.signature != null :
      !this.signature.equals(other.**signature)) return false;
                     if (this.clazz == null ? other.clazz != null :
      !this.clazz.equals(other.**clazz)) return false;
                     return true;
             }
      
             @java.lang.Override
             @java.lang.SuppressWarnings("**all")
             public int hashCode() {
                     final int PRIME = 31;
                     int result = 1;
                     result = result * PRIME + (this.getMethod() == null ? 0 :
      this.getMethod().hashCode());
                     result = result * PRIME + (this.signature == null ? 0 :
      this.signature.hashCode());
                     result = result * PRIME + (this.clazz == null ? 0 :
      this.clazz.hashCode());
                     return result;
             }
      
             @java.lang.SuppressWarnings("**all")
             public JavaMethod getMethod() {
                     return this.method;
             }
      }
      

      there are some issues with this, see sub tickets

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              pynicolas OLD - Pierre-Yves Nicolas
              Reporter:
              lkwg82 Lars K. W. Gohlke (Inactive)
              Votes:
              25 Vote for this issue
              Watchers:
              28 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: