Reports loops which could be replaced with single String.repeat() method (available since Java 11).

Example:


  void append(StringBuilder sb, int count, Object obj) {
    for (int i = 0; i < count; i++) {
      sb.append(obj);
    }
  }

After the quick-fix is applied the result looks like:


  void append(StringBuilder sb, int count, Object obj) {
    sb.append(String.valueOf(obj).repeat(Math.max(0, count)));
  }

By default the inspection may wrap count with Math.max(0, count) if it cannot prove statically that count is non-negative. This is done to prevent possible semantics change, as String.repeat() rejects negative numbers. You may disable this behavior via option if you don't like it. Similarly a string to repeat could be wrapped with String.valueOf to prevent possible NullPointerException if it's unknown whether it could be null.

This inspection only reports if the language level of the project or module is 11 or higher

New in 2019.1