[codeInsight] IDEA-113640 Provide intention to combine System.out.println(String.format(...)) into System.out.printf

This patch adds processing of Java 14's text blocks to RedundantStringFormatCallInspection and fixes the problems from the code review

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: 126cfc001e7b201b62060333de7f71480403fb93
This commit is contained in:
Nikita Eshkeev
2020-05-07 22:24:17 +03:00
committed by intellij-monorepo-bot
parent c8bc41207e
commit 8db5967f7b
13 changed files with 479 additions and 99 deletions

View File

@@ -12,6 +12,7 @@ class Main {
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */);
System.out.printf(/* one */ Locale.US, /* two */ "%s, %s!" /* three */, /* four */ "Hello" /* five */, /* six */ "World" /* seven */);
System.out.print("hello" + String.format("%n"))
}
Main() {

View File

@@ -10,6 +10,7 @@ class Main {
: /* second leg start */ "%s: %s") + "%n" /* second leg end */,
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */);
System.out.println("hello" + String.format("%n"))
}
Main() {

View File

@@ -0,0 +1,19 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
package java.lang;
class String {
String(Object original) {}
public native String formatted(Object... args);
}
class Main {
static {
/* 6 */
String s = (/* 1 */ new /* 2 */ String(/* 3 */"""
Hello, World!
"""/* 4 */)/* 5 */);
String s1 = new String("""
%s, %s!
""").formatted("Hello", "World");
}
}

View File

@@ -0,0 +1,52 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
class Main {
static {
System.out.print(/* one */"""
Hello
""" /* two */);
System.out.printf(/* three */"""
Hello%n
""" /* four */);
}
Main() {
System.out.print(/* one */"""
Hello
""" /* two */);
System.out.printf(/* three */"""
Hello%n
""" /* four */);
}
void f() {
System.out.print(/* one */"""
Hello
""" /* two */);
System.out.printf(/* three */"""
Hello%n
""" /* four */);
}
void out(PrintStream printer) {
printer.print(/* one */"""
Hello
""" /* two */);
printer.printf(/* three */"""
Hello%n
""" /* four */);
}
void caller() {
printf(/* one */"""
Hello
""" /* two */);
printf(/* three */"""
Hello%n
""" /* four */);
}
static void printf(String value) {}
}

View File

@@ -0,0 +1,64 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
package java.lang;
import java.io.PrintStream;
class String {
String(Object original) {}
public native String formatted(Object... args);
}
class Main {
static {
/* one */
System.out.printf(/* three */(new String("""
%s, %s!
"""/* two */)) + "%n", "Hello"/* four */, /* six */ "World" /* seven */);
/* one */
System.out.printf(/* six */( /* two */ ( /* three */ new String("""
%s, %s!
"""/* four */) /* five */)), "Hello"/* seven */, /* eight */ "World" /* nine */);
/* one */
System.out.printf(/* six */new String("""
%s, %s!
"""/* two */), "Hello"/* seven */, /* eight */ "World" /* nine */);
System.out.printf(/* five */( /* one */ new String("""
%s,
""" /* two */) + /* three */
new String("""
%s!
""") + "%n"/* four */), "Hello"/* six */, /* seven */ "World" /* eight */);
System.out.printf(/* five */( /* one */ new String("""
%s,
""" /* two */) + new String(/* three */
"""
%s!
""")/* four */), "Hello"/* six */, /* seven */ "World" /* eight */);
}
Main() {
}
void f() {
}
void out(PrintStream printer) {
}
void caller() {
println(( /* one */ new String("""
%s,
""" /* two */ + /* three */
"""
%s!
"""/* four */)).formatted(/* five */"Hello"/* six */, /* seven */ "World" /* eight */));
}
static void println(String value) {}
}

View File

@@ -12,6 +12,7 @@ class Main {
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */));
System.out.print(String.format(/* one */ Locale.US, /* two */ "%s, %s!" /* three */, /* four */ "Hello" /* five */, /* six */ "World" /* seven */));
System.out.print("hello" + String.format("%n"))
}
Main() {

View File

@@ -10,6 +10,7 @@ class Main {
: /* second leg start */ "%s: %s" /* second leg end */,
/* first arg start */ "Hello"/* first arg end */,
/* second arg start */ "World" /* second arg end */));
System.out.println("hello" + String.format("%n"))
}
Main() {

View File

@@ -0,0 +1,18 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
package java.lang;
class String {
String(Object original) {}
public native String formatted(Object... args);
}
class Main {
static {
String s = (/* 1 */ new /* 2 */ String(/* 3 */"""
Hello, World!
"""/* 4 */)/* 5 */)./* 6 */<caret>formatted();
String s1 = new String("""
%s, %s!
""").formatted("Hello", "World");
}
}

View File

@@ -0,0 +1,52 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
class Main {
static {
System.out.<caret>printf(/* one */"""
Hello
""" /* two */);
System.out.printf(/* three */"""
Hello%n
""" /* four */);
}
Main() {
System.out.printf(/* one */"""
Hello
""" /* two */);
System.out.printf(/* three */"""
Hello%n
""" /* four */);
}
void f() {
System.out.printf(/* one */"""
Hello
""" /* two */);
System.out.printf(/* three */"""
Hello%n
""" /* four */);
}
void out(PrintStream printer) {
printer.printf(/* one */"""
Hello
""" /* two */);
printer.printf(/* three */"""
Hello%n
""" /* four */);
}
void caller() {
printf(/* one */"""
Hello
""" /* two */);
printf(/* three */"""
Hello%n
""" /* four */);
}
static void printf(String value) {}
}

View File

@@ -0,0 +1,62 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
package java.lang;
import java.io.PrintStream;
class String {
String(Object original) {}
public native String formatted(Object... args);
}
class Main {
static {
System.out.println(/* one */ new String("""
%s, %s!
"""/* two */).<caret>formatted(/* three */"Hello"/* four */, /* six */ "World" /* seven */));
System.out.print(/* one */ ( /* two */ ( /* three */ new String("""
%s, %s!
"""/* four */) /* five */))
.formatted(/* six */"Hello"/* seven */, /* eight */ "World" /* nine */));
System.out.print(/* one */ new String("""
%s, %s!
"""/* two */).formatted(/* six */"Hello"/* seven */, /* eight */ "World" /* nine */));
System.out.println(( /* one */ new String("""
%s,
""" /* two */) + /* three */
new String("""
%s!
""")/* four */).formatted(/* five */"Hello"/* six */, /* seven */ "World" /* eight */));
System.out.print(( /* one */ new String("""
%s,
""" /* two */) + new String(/* three */
"""
%s!
""")/* four */).formatted(/* five */"Hello"/* six */, /* seven */ "World" /* eight */));
}
Main() {
}
void f() {
}
void out(PrintStream printer) {
}
void caller() {
println(( /* one */ new String("""
%s,
""" /* two */ + /* three */
"""
%s!
"""/* four */)).formatted(/* five */"Hello"/* six */, /* seven */ "World" /* eight */));
}
static void println(String value) {}
}