diff --git a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java index 6402980..d54b682 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java @@ -22,13 +22,13 @@ import org.openrewrite.java.*; import org.openrewrite.java.search.FindFieldsOfType; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; -import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.java.tree.*; +import org.openrewrite.marker.Markers; import java.time.Duration; +import java.util.Collections; import java.util.Set; +import java.util.UUID; @Value @EqualsAndHashCode(callSuper = false) @@ -79,6 +79,7 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { LoggingFramework framework = LoggingFramework.fromOption(loggingFramework); + AnnotationMatcher lombokLogAnnotationMatcher = new AnnotationMatcher("@lombok.extern..*"); return Preconditions.check(new UsesMethod<>(systemOutPrint), new JavaIsoVisitor() { @Override @@ -101,19 +102,11 @@ private J.MethodInvocation logInsteadOfPrint(Cursor printCursor, ExecutionContex Set loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); if (!loggers.isEmpty()) { J.Identifier computedLoggerName = loggers.iterator().next().getVariables().get(0).getName(); - 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, printCursor); - - if (framework == LoggingFramework.JUL) { - maybeAddImport("java.util.logging.Level"); - } + print = replaceMethodInvocation(printCursor, ctx, print, computedLoggerName); + } else if (clazz.getAllAnnotations().stream().anyMatch(lombokLogAnnotationMatcher::matches)) { + String fieldName = loggerName == null ? "log" : loggerName; + J.Identifier logField = new J.Identifier(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, Collections.emptyList(), fieldName, null, null); + print = replaceMethodInvocation(printCursor, ctx, print, logField); } else if (addLogger != null && addLogger) { doAfterVisit(AddLogger.addLogger(clazz, framework, loggerName == null ? "logger" : loggerName)); @@ -123,6 +116,23 @@ private J.MethodInvocation logInsteadOfPrint(Cursor printCursor, ExecutionContex return print; } + private J.MethodInvocation replaceMethodInvocation(Cursor printCursor, ExecutionContext ctx, J.MethodInvocation print, J.Identifier computedLoggerName) { + 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, printCursor); + + if (framework == LoggingFramework.JUL) { + maybeAddImport("java.util.logging.Level"); + } + return print; + } + private

JavaTemplate getInfoTemplate(JavaVisitor

visitor) { String levelOrDefault = getLevel(); switch (framework) { diff --git a/src/test/java/org/openrewrite/java/logging/SystemOutToLoggingTest.java b/src/test/java/org/openrewrite/java/logging/SystemOutToLoggingTest.java index 5911c39..c1137a5 100644 --- a/src/test/java/org/openrewrite/java/logging/SystemOutToLoggingTest.java +++ b/src/test/java/org/openrewrite/java/logging/SystemOutToLoggingTest.java @@ -17,8 +17,10 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -88,4 +90,39 @@ void test() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-logging-frameworks/issues/114") + void supportLombokLogAnnotations() { + rewriteRun( + spec -> spec.recipe(new SystemOutToLogging(null, null, null, "info")) + .parser(JavaParser.fromJavaVersion().classpath("slf4j-api", "lombok")) + .typeValidationOptions(TypeValidation.builder().identifiers(false).build()), + //language=java + java( + """ + import lombok.extern.slf4j.Slf4j; + @Slf4j + class Test { + int n; + + void test() { + System.out.println("Oh " + n + " no"); + } + } + """, + """ + import lombok.extern.slf4j.Slf4j; + @Slf4j + class Test { + int n; + + void test() { + log.info("Oh {} no", n); + } + } + """ + ) + ); + } }