AmbiguousJsonCreator

WARNING

LikelyError

View source code on GitHub

Summary

JsonCreator.Mode should be set for single-argument creators

Suppression

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

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

Samples

Replacement

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

 import com.fasterxml.jackson.annotation.JsonCreator;
 
 enum A {
   FOO;
 
-  @JsonCreator
+  @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
   public static A of(String s) {
     return FOO;
   }
 }
 

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.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

interface Container {
  enum A {
    FOO(1);

    private final int i;

    A(int i) {
      this.i = i;
    }

    // BUG: Diagnostic matches: X
    @JsonCreator
    public static A of(int i) {
      return FOO;
    }
  }

  enum B {
    FOO(1);

    private final int i;

    B(int i) {
      this.i = i;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static B of(int i) {
      return FOO;
    }
  }

  enum C {
    FOO(1, "s");

    @JsonValue private final int i;
    private final String s;

    C(int i, String s) {
      this.i = i;
      this.s = s;
    }

    // BUG: Diagnostic matches: X
    @JsonCreator
    public static C of(int i) {
      return FOO;
    }
  }

  enum D {
    FOO(1, "s");

    private final int i;
    private final String s;

    D(int i, String s) {
      this.i = i;
      this.s = s;
    }

    @JsonCreator
    public static D of(int i, String s) {
      return FOO;
    }
  }

  enum E {
    FOO;

    // BUG: Diagnostic matches: X
    @JsonCreator
    public static E of(String s) {
      return FOO;
    }
  }

  class F {
    private final String s;

    F(String s) {
      this.s = s;
    }

    @JsonCreator
    public static F of(String s) {
      return new F(s);
    }
  }
}

Copyright © 2017-2024 Picnic Technologies BV