Skip to content

Commit

Permalink
Issue checkstyle#3453: UnusedImportsCheck: handle qualified names in …
Browse files Browse the repository at this point in the history
…javadoc
  • Loading branch information
cushon committed Sep 2, 2017
1 parent 1282cdf commit 3955975
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Expand Up @@ -300,8 +300,27 @@ private static Set<String> matchPattern(String identifier, Pattern pattern) {
final Set<String> references = new HashSet<>();
final Matcher matcher = pattern.matcher(identifier);
while (matcher.find()) {
references.add(matcher.group(1));
references.add(topLevelType(matcher.group(1)));
}
return references;
}

/**
* If the given type string contains "." (e.g. "Map.Entry"), returns the
* top level type (e.g. "Map"), as that is what must be imported for the
* type to resolve. Otherwise, returns the type as-is.
* @param type A possibly qualified type name
* @return The simple name of the top level type
*/
private static String topLevelType(String type) {
final String topLevelType;
final int dotIndex = type.indexOf('.');
if (dotIndex == -1) {
topLevelType = type;
}
else {
topLevelType = type.substring(0, dotIndex);
}
return topLevelType;
}
}
Expand Up @@ -238,4 +238,13 @@ public void testImportsFromJavaLang() throws Exception {
};
verify(checkConfig, getPath("InputUnusedImportsFromJavaLang.java"), expected);
}

@Test
public void testImportsJavadocQualifiedName() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(UnusedImportsCheck.class);
final String[] expected = {
"4:8: " + getCheckMessage(MSG_KEY, "java.util.List"),
};
verify(checkConfig, getPath("InputUnusedImportsJavadocQualifiedName.java"), expected);
}
}
@@ -0,0 +1,9 @@
package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports;

import java.util.Map; // OK
import java.util.List; // VIOLATION

/**
* Use {@link Map.Entry} in this javadoc.
*/
public class InputUnusedImportsJavadocQualifiedName {}

0 comments on commit 3955975

Please sign in to comment.