Skip to content

Commit

Permalink
Fix some failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jan 19, 2024
1 parent e47737d commit bc81bcb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/openrewrite/java/logging/AddLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.java.logging;

import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
Expand Down Expand Up @@ -114,10 +113,10 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
return cd;
}

cd = template.apply(new Cursor(getCursor().getParent(), cd), cd.getBody().getCoordinates().firstStatement(), loggerName, cd.getSimpleName());
cd = template.apply(updateCursor(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<ExecutionContext>().visitNonNull(cd, ctx, getCursor());
J.ClassDeclaration formatted = (J.ClassDeclaration) new AutoFormatVisitor<ExecutionContext>().visitNonNull(cd, ctx, getCursor().getParent());
cd = cd.withBody(cd.getBody().withStatements(ListUtils.map(cd.getBody().getStatements(), (i, stat) -> {
if (i == 1) {
return stat.withPrefix(formatted.getBody().getStatements().get(i).getPrefix());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.FindFieldsOfType;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import org.openrewrite.marker.Markers;

import java.time.Duration;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -76,17 +76,18 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (printStackTrace.matches(m)) {
J.ClassDeclaration clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class);
Set<J.VariableDeclarations> loggers = FindFieldsOfType.find(clazz, framework.getLoggerType());
Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
AnnotationService annotationService = service(AnnotationService.class);
Set<J.VariableDeclarations> loggers = FindFieldsOfType.find(classCursor.getValue(), framework.getLoggerType());
if (!loggers.isEmpty()) {
J.Identifier logField = loggers.iterator().next().getVariables().get(0).getName();
m = replaceMethodInvocation(m, logField);
} else if (clazz.getAllAnnotations().stream().anyMatch(lombokLogAnnotationMatcher::matches)) {
} else if (annotationService.matches(classCursor, lombokLogAnnotationMatcher)) {
String fieldName = loggerName == null ? "log" : loggerName;
J.Identifier logField = new J.Identifier(UUID.randomUUID(), Space.SINGLE_SPACE, Markers.EMPTY, Collections.emptyList(), fieldName, null, null);
m = replaceMethodInvocation(m, logField);
} else if (addLogger != null && addLogger) {
doAfterVisit(AddLogger.addLogger(clazz, framework, loggerName == null ? "logger" : loggerName));
doAfterVisit(AddLogger.addLogger(classCursor.getValue(), framework, loggerName == null ? "logger" : loggerName));
}
}
return m;
Expand All @@ -103,10 +104,10 @@ private J.MethodInvocation replaceMethodInvocation(J.MethodInvocation m, J.Ident
m.getSelect());
}
};
return addLogger != null && addLogger ? visitor : Preconditions.check(
return Repeat.repeatUntilStable(addLogger != null && addLogger ? visitor : Preconditions.check(
Preconditions.or(
new UsesType<>(framework.getLoggerType(), null),
new UsesType<>("lombok.extern..*", null))
, visitor);
, visitor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import org.openrewrite.java.*;
import org.openrewrite.java.search.FindFieldsOfType;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.service.AnnotationService;
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;
Expand Down Expand Up @@ -73,7 +73,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
LoggingFramework framework = LoggingFramework.fromOption(loggingFramework);
AnnotationMatcher lombokLogAnnotationMatcher = new AnnotationMatcher("@lombok.extern..*");

return Preconditions.check(new UsesMethod<>(systemErrPrint), new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(new UsesMethod<>(systemErrPrint), Repeat.repeatUntilStable(new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
J.Block b = super.visitBlock(block, ctx);
Expand Down Expand Up @@ -133,17 +133,18 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

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<J.VariableDeclarations> loggers = FindFieldsOfType.find(clazz, framework.getLoggerType());
Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
AnnotationService annotationService = service(AnnotationService.class);
Set<J.VariableDeclarations> loggers = FindFieldsOfType.find(classCursor.getValue(), framework.getLoggerType());
if (!loggers.isEmpty()) {
J.Identifier computedLoggerName = loggers.iterator().next().getVariables().get(0).getName();
print = replaceMethodInvocation(printCursor, ctx, exceptionPrintStackTrace, print, computedLoggerName);
} else if (clazz.getAllAnnotations().stream().anyMatch(lombokLogAnnotationMatcher::matches)) {
} else if (annotationService.matches(classCursor, lombokLogAnnotationMatcher)) {
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, exceptionPrintStackTrace, print, logField);
} else if (addLogger != null && addLogger) {
doAfterVisit(AddLogger.addLogger(clazz, framework, loggerName == null ? "logger" : loggerName));
doAfterVisit(AddLogger.addLogger(classCursor.getValue(), framework, loggerName == null ? "logger" : loggerName));
}
return print;
}
Expand Down Expand Up @@ -205,6 +206,6 @@ public JavaTemplate getErrorTemplateNoException() {
.build();
}
}
});
}));
}
}
18 changes: 10 additions & 8 deletions src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import org.openrewrite.java.*;
import org.openrewrite.java.search.FindFieldsOfType;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.service.AnnotationService;
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;
Expand Down Expand Up @@ -76,15 +76,16 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
LoggingFramework framework = LoggingFramework.fromOption(loggingFramework);
AnnotationMatcher lombokLogAnnotationMatcher = new AnnotationMatcher("@lombok.extern..*");

return Preconditions.check(new UsesMethod<>(systemOutPrint), new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(new UsesMethod<>(systemOutPrint), Repeat.repeatUntilStable(new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
Cursor cursor = updateCursor(m);
if (systemOutPrint.matches((Expression) method)) {
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(new Cursor(getCursor().getParent(), m), ctx);
return logInsteadOfPrint(cursor, ctx);
}
}
}
Expand All @@ -93,17 +94,18 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

private J.MethodInvocation logInsteadOfPrint(Cursor printCursor, ExecutionContext ctx) {
J.MethodInvocation print = printCursor.getValue();
J.ClassDeclaration clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class);
Set<J.VariableDeclarations> loggers = FindFieldsOfType.find(clazz, framework.getLoggerType());
Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
AnnotationService annotationService = service(AnnotationService.class);
Set<J.VariableDeclarations> loggers = FindFieldsOfType.find(classCursor.getValue(), framework.getLoggerType());
if (!loggers.isEmpty()) {
J.Identifier computedLoggerName = loggers.iterator().next().getVariables().get(0).getName();
print = replaceMethodInvocation(printCursor, ctx, print, computedLoggerName);
} else if (clazz.getAllAnnotations().stream().anyMatch(lombokLogAnnotationMatcher::matches)) {
} else if (annotationService.matches(classCursor, lombokLogAnnotationMatcher)) {
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));
doAfterVisit(AddLogger.addLogger(classCursor.getValue(), framework, loggerName == null ? "logger" : loggerName));
}
return print;
}
Expand Down Expand Up @@ -167,6 +169,6 @@ private String getLevel() {
}
return levelOrDefault;
}
});
}));
}
}

0 comments on commit bc81bcb

Please sign in to comment.