From 4f9c5c6754aafea7e62dc60131ad6dc9aef94dd0 Mon Sep 17 00:00:00 2001 From: Fabrice Tiercelin Date: Fri, 28 May 2021 07:05:38 +0200 Subject: [PATCH] Do not replace if literals are evaluated to null --- .../ui/fix/LogParametersRatherThanLogMessageCleanUp.java | 2 +- .../samples_in/LogParametersRatherThanLogMessageSample.java | 6 ++++++ .../LogParametersRatherThanLogMessageSample.java | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/LogParametersRatherThanLogMessageCleanUp.java b/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/LogParametersRatherThanLogMessageCleanUp.java index 0a32d28c5..84e112ec3 100644 --- a/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/LogParametersRatherThanLogMessageCleanUp.java +++ b/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/LogParametersRatherThanLogMessageCleanUp.java @@ -96,7 +96,7 @@ private boolean maybeReplaceConcatenation(final MethodInvocation visited, final hasLiteral= true; String literal= (String) string.resolveConstantExpressionValue(); - if (literal != null && (literal.contains("{") || literal.contains("}"))) { //$NON-NLS-1$ //$NON-NLS-2$ + if (literal == null || literal.contains("{") || literal.contains("}")) { //$NON-NLS-1$ //$NON-NLS-2$ return true; } diff --git a/samples/src/test/java/org/autorefactor/refactoring/rules/samples_in/LogParametersRatherThanLogMessageSample.java b/samples/src/test/java/org/autorefactor/refactoring/rules/samples_in/LogParametersRatherThanLogMessageSample.java index 29f3eaa3c..9a871fbdc 100644 --- a/samples/src/test/java/org/autorefactor/refactoring/rules/samples_in/LogParametersRatherThanLogMessageSample.java +++ b/samples/src/test/java/org/autorefactor/refactoring/rules/samples_in/LogParametersRatherThanLogMessageSample.java @@ -25,6 +25,8 @@ */ package org.autorefactor.refactoring.rules.samples_in; +import java.util.List; + import org.slf4j.Logger; public class LogParametersRatherThanLogMessageSample { @@ -93,4 +95,8 @@ public void doNotReplaceConcatenateSplitLiteral(Logger slf4jLog) { slf4jLog.debug("This text is very long and it can't be kept on a single line" + " so we have to break it in order to keep readability."); } + + public void doNotReplaceIfLiteralsAreEvaluatedToNull(Logger slf4jLog) { + slf4jLog.debug("aa" + "aa" + 1); + } } diff --git a/samples/src/test/java/org/autorefactor/refactoring/rules/samples_out/LogParametersRatherThanLogMessageSample.java b/samples/src/test/java/org/autorefactor/refactoring/rules/samples_out/LogParametersRatherThanLogMessageSample.java index 7040007fe..14a6135f5 100644 --- a/samples/src/test/java/org/autorefactor/refactoring/rules/samples_out/LogParametersRatherThanLogMessageSample.java +++ b/samples/src/test/java/org/autorefactor/refactoring/rules/samples_out/LogParametersRatherThanLogMessageSample.java @@ -25,6 +25,8 @@ */ package org.autorefactor.refactoring.rules.samples_out; +import java.util.List; + import org.slf4j.Logger; public class LogParametersRatherThanLogMessageSample { @@ -93,4 +95,8 @@ public void doNotReplaceConcatenateSplitLiteral(Logger slf4jLog) { slf4jLog.debug("This text is very long and it can't be kept on a single line" + " so we have to break it in order to keep readability."); } + + public void doNotReplaceIfLiteralsAreEvaluatedToNull(Logger slf4jLog) { + slf4jLog.debug("aa" + "aa" + 1); + } }