[java] UnnecessaryImportRule: Support Markdown comments

This commit is contained in:
Andreas Dangel
2024-07-20 16:58:34 +02:00
parent 7a6662fcda
commit 176e522721
2 changed files with 35 additions and 2 deletions

View File

@ -101,7 +101,18 @@ public class UnnecessaryImportRule extends AbstractJavaRule {
private static final Pattern LINK_IN_SNIPPET = Pattern
.compile("//\\s*@link\\s+(?:.*?)?target=[\"']?" + TYPE_PART_GROUP + "[\"']?");
private static final Pattern[] PATTERNS = { SEE_PATTERN, LINK_PATTERNS, VALUE_PATTERN, THROWS_PATTERN, EXCEPTION_PATTERN, LINK_IN_SNIPPET };
/*
* Java 23, JEP 467: Markdown Documentation Comments
*
* [Type#method()]
* [Type]
* [alternative Text][Type#method()]
* [alternative Text][Type]
*/
private static final Pattern MARKDOWN_PATTERN = Pattern.compile("\\[" + TYPE_PART_GROUP + "]");
private static final Pattern[] PATTERNS = { SEE_PATTERN, LINK_PATTERNS, VALUE_PATTERN, THROWS_PATTERN,
EXCEPTION_PATTERN, LINK_IN_SNIPPET, MARKDOWN_PATTERN };
@Override
public Object visit(ASTCompilationUnit node, Object data) {

View File

@ -203,8 +203,8 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.LinkedList;
import java.util.File;
import java.util.NoSuchElementException;
import java.io.File;
import java.io.IOException;
public class Foo {
@ -1237,4 +1237,26 @@ public class Example {
}
]]></code>
</test-code>
<test-code>
<description>imports used in markdown javadoc comment (since java 23, JEP 467)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.LinkedList;
import java.io.File;
public class Foo {
///
/// [`List`][List]
/// [arrayList][ArrayList]
/// [LinkedList]
/// [Calendar#DATE]
/// [File#exists()]
public void test() {}
}
]]></code>
</test-code>
</test-data>