diff --git a/src/main/java/org/openrewrite/java/migrate/joda/ScopeAwareVisitor.java b/src/main/java/org/openrewrite/java/migrate/joda/ScopeAwareVisitor.java index 0858928f0..d25157f0e 100644 --- a/src/main/java/org/openrewrite/java/migrate/joda/ScopeAwareVisitor.java +++ b/src/main/java/org/openrewrite/java/migrate/joda/ScopeAwareVisitor.java @@ -34,6 +34,9 @@ class ScopeAwareVisitor extends JavaVisitor { @Override public J preVisit(J j, ExecutionContext ctx) { + if (j instanceof J.ClassDeclaration) { + scopes.push(new VariablesInScope(getCursor())); + } if (j instanceof J.Block) { scopes.push(new VariablesInScope(getCursor())); } @@ -50,9 +53,15 @@ public J preVisit(J j, ExecutionContext ctx) { @Override public J postVisit(J j, ExecutionContext ctx) { + if (j instanceof J.ClassDeclaration) { + scopes.pop(); + } if (j instanceof J.Block) { scopes.pop(); } + if (j instanceof J.MethodDeclaration) { + scopes.pop(); + } return super.postVisit(j, ctx); } diff --git a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java b/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java index 2265fab7e..4f3590a65 100644 --- a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java +++ b/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java @@ -280,6 +280,45 @@ public void bar(ZonedDateTime dt) { ); } + @Test + void migrationWithRecord() { + //language=java + rewriteRun( + java( + """ + import org.joda.time.DateTime; + + record A(String x) { + public void foo() { + new Bar().bar(new DateTime()); + } + + private static class Bar { + public void bar(DateTime dt) { + dt.getMillis(); + } + } + } + """, + """ + import java.time.ZonedDateTime; + + record A(String x) { + public void foo() { + new Bar().bar(ZonedDateTime.now()); + } + + private static class Bar { + public void bar(ZonedDateTime dt) { + dt.toInstant().toEpochMilli(); + } + } + } + """ + ) + ); + } + @Test void migrateMethodWithSafeReturnExpression() { //language=java