Skip to content

Commit

Permalink
Also cover SystemOutToLogging
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 26, 2023
1 parent f19b1f0 commit 751f9e9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -79,6 +79,7 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
LoggingFramework framework = LoggingFramework.fromOption(loggingFramework);
AnnotationMatcher lombokLogAnnotationMatcher = new AnnotationMatcher("@lombok.extern..*");

return Preconditions.check(new UsesMethod<>(systemOutPrint), new JavaIsoVisitor<ExecutionContext>() {
@Override
Expand All @@ -101,19 +102,11 @@ private J.MethodInvocation logInsteadOfPrint(Cursor printCursor, ExecutionContex
Set<J.VariableDeclarations> 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));

Expand All @@ -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 <P> JavaTemplate getInfoTemplate(JavaVisitor<P> visitor) {
String levelOrDefault = getLevel();
switch (framework) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
"""
)
);
}
}

0 comments on commit 751f9e9

Please sign in to comment.