AutowiredConstructor

SUGGESTION

Simplification

View source code on GitHub

Summary

Omit @Autowired on a class’ sole constructor, as it is redundant

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("AutowiredConstructor") to the enclosing element.

Disable this pattern completely by adding -Xep:AutowiredConstructor:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import org.springframework.beans.factory.annotation.Autowired;
 
 interface Container {
   class A {
-    @Autowired
     @Deprecated
     A() {}
   }
 
   class B {
-    @Autowired
     B(String x) {}
   }
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import com.google.errorprone.annotations.Immutable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

interface Container {
  @Immutable
  class A {
    A() {}
  }

  class B {
    @Autowired
    void setProperty(Object o) {}
  }

  class C {
    // BUG: Diagnostic contains:
    @Autowired
    C() {}
  }

  class D {
    // BUG: Diagnostic contains:
    @Autowired
    D(String x) {}
  }

  class E {
    @Autowired
    E() {}

    E(String x) {}
  }

  class F {
    F() {}

    @Autowired
    F(String x) {}
  }

  class G {
    @Autowired private Object o;
  }

  class H {
    @SafeVarargs
    H(List<String>... lists) {}
  }
}

Copyright © 2017-2024 Picnic Technologies BV