From 1bcf355a78fc0994c98ca76812b0a6a6019b52fc Mon Sep 17 00:00:00 2001 From: amishra-u <119983081+amishra-u@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:13:38 -0800 Subject: [PATCH] fix: Skip code references in comment (#638) * fix NPE for records * 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()); + } + } + """ + ) + ); + } }