From bc81bcba01b0e7d76c647266e9f2097fc101a641 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Fri, 19 Jan 2024 15:22:43 +0100 Subject: [PATCH] Fix some failing tests See: https://github.com/openrewrite/rewrite/pull/3921 --- .../openrewrite/java/logging/AddLogger.java | 5 ++--- .../logging/PrintStackTraceToLogError.java | 15 ++++++++------- .../java/logging/SystemErrToLogging.java | 15 ++++++++------- .../java/logging/SystemOutToLogging.java | 18 ++++++++++-------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/openrewrite/java/logging/AddLogger.java b/src/main/java/org/openrewrite/java/logging/AddLogger.java index 3cc276a..cdd8899 100644 --- a/src/main/java/org/openrewrite/java/logging/AddLogger.java +++ b/src/main/java/org/openrewrite/java/logging/AddLogger.java @@ -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; @@ -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().visitNonNull(cd, ctx, getCursor()); + J.ClassDeclaration formatted = (J.ClassDeclaration) new AutoFormatVisitor().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()); diff --git a/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java b/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java index d57b62a..f979931 100644 --- a/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java +++ b/src/main/java/org/openrewrite/java/logging/PrintStackTraceToLogError.java @@ -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; @@ -76,17 +76,18 @@ public TreeVisitor 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 loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); + Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance); + AnnotationService annotationService = service(AnnotationService.class); + Set 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; @@ -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)); } } diff --git a/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java index 45122c1..91b4839 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemErrToLogging.java @@ -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; @@ -73,7 +73,7 @@ public TreeVisitor getVisitor() { LoggingFramework framework = LoggingFramework.fromOption(loggingFramework); AnnotationMatcher lombokLogAnnotationMatcher = new AnnotationMatcher("@lombok.extern..*"); - return Preconditions.check(new UsesMethod<>(systemErrPrint), new JavaIsoVisitor() { + return Preconditions.check(new UsesMethod<>(systemErrPrint), Repeat.repeatUntilStable(new JavaIsoVisitor() { @Override public J.Block visitBlock(J.Block block, ExecutionContext ctx) { J.Block b = super.visitBlock(block, ctx); @@ -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 loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); + Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance); + AnnotationService annotationService = service(AnnotationService.class); + Set 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; } @@ -205,6 +206,6 @@ public JavaTemplate getErrorTemplateNoException() { .build(); } } - }); + })); } } diff --git a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java index 1b0c546..554289f 100644 --- a/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java +++ b/src/main/java/org/openrewrite/java/logging/SystemOutToLogging.java @@ -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; @@ -76,15 +76,16 @@ public TreeVisitor getVisitor() { LoggingFramework framework = LoggingFramework.fromOption(loggingFramework); AnnotationMatcher lombokLogAnnotationMatcher = new AnnotationMatcher("@lombok.extern..*"); - return Preconditions.check(new UsesMethod<>(systemOutPrint), new JavaIsoVisitor() { + return Preconditions.check(new UsesMethod<>(systemOutPrint), Repeat.repeatUntilStable(new JavaIsoVisitor() { @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); } } } @@ -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 loggers = FindFieldsOfType.find(clazz, framework.getLoggerType()); + Cursor classCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance); + AnnotationService annotationService = service(AnnotationService.class); + Set 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; } @@ -167,6 +169,6 @@ private String getLevel() { } return levelOrDefault; } - }); + })); } }