From 5485d36abaa7c4ef127cc0332ed5e2cd2033cb12 Mon Sep 17 00:00:00 2001 From: Anshuman Mishra Date: Thu, 19 Dec 2024 13:54:20 -0800 Subject: [PATCH 1/2] fix NPE for records --- .../java/migrate/joda/ScopeAwareVisitor.java | 9 +++++ .../java/migrate/joda/JodaTimeRecipeTest.java | 39 +++++++++++++++++++ 2 files changed, 48 insertions(+) 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 From 0a28830ae25956ce11b167043fd0937b816e9726 Mon Sep 17 00:00:00 2001 From: Anshuman Mishra Date: Thu, 19 Dec 2024 14:21:10 -0800 Subject: [PATCH 2/2] fix: Skip code references in comment --- .../java/migrate/joda/JodaTimeScanner.java | 19 +++++++--- .../java/migrate/joda/JodaTimeRecipeTest.java | 35 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeScanner.java b/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeScanner.java index 74df986dd..1bd326c99 100644 --- a/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeScanner.java +++ b/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeScanner.java @@ -26,11 +26,9 @@ import org.openrewrite.analysis.dataflow.Dataflow; import org.openrewrite.analysis.dataflow.analysis.SinkFlowSummary; import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; +import org.openrewrite.java.JavadocVisitor; +import org.openrewrite.java.tree.*; import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable; -import org.openrewrite.java.tree.JavaType; -import org.openrewrite.java.tree.MethodCall; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -52,6 +50,19 @@ public JodaTimeScanner(JodaTimeRecipe.Accumulator acc) { this.acc = acc; } + @Override + protected JavadocVisitor getJavadocVisitor() { + return new JavadocVisitor(this) { + /** + * Do not visit the method referenced from the Javadoc, may cause recipe to fail. + */ + @Override + public Javadoc visitReference(Javadoc.Reference reference, ExecutionContext ctx) { + return reference; + } + }; + } + @Override public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { super.visitCompilationUnit(cu, 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 4f3590a65..a54ef7cec 100644 --- a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java +++ b/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java @@ -467,4 +467,39 @@ public Interval interval() { ) ); } + + @Test + void migrateWithMethodReferenceInComment() { + //language=java + rewriteRun( + java( + """ + import org.joda.time.DateTime; + + class A { + public void foo() { + /** + * some method reference in comment + * {@link java.util.List#add(DateTime)} + */ + System.out.println(new DateTime()); + } + } + """, + """ + import java.time.ZonedDateTime; + + class A { + public void foo() { + /** + * some method reference in comment + * {@link java.util.List#add(DateTime)} + */ + System.out.println(ZonedDateTime.now()); + } + } + """ + ) + ); + } }