NonStaticImport

SUGGESTION

Style

View source code on GitHub

Summary

Member should not be statically imported

Suppression

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

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

Samples

Replacement

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

-import static com.google.common.collect.ImmutableList.copyOf;
-import static com.google.common.collect.ImmutableSet.of;
-import static java.time.Clock.systemUTC;
-import static java.time.Instant.MAX;
-import static java.time.Instant.MIN;
-import static java.util.Collections.min;
-import static java.util.Locale.ROOT;
-import static java.util.Optional.empty;
-
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.time.Clock;
 import java.time.Instant;
+import java.util.Collections;
 import java.util.Locale;
 import java.util.Optional;
 
 class A {
   void m() {
-    systemUTC();
     Clock.systemUTC();
+    Clock.systemUTC();
 
-    Optional<Integer> o1 = empty();
+    Optional<Integer> o1 = Optional.empty();
     Optional<Integer> o2 = Optional.empty();
 
-    Object l1 = copyOf(ImmutableList.of());
+    Object l1 = ImmutableList.copyOf(ImmutableList.of());
     Object l2 = ImmutableList.copyOf(ImmutableList.of());
 
-    Locale lo1 = ROOT;
+    Locale lo1 = Locale.ROOT;
     Locale lo2 = Locale.ROOT;
 
-    Instant i1 = MIN;
-    Instant i2 = MAX;
+    Instant i1 = Instant.MIN;
+    Instant i2 = Instant.MAX;
 
-    ImmutableSet.of(min(of()));
+    ImmutableSet.of(Collections.min(ImmutableSet.of()));
   }
 
   private static final class WithCustomConstant {
     private static final Instant MIN = Instant.EPOCH;
     private static final Instant OTHER = MIN;
-    private static final Instant OTHER_MAX = MAX;
+    private static final Instant OTHER_MAX = Instant.MAX;
   }
 }
 

Identification

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

package pkg;

// BUG: Diagnostic contains:
import static com.google.common.base.Strings.nullToEmpty;
// BUG: Diagnostic contains:
import static com.google.common.collect.ImmutableList.copyOf;
// BUG: Diagnostic contains:
import static java.lang.Integer.MAX_VALUE;
// BUG: Diagnostic contains:
import static java.lang.Integer.MIN_VALUE;
// BUG: Diagnostic contains:
import static java.time.Clock.systemUTC;
// BUG: Diagnostic contains:
import static java.time.Instant.MIN;
// BUG: Diagnostic contains:
import static java.time.ZoneOffset.SHORT_IDS;
import static java.time.ZoneOffset.UTC;
// BUG: Diagnostic contains:
import static java.util.Collections.min;
import static java.util.Locale.ENGLISH;
// BUG: Diagnostic contains:
import static java.util.Locale.ROOT;
// BUG: Diagnostic contains:
import static java.util.Optional.empty;
import static pkg.A.WithMethodThatIsSelectivelyFlagged.list;
// BUG: Diagnostic contains:
import static reactor.core.publisher.Flux.just;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Locale;
import java.util.Map;
import pkg.A.Wrapper.INSTANCE;

class A {
  private Integer MIN_VALUE = 12;

  void m() {
    nullToEmpty(null);
    copyOf(ImmutableList.of());
    int max = MAX_VALUE;
    int min = MIN_VALUE;
    systemUTC();
    Instant minInstant = MIN;
    Map<String, String> shortIds = SHORT_IDS;
    ZoneOffset utc = UTC;
    min(ImmutableSet.of());
    Locale english = ENGLISH;
    Locale root = ROOT;
    empty();
    just();

    list();
    new INSTANCE();
  }

  static final class WithMethodThatIsSelectivelyFlagged {
    static ImmutableList<String> list() {
      return ImmutableList.of();
    }
  }

  static final class Wrapper {
    static final class INSTANCE {}
  }
}

Copyright © 2017-2024 Picnic Technologies BV