[java-inspections] IDEA-357938 try to infer logging type inside methods for StringConcatenationArgumentToLogCallInspection

GitOrigin-RevId: a09f433bdc1cbed3c81ee4fab6f4986b5ada4271
This commit is contained in:
Mikhail Pyltsin
2024-09-18 14:42:24 +00:00
committed by intellij-monorepo-bot
parent 0cdf89293d
commit 3001c41aed
5 changed files with 41 additions and 2 deletions
@@ -142,6 +142,20 @@ public final class StringConcatenationArgumentToLogCallInspection extends BaseIn
if (qualifierExpression != null) {
boolean isFormatted = true;
if (qualifierExpression instanceof PsiMethodCallExpression callExpression) {
PsiMethod method = callExpression.resolveMethod();
if (method != null &&
method.getContainingFile() == qualifierExpression.getContainingFile() &&
(method.hasModifierProperty(PsiModifier.PRIVATE) ||
method.hasModifierProperty(PsiModifier.STATIC))) {
PsiReturnStatement[] statements = PsiUtil.findReturnStatements(method);
if (statements.length == 1) {
PsiReturnStatement statement = statements[0];
qualifierExpression = statement.getReturnValue();
}
}
}
if (qualifierExpression instanceof PsiReferenceExpression referenceExpression &&
referenceExpression.resolve() instanceof PsiVariable loggerVariable) {
if (!loggerVariable.isPhysical() ||
@@ -2,7 +2,7 @@ import org.apache.logging.log4j.*;
class Log4JFormatted {
private static final Logger logger = LogManager.getFormattedLogger(Log4JFormatted.class);
private static final Logger logger = LogManager.getFormatterLogger(Log4JFormatted.class);
public static void m(String a) {
logger.i<caret>nfo("12" + a);
@@ -0,0 +1,12 @@
import org.apache.logging.log4j.*;
class SimpleConcatenationInsideMethod {
public void m(String a) {
getLogger().i<caret>nfo("12{}", a);
}
private Logger getLogger() {
return LogManager.getLogger(SimpleConcatenationInsideMethod.class);
}
}
@@ -0,0 +1,12 @@
import org.apache.logging.log4j.*;
class SimpleConcatenationInsideMethod {
public void m(String a) {
getLogger().i<caret>nfo("12" + a);
}
private Logger getLogger() {
return LogManager.getLogger(SimpleConcatenationInsideMethod.class);
}
}
@@ -40,10 +40,10 @@ public class StringConcatenationArgumentToLogCallFixTest extends IGQuickFixesTes
public void testUseOfConstant() { doTest(); }
public void testCharLiteral() { doTest(); }
public void testQuoteCharLiteral() { doTest(); }
public void testLog4JLogBuilder() { doTest(); }
public void testLog4jFormatted() {
assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.call.quickfix"));
}
public void testLog4JLogBuilder() { doTest(); }
public void testTextBlocks() {
doTest(
InspectionsBundle.message("fix.all.inspection.problems.in.file",
@@ -54,6 +54,7 @@ public class StringConcatenationArgumentToLogCallFixTest extends IGQuickFixesTes
public void testMessageFormatMissingParameter() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.message.format.call.quickfix")); }
public void testMessageFormatMoreArguments() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.message.format.call.quickfix")); }
public void testMessageFormatFormatter() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.message.format.call.quickfix")); }
public void testSimpleConcatenationInsideMethod() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.call.quickfix")); }
public void testSimpleStringFormat() { doTest(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }
public void testStringFormatWithWidth() { assertQuickfixNotAvailable(InspectionGadgetsBundle.message("string.concatenation.argument.to.log.string.format.call.quickfix")); }