Skip to content

Commit

Permalink
fix: Skip code references in comment (#638)
Browse files Browse the repository at this point in the history
* fix NPE for records

* fix: Skip code references in comment
  • Loading branch information
amishra-u authored Dec 19, 2024
1 parent 9a50b85 commit 1bcf355
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +50,19 @@ public JodaTimeScanner(JodaTimeRecipe.Accumulator acc) {
this.acc = acc;
}

@Override
protected JavadocVisitor<ExecutionContext> getJavadocVisitor() {
return new JavadocVisitor<ExecutionContext>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
"""
)
);
}
}

0 comments on commit 1bcf355

Please sign in to comment.