From 68f1da981c3281066148a3c5809de98eee70a200 Mon Sep 17 00:00:00 2001 From: Nikita Eshkeev Date: Tue, 5 May 2020 00:27:52 +0300 Subject: [PATCH] [codeInsight] IDEA-113640 Provide intention to combine System.out.println(String.format(...)) into System.out.printf This patch removes StringUtil.isEmpty("%n") from RedundantStringFormatCallInspection#joinWithNewlineToken as it is always true. In addition to that it also includes: - inlining the getMethodNameRange method since the createProblemDescriptor method accepts a nullable parameter - moving RedundantStringFormatCallInspection#joinWithNewlineToken inside of StringFormatToPrintfQuickFix to reduce its visibility scope Signed-off-by: Nikita Eshkeev GitOrigin-RevId: e70f0b6be7eb158910016ac2bb31bb5667186c91 --- .../com/intellij/psi/util/PsiLiteralUtil.java | 7 +- .../RedundantStringFormatCallInspection.java | 68 ++++++++----------- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiLiteralUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiLiteralUtil.java index 9cae6add70f3..44eec6b67e09 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiLiteralUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiLiteralUtil.java @@ -3,15 +3,16 @@ package com.intellij.psi.util; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.*; +import com.intellij.psi.JavaTokenType; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiJavaToken; +import com.intellij.psi.PsiLiteralExpression; import com.intellij.psi.tree.IElementType; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class PsiLiteralUtil { @NonNls public static final String HEX_PREFIX = "0x"; @NonNls public static final String BIN_PREFIX = "0b"; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/RedundantStringFormatCallInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/RedundantStringFormatCallInspection.java index 43569dd7fd1f..18517c654c46 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/RedundantStringFormatCallInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/RedundantStringFormatCallInspection.java @@ -73,9 +73,7 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo final PsiExpression formatValue = args.getExpressions()[0]; if (containsNewlineToken(formatValue)) return null; - final TextRange textRange = getMethodNameRange(call); - if (textRange == null) return null; - return myManager.createProblemDescriptor(call, textRange, + return myManager.createProblemDescriptor(call, getMethodNameRange(call), InspectionGadgetsBundle.message("redundant.call.problem.descriptor"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly, new ReplaceWithPrintFix()); @@ -84,9 +82,7 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo @Nullable private ProblemDescriptor getRedundantStringFormatProblem(@NotNull final PsiMethodCallExpression call) { if (isStringFormatCallRedundant(call)) { - final TextRange textRange = getMethodNameRange(call); - if (textRange == null) return null; - return myManager.createProblemDescriptor(call, textRange, + return myManager.createProblemDescriptor(call, getMethodNameRange(call), InspectionGadgetsBundle.message("redundant.call.problem.descriptor"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly, new RemoveRedundantStringFormatFix()); @@ -97,9 +93,7 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo if (!PRINTSTREAM_PRINT.test(printlnCall)) return null; } - final TextRange textRange = getMethodNameRange(call); - if (textRange == null) return null; - return myManager.createProblemDescriptor(call, textRange, + return myManager.createProblemDescriptor(call, getMethodNameRange(call), InspectionGadgetsBundle.message("redundant.call.problem.descriptor"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly, new StringFormatToPrintfQuickFix(isPrintlnCall)); @@ -290,35 +284,33 @@ public final class RedundantStringFormatCallInspection extends LocalInspectionTo } } + @Contract(value = "null -> null; !null -> !null", pure = true) + private static PsiLiteralExpression joinWithNewlineToken(@Nullable final PsiLiteralExpression expression) { + if (expression == null) return null; + + final Object value = expression.getValue(); + if (value == null) return expression; + + final StringBuilder newExpression = new StringBuilder(); + + final String leftText = value.toString(); + if (expression.isTextBlock()) { + final String indent = StringUtil.repeat(" ", PsiLiteralUtil.getTextBlockIndent(expression)); + newExpression.append("\"\"\"").append('\n').append(indent); + newExpression.append(leftText.replaceAll("\n", "\n" + indent)); + newExpression.append("%n"); + newExpression.append("\"\"\""); + } + else { + newExpression.append('"'); + newExpression.append(StringUtil.escapeStringCharacters(leftText)); + newExpression.append("%n"); + newExpression.append('"'); + } + + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(expression.getProject()); + return (PsiLiteralExpression)factory.createExpressionFromText(newExpression.toString(), null); + } } } - - @Contract(value = "null -> null; !null -> !null", pure = true) - private static PsiLiteralExpression joinWithNewlineToken(@Nullable final PsiLiteralExpression expression) { - if (expression == null) return null; - if (StringUtil.isEmpty("%n")) return expression; - - final Object value = expression.getValue(); - if (value == null) return expression; - - final StringBuilder newExpression = new StringBuilder(); - - final String leftText = value.toString(); - if (expression.isTextBlock()) { - final String indent = StringUtil.repeat(" ", PsiLiteralUtil.getTextBlockIndent(expression)); - newExpression.append("\"\"\"").append('\n').append(indent); - newExpression.append(leftText.replaceAll("\n", "\n" + indent)); - newExpression.append("%n"); - newExpression.append("\"\"\""); - } - else { - newExpression.append('"'); - newExpression.append(StringUtil.escapeStringCharacters(leftText)); - newExpression.append("%n"); - newExpression.append('"'); - } - - final PsiElementFactory factory = JavaPsiFacade.getElementFactory(expression.getProject()); - return (PsiLiteralExpression)factory.createExpressionFromText(newExpression.toString(), null); - } }