报告可以替换为 switch 语句的 if 语句。

替换结果通常更简短、更清晰。

示例:


  void test(String str) {
    if (str.equals("1")) {
      System.out.println(1);
    } else if (str.equals("2")) {
      System.out.println(2);
    } else if (str.equals("3")) {
      System.out.println(3);
    } else {
      System.out.println(4);
    }
  }

在应用快速修复后:


  void test(String str) {
    switch (str) {
      case "1" -> System.out.println(1);
      case "2" -> System.out.println(2);
      case "3" -> System.out.println(3);
      default -> System.out.println(4);
    }
  }
  

项目或模块的语言级别为 7 级或更高级别时,才适用该检查。

使用 'if' 条件分支的最小数量字段指定必须报告 if 语句的最小 if 条件分支数。 注意,不计入终端 else 分支 (无 if)。

使用对数字启用 switch 建议选项,可以对基元和装箱的数字及字符启用 switch 语句建议。

使用对枚举启用 switch 建议选项,对 enum 常量启用 switch 语句建议。

使用仅对 null-safe 表达式启用建议选项,仅建议不能引入 NullPointerExceptionswitch 语句。