From 73eb21a1ae9475196c78d5ea7817ce7651f55093 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 1 Jun 2023 12:03:13 +0200 Subject: [PATCH] Adjust to new OpenRewrite 8.0 `JavaTemplate` API --- .../openrewrite/java/logging/AddLogger.java | 11 ++--- .../java/logging/LoggingFramework.java | 8 ++-- .../java/logging/ParameterizedLogging.java | 26 ++++------- .../logging/PrintStackTraceToLogError.java | 11 ++--- .../java/logging/SystemErrToLogging.java | 45 +++++++++++-------- .../java/logging/SystemOutToLogging.java | 13 +++--- .../logback/Log4jAppenderToLogback.java | 32 ++++++------- .../logging/logback/Log4jLayoutToLogback.java | 24 ++++------ .../slf4j/LoggersNamedForEnclosingClass.java | 19 +++----- .../slf4j/Slf4jLogShouldBeConstant.java | 8 ++-- 10 files changed, 91 insertions(+), 106 deletions(-) diff --git a/src/main/java/org/openrewrite/java/logging/AddLogger.java b/src/main/java/org/openrewrite/java/logging/AddLogger.java index f345d08..3cc276a 100644 --- a/src/main/java/org/openrewrite/java/logging/AddLogger.java +++ b/src/main/java/org/openrewrite/java/logging/AddLogger.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java.logging; +import org.openrewrite.Cursor; import org.openrewrite.ExecutionContext; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; @@ -65,7 +66,7 @@ public static AddLogger addSlf4jLogger(J.ClassDeclaration scope, String loggerNa return new AddLogger(scope, "org.slf4j.Logger", "org.slf4j.LoggerFactory", loggerName, visitor -> JavaTemplate .builder("private static final Logger #{} = LoggerFactory.getLogger(#{}.class);") - .context(visitor::getCursor) + .contextSensitive() .imports("org.slf4j.Logger", "org.slf4j.LoggerFactory") .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")) .build() @@ -76,7 +77,7 @@ public static AddLogger addJulLogger(J.ClassDeclaration scope, String loggerName return new AddLogger(scope, "java.util.logging.Logger", "java.util.logging.LogManager", loggerName, visitor -> JavaTemplate .builder("private static final Logger #{} = LogManager.getLogger(\"#{}\");") - .context(visitor::getCursor) + .contextSensitive() .imports("java.util.logging.Logger", "java.util.logging.LogManager") .build() ); @@ -86,7 +87,7 @@ public static AddLogger addLog4j1Logger(J.ClassDeclaration scope, String loggerN return new AddLogger(scope, "org.apache.log4j.Logger", "org.apache.log4j.LogManager", loggerName, visitor -> JavaTemplate .builder("private static final Logger #{} = LogManager.getLogger(#{}.class);") - .context(visitor::getCursor) + .contextSensitive() .imports("org.apache.log4j.Logger", "org.apache.log4j.LogManager") .javaParser(JavaParser.fromJavaVersion().classpath("log4j")) .build() @@ -97,7 +98,7 @@ public static AddLogger addLog4j2Logger(J.ClassDeclaration scope, String loggerN return new AddLogger(scope, "org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager", loggerName, visitor -> JavaTemplate .builder("private static final Logger #{} = LogManager.getLogger(#{}.class);") - .context(visitor::getCursor) + .contextSensitive() .imports("org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager") .javaParser(JavaParser.fromJavaVersion().classpath("log4j-api")) .build() @@ -113,7 +114,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex return cd; } - cd = cd.withTemplate(template, getCursor(), cd.getBody().getCoordinates().firstStatement(), loggerName, cd.getSimpleName()); + cd = template.apply(new Cursor(getCursor().getParent(), cd), cd.getBody().getCoordinates().firstStatement(), loggerName, cd.getSimpleName()); // ensure the appropriate number of blank lines on next statement after new field J.ClassDeclaration formatted = (J.ClassDeclaration) new AutoFormatVisitor().visitNonNull(cd, ctx, getCursor()); diff --git a/src/main/java/org/openrewrite/java/logging/LoggingFramework.java b/src/main/java/org/openrewrite/java/logging/LoggingFramework.java index 5755e88..cb589e6 100644 --- a/src/main/java/org/openrewrite/java/logging/LoggingFramework.java +++ b/src/main/java/org/openrewrite/java/logging/LoggingFramework.java @@ -58,27 +58,27 @@ public

JavaTemplate getErrorTemplate(JavaVisitor

visitor, String message) case SLF4J: return JavaTemplate .builder("#{any(org.slf4j.Logger)}.error(" + message + ", #{any(java.lang.Throwable)})") - .context(visitor::getCursor) + .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")) .build(); case Log4J1: return JavaTemplate .builder("#{any(org.apache.log4j.Category)}.error(" + message + ", #{any(java.lang.Throwable)})") - .context(visitor::getCursor) + .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpath("log4j")) .build(); case Log4J2: return JavaTemplate .builder("#{any(org.apache.logging.log4j.Logger)}.error(" + message + ", #{any(java.lang.Throwable)})") - .context(visitor::getCursor) + .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpath("log4j-api")) .build(); case JUL: default: return JavaTemplate .builder("#{any(java.util.logging.Logger)}.log(Level.SEVERE, " + message + ", #{any(java.lang.Throwable)})") - .context(visitor::getCursor) + .contextSensitive() .imports("java.util.logging.Level") .build(); diff --git a/src/main/java/org/openrewrite/java/logging/ParameterizedLogging.java b/src/main/java/org/openrewrite/java/logging/ParameterizedLogging.java index 660407d..9965ea9 100644 --- a/src/main/java/org/openrewrite/java/logging/ParameterizedLogging.java +++ b/src/main/java/org/openrewrite/java/logging/ParameterizedLogging.java @@ -95,25 +95,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu }); messageBuilder.append("\""); newArgList.forEach(arg -> messageBuilder.append(", #{any()}")); - m = m.withTemplate( - JavaTemplate.builder(messageBuilder.toString()) - .context(getCursor()) - .build(), - getCursor(), - m.getCoordinates().replaceArguments(), - newArgList.toArray() - ); + m = JavaTemplate.builder(messageBuilder.toString()) + .contextSensitive() + .build() + .apply(new Cursor(getCursor().getParent(), m), m.getCoordinates().replaceArguments(), newArgList.toArray()); } else if (!TypeUtils.isString(logMsg.getType()) && logMsg.getType() instanceof JavaType.Class) { StringBuilder messageBuilder = new StringBuilder("\"{}\""); m.getArguments().forEach(arg -> messageBuilder.append(", #{any()}")); - m = m.withTemplate( - JavaTemplate.builder(messageBuilder.toString()) - .context(getCursor()) - .build(), - getCursor(), - m.getCoordinates().replaceArguments(), - m.getArguments().toArray() - ); + m = JavaTemplate.builder(messageBuilder.toString()) + .contextSensitive() + .build() + .apply(new Cursor(getCursor().getParent(), m), m.getCoordinates().replaceArguments(), m.getArguments().toArray()); } if (Boolean.TRUE.equals(removeToString)) { m = m.withArguments(ListUtils.map(m.getArguments(), arg -> (Expression) removeToStringVisitor.visitNonNull(arg, ctx, getCursor()))); @@ -134,7 +126,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) if (TO_STRING.matches(method.getSelect())) { getCursor().putMessage("DO_NOT_REMOVE", Boolean.TRUE); } else if (TO_STRING.matches(method)) { - return method.withTemplate(t, getCursor(), method.getCoordinates().replace(), method.getSelect()); + return t.apply(getCursor(), method.getCoordinates().replace(), method.getSelect()); } return super.visitMethodInvocation(method, ctx); } diff --git a/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java b/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java index 274837d..0733ef3 100644 --- a/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java +++ b/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java @@ -78,11 +78,12 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu J.ClassDeclaration clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class); Set loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); if (!loggers.isEmpty()) { - m = m.withTemplate(framework.getErrorTemplate(this, "\"Exception\""), - getCursor(), - m.getCoordinates().replace(), - loggers.iterator().next().getVariables().get(0).getName(), - m.getSelect()); + m = framework.getErrorTemplate(this, "\"Exception\"") + .apply( + new Cursor(getCursor().getParent(), m), + m.getCoordinates().replace(), + loggers.iterator().next().getVariables().get(0).getName(), + m.getSelect()); if (framework == LoggingFramework.JUL) { maybeAddImport("java.util.logging.Level"); } diff --git a/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java index 952e782..0d2128c 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java @@ -42,6 +42,7 @@ public class SystemErrToLogging extends Recipe { public Duration getEstimatedEffortPerOccurrence() { return Duration.ofMinutes(5); } + private static final MethodMatcher printStackTrace = new MethodMatcher("java.lang.Throwable printStackTrace(..)"); @Option(displayName = "Add logger", @@ -81,6 +82,7 @@ public TreeVisitor getVisitor() { @Override public J.Block visitBlock(J.Block block, ExecutionContext ctx) { J.Block b = super.visitBlock(block, ctx); + Cursor blockCursor = new Cursor(getCursor().getParent(), b); AtomicBoolean addedLogger = new AtomicBoolean(false); AtomicInteger skip = new AtomicInteger(-1); @@ -103,7 +105,8 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { } } - J.MethodInvocation unchangedIfAddedLogger = logInsteadOfPrint(m, ctx, exceptionPrintStackTrace); + Cursor printCursor = new Cursor(blockCursor, m); + J.MethodInvocation unchangedIfAddedLogger = logInsteadOfPrint(printCursor, ctx, exceptionPrintStackTrace); addedLogger.set(unchangedIfAddedLogger == m); return unchangedIfAddedLogger; } @@ -124,7 +127,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu if (m.getSelect() != null && m.getSelect() instanceof J.FieldAccess) { JavaType.Variable field = ((J.FieldAccess) m.getSelect()).getName().getFieldType(); if (field != null && "err".equals(field.getName()) && TypeUtils.isOfClassType(field.getOwner(), "java.lang.System")) { - return logInsteadOfPrint(m, ctx, null); + Cursor printCursor = new Cursor(getCursor().getParent(), m); + return logInsteadOfPrint(printCursor, ctx, null); } } } @@ -132,29 +136,32 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu return m; } - private J.MethodInvocation logInsteadOfPrint(J.MethodInvocation print, ExecutionContext ctx, @Nullable Expression exceptionPrintStackTrace) { + private J.MethodInvocation logInsteadOfPrint(Cursor printCursor, ExecutionContext ctx, @Nullable Expression exceptionPrintStackTrace) { + J.MethodInvocation print = printCursor.getValue(); J.ClassDeclaration clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class); Set loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); if (!loggers.isEmpty()) { J.Identifier computedLoggerName = loggers.iterator().next().getVariables().get(0).getName(); if (exceptionPrintStackTrace == null) { - print = print.withTemplate(getErrorTemplateNoException(this), - getCursor(), - print.getCoordinates().replace(), - computedLoggerName, - print.getArguments().get(0)); + print = getErrorTemplateNoException(this) + .apply( + printCursor, + print.getCoordinates().replace(), + computedLoggerName, + print.getArguments().get(0)); } else { - print = print.withTemplate(framework.getErrorTemplate(this, "#{any(String)}"), - getCursor(), - print.getCoordinates().replace(), - computedLoggerName, - print.getArguments().get(0), - exceptionPrintStackTrace); + print = framework.getErrorTemplate(this, "#{any(String)}") + .apply( + printCursor, + print.getCoordinates().replace(), + computedLoggerName, + print.getArguments().get(0), + exceptionPrintStackTrace); } print = (J.MethodInvocation) new ParameterizedLogging(framework.getLoggerType() + " error(..)", false) .getVisitor() - .visitNonNull(print, ctx, getCursor()); + .visitNonNull(print, ctx, printCursor); if (framework == LoggingFramework.JUL) { maybeAddImport("java.util.logging.Level"); @@ -173,27 +180,27 @@ public

JavaTemplate getErrorTemplateNoException(JavaVisitor

visitor) { case SLF4J: return JavaTemplate .builder("#{any(org.slf4j.Logger)}.error(#{any(String)})") - .context(visitor::getCursor) + .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")) .build(); case Log4J1: return JavaTemplate .builder("#{any(org.apache.log4j.Category)}.error(#{any(String)})") - .context(visitor::getCursor) + .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpath("log4j")) .build(); case Log4J2: return JavaTemplate .builder("#{any(org.apache.logging.log4j.Logger)}.error(#{any(String)})") - .context(visitor::getCursor) + .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpath("log4j-api")) .build(); case JUL: default: return JavaTemplate .builder("#{any(java.util.logging.Logger)}.log(Level.SEVERE, #{any(String)})") - .context(visitor::getCursor) + .contextSensitive() .imports("java.util.logging.Level") .build(); } diff --git a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java index 482e632..6402980 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java @@ -88,27 +88,28 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu if (m.getSelect() != null && m.getSelect() instanceof J.FieldAccess) { JavaType.Variable field = ((J.FieldAccess) m.getSelect()).getName().getFieldType(); if (field != null && "out".equals(field.getName()) && TypeUtils.isOfClassType(field.getOwner(), "java.lang.System")) { - return logInsteadOfPrint(m, ctx); + return logInsteadOfPrint(new Cursor(getCursor().getParent(), m), ctx); } } } return m; } - private J.MethodInvocation logInsteadOfPrint(J.MethodInvocation print, ExecutionContext ctx) { + private J.MethodInvocation logInsteadOfPrint(Cursor printCursor, ExecutionContext ctx) { + J.MethodInvocation print = printCursor.getValue(); J.ClassDeclaration clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class); Set loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); if (!loggers.isEmpty()) { J.Identifier computedLoggerName = loggers.iterator().next().getVariables().get(0).getName(); - print = print.withTemplate(getInfoTemplate(this), - getCursor(), + print = getInfoTemplate(this).apply( + printCursor, print.getCoordinates().replace(), computedLoggerName, print.getArguments().get(0)); print = (J.MethodInvocation) new ParameterizedLogging(framework.getLoggerType() + " " + getLevel() + "(..)", false) .getVisitor() - .visitNonNull(print, ctx, getCursor()); + .visitNonNull(print, ctx, printCursor); if (framework == LoggingFramework.JUL) { maybeAddImport("java.util.logging.Level"); @@ -153,7 +154,7 @@ private

JavaTemplate getInfoTemplate(JavaVisitor

visitor) { private String getLevel() { String levelOrDefault = level == null ? "info" : level; - if(framework == LoggingFramework.JUL) { + if (framework == LoggingFramework.JUL) { String julLevel = levelOrDefault.toUpperCase(); if ("debug".equals(levelOrDefault)) { julLevel = "FINE"; diff --git a/src/main/java/org/openrewrite/java/logging/logback/Log4jAppenderToLogback.java b/src/main/java/org/openrewrite/java/logging/logback/Log4jAppenderToLogback.java index 9717933..c57d46e 100644 --- a/src/main/java/org/openrewrite/java/logging/logback/Log4jAppenderToLogback.java +++ b/src/main/java/org/openrewrite/java/logging/logback/Log4jAppenderToLogback.java @@ -15,10 +15,7 @@ */ package org.openrewrite.java.logging.logback; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; +import org.openrewrite.*; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.*; import org.openrewrite.java.search.UsesType; @@ -37,10 +34,10 @@ public String getDisplayName() { @Override public String getDescription() { return "Migrates custom Log4j 2.x Appender components to `logback-classic`. This recipe operates on the following assumptions: " + - "1.) The contents of the `append()` method remains unchanged. " + - "2.) The `requiresLayout()` method is not used in logback and can be removed. " + - "3.) In logback, the `stop()` method is the equivalent of log4j's close() method. " + - "For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html)."; + "1.) The contents of the `append()` method remains unchanged. " + + "2.) The `requiresLayout()` method is not used in logback and can be removed. " + + "3.) In logback, the `stop()` method is the equivalent of log4j's close() method. " + + "For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html)."; } @Override @@ -72,17 +69,14 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex doAfterVisit(new ChangeType("org.apache.log4j.spi.LoggingEvent", "ch.qos.logback.classic.spi.ILoggingEvent", null).getVisitor()); doAfterVisit(new ChangeType("org.apache.log4j.Layout", "ch.qos.logback.core.LayoutBase", null).getVisitor()); - cd = cd.withTemplate( - JavaTemplate.builder("AppenderBase") - .context(getCursor()) - .imports("ch.qos.logback.core.AppenderBase", "ch.qos.logback.classic.spi.ILoggingEvent") - .javaParser(JavaParser.fromJavaVersion().dependsOn( - "package ch.qos.logback.classic.spi;public interface ILoggingEvent{ }", - "package org.apache.log4j.spi;public class LoggingEvent { public String getRenderedMessage() {return null;}}")) - .build(), - getCursor(), - cd.getCoordinates().replaceExtendsClause() - ); + cd = JavaTemplate.builder("AppenderBase") + .contextSensitive() + .imports("ch.qos.logback.core.AppenderBase", "ch.qos.logback.classic.spi.ILoggingEvent") + .javaParser(JavaParser.fromJavaVersion().dependsOn( + "package ch.qos.logback.classic.spi;public interface ILoggingEvent{ }", + "package org.apache.log4j.spi;public class LoggingEvent { public String getRenderedMessage() {return null;}}")) + .build() + .apply(new Cursor(getCursor().getParent(), cd), cd.getCoordinates().replaceExtendsClause()); // should be covered by maybeAddImport, todo doAfterVisit(new AddImport<>("ch.qos.logback.core.AppenderBase", null, false)); diff --git a/src/main/java/org/openrewrite/java/logging/logback/Log4jLayoutToLogback.java b/src/main/java/org/openrewrite/java/logging/logback/Log4jLayoutToLogback.java index 4cb865b..5875499 100644 --- a/src/main/java/org/openrewrite/java/logging/logback/Log4jLayoutToLogback.java +++ b/src/main/java/org/openrewrite/java/logging/logback/Log4jLayoutToLogback.java @@ -15,10 +15,7 @@ */ package org.openrewrite.java.logging.logback; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; +import org.openrewrite.*; import org.openrewrite.internal.ListUtils; import org.openrewrite.java.*; import org.openrewrite.java.search.UsesType; @@ -77,17 +74,14 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex doAfterVisit(new ChangeType("org.apache.log4j.spi.LoggingEvent", "ch.qos.logback.classic.spi.ILoggingEvent", null).getVisitor()); - cd = cd.withTemplate( - JavaTemplate.builder("LayoutBase") - .context(getCursor()) - .imports("ch.qos.logback.core.LayoutBase", "ch.qos.logback.classic.spi.ILoggingEvent") - .javaParser(JavaParser.fromJavaVersion().dependsOn( - "package ch.qos.logback.classic.spi;public interface ILoggingEvent{ }", - "package org.apache.log4j.spi;public class LoggingEvent { public String getRenderedMessage() {return null;}}")) - .build(), - getCursor(), - cd.getCoordinates().replaceExtendsClause() - ); + cd = JavaTemplate.builder("LayoutBase") + .contextSensitive() + .imports("ch.qos.logback.core.LayoutBase", "ch.qos.logback.classic.spi.ILoggingEvent") + .javaParser(JavaParser.fromJavaVersion().dependsOn( + "package ch.qos.logback.classic.spi;public interface ILoggingEvent{ }", + "package org.apache.log4j.spi;public class LoggingEvent { public String getRenderedMessage() {return null;}}")) + .build() + .apply(new Cursor(getCursor().getParent(), cd), cd.getCoordinates().replaceExtendsClause()); // should be covered by maybeAddImport, todo doAfterVisit(new AddImport<>("ch.qos.logback.core.LayoutBase", null, false)); diff --git a/src/main/java/org/openrewrite/java/logging/slf4j/LoggersNamedForEnclosingClass.java b/src/main/java/org/openrewrite/java/logging/slf4j/LoggersNamedForEnclosingClass.java index 68d8304..93c3966 100644 --- a/src/main/java/org/openrewrite/java/logging/slf4j/LoggersNamedForEnclosingClass.java +++ b/src/main/java/org/openrewrite/java/logging/slf4j/LoggersNamedForEnclosingClass.java @@ -15,10 +15,7 @@ */ package org.openrewrite.java.logging.slf4j; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; +import org.openrewrite.*; import org.openrewrite.java.*; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.J; @@ -93,14 +90,12 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu return mi; } - return mi.withTemplate(JavaTemplate.builder("LoggerFactory.getLogger(#{})") - .context(getCursor()) - .imports("org.slf4j.LoggerFactory") - .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")) - .build(), - getCursor(), - mi.getCoordinates().replace(), - enclosingClazzName); + return JavaTemplate.builder("LoggerFactory.getLogger(#{})") + .contextSensitive() + .imports("org.slf4j.LoggerFactory") + .javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api")) + .build() + .apply(new Cursor(getCursor().getParent(), mi), mi.getCoordinates().replace(), enclosingClazzName); } }); } diff --git a/src/main/java/org/openrewrite/java/logging/slf4j/Slf4jLogShouldBeConstant.java b/src/main/java/org/openrewrite/java/logging/slf4j/Slf4jLogShouldBeConstant.java index 5f6479e..69a92cc 100644 --- a/src/main/java/org/openrewrite/java/logging/slf4j/Slf4jLogShouldBeConstant.java +++ b/src/main/java/org/openrewrite/java/logging/slf4j/Slf4jLogShouldBeConstant.java @@ -101,16 +101,16 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu } else if (STRING_VALUE_OF.matches(args.get(0))) { Expression valueOf = ((J.MethodInvocation) args.get(0)).getArguments().get(0); if (TypeUtils.isAssignableTo(JavaType.ShallowClass.build("java.lang.Throwable"), valueOf.getType())) { - J.MethodInvocation m = method.withTemplate(JavaTemplate.builder("\"Exception\", #{any()}").context(getCursor()).build(), - getCursor(), method.getCoordinates().replaceArguments(), valueOf); + J.MethodInvocation m = JavaTemplate.builder("\"Exception\", #{any()}").contextSensitive().build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), valueOf); m = m.withSelect(method.getSelect()); return m; } } else if (args.get(0) instanceof J.MethodInvocation && "toString".equals(((J.MethodInvocation) args.get(0)).getSimpleName())) { Expression valueOf = ((J.MethodInvocation) args.get(0)).getSelect(); if (valueOf != null) { - J.MethodInvocation m = method.withTemplate(JavaTemplate.builder("\"{}\", #{any()}").context(getCursor()).build(), - getCursor(), method.getCoordinates().replaceArguments(), valueOf); + J.MethodInvocation m = JavaTemplate.builder("\"{}\", #{any()}").contextSensitive().build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), valueOf); m = m.withSelect(method.getSelect()); return m; }