Skip to content

Commit

Permalink
Don't change Javadoc method references to static import (#3756)
Browse files Browse the repository at this point in the history
* Don't change Javadoc method references to static import

Fixes #3663.

The Javadoc visitor invokes the Java visitor when visiting a method reference in Javadoc comments. Don't modify such method references to static imports.

* Inline `UseStaticImportJavadocVisitor`

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
Bananeweizen and timtebeek authored Dec 2, 2023
1 parent d0704ac commit eccdbd4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,42 @@ void sample() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/3663")
@Test
void javadocLinkUnchanged() {
rewriteRun(
spec -> spec.recipe(new UseStaticImport("java.util.Collections emptyList()")),
java(
"""
import java.util.Collections;
import java.util.List;
public class WithJavadoc {
/**
* This method uses {@link Collections#emptyList()}.
*/
public void mustNotChangeTheJavadocAbove() {
List<Object> list = Collections.emptyList();
}
}
""",
"""
import java.util.Collections;
import java.util.List;
import static java.util.Collections.emptyList;
public class WithJavadoc {
/**
* This method uses {@link Collections#emptyList()}.
*/
public void mustNotChangeTheJavadocAbove() {
List<Object> list = emptyList();
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Javadoc;

@ToString
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -87,5 +88,19 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}
return m;
}

@Override
protected JavadocVisitor<ExecutionContext> getJavadocVisitor() {
return new JavadocVisitor<ExecutionContext>(this) {
/**
* Do not visit the method referenced from the Javadoc.
* Otherwise, the Javadoc method reference would eventually be refactored to static import, which is not valid for Javadoc.
*/
@Override
public Javadoc visitReference(Javadoc.Reference reference, ExecutionContext p) {
return reference;
}
};
}
}
}

0 comments on commit eccdbd4

Please sign in to comment.