forked from phoedos/pmd
#1404 Java8 'Unnecessary use of fully qualified name' in Streams Collector
This commit is contained in:
@ -98,27 +98,16 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
|
||||
if (importDeclaration.isStatic()) {
|
||||
String[] importParts = importDeclaration.getImportedName().split("\\.");
|
||||
String[] nameParts = name.split("\\.");
|
||||
boolean checkClassImport = false;
|
||||
if (importDeclaration.isImportOnDemand()) {
|
||||
// Name class part matches class part of static import?
|
||||
if (nameParts[nameParts.length - 2].equals(importParts[importParts.length - 1])) {
|
||||
checkClassImport = true;
|
||||
matches.add(importDeclaration);
|
||||
}
|
||||
} else {
|
||||
// Last 2 parts match?
|
||||
if (nameParts[nameParts.length - 1].equals(importParts[importParts.length - 1])
|
||||
&& nameParts[nameParts.length - 2].equals(importParts[importParts.length - 2])) {
|
||||
checkClassImport = true;
|
||||
}
|
||||
}
|
||||
if (checkClassImport) {
|
||||
// Name class part matches a direct class import?
|
||||
String nameEnd = "." + nameParts[nameParts.length - 2];
|
||||
for (ASTImportDeclaration importDeclaration2 : imports) {
|
||||
if (!importDeclaration2.isStatic() && !importDeclaration2.isImportOnDemand()
|
||||
&& importDeclaration2.getImportedName().endsWith(nameEnd)) {
|
||||
matches.add(importDeclaration2);
|
||||
}
|
||||
matches.add(importDeclaration);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -126,8 +115,10 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
|
||||
}
|
||||
|
||||
if (!matches.isEmpty()) {
|
||||
String importStr = matches.get(0).getImportedName() + (matches.get(0).isImportOnDemand() ? ".*" : "");
|
||||
addViolation(data, node, new Object[] { node.getImage(), importStr });
|
||||
ASTImportDeclaration firstMatch = matches.get(0);
|
||||
String importStr = firstMatch.getImportedName() + (matches.get(0).isImportOnDemand() ? ".*" : "");
|
||||
String type = firstMatch.isStatic() ? "static " : "";
|
||||
addViolation(data, node, new Object[] { node.getImage(), importStr, type });
|
||||
}
|
||||
|
||||
matches.clear();
|
||||
|
@ -125,7 +125,7 @@ import static Yoko; // Too much !
|
||||
language="java"
|
||||
since="5.0"
|
||||
class="net.sourceforge.pmd.lang.java.rule.imports.UnnecessaryFullyQualifiedNameRule"
|
||||
message="Unnecessary use of fully qualified name ''{0}'' due to existing import ''{1}''"
|
||||
message="Unnecessary use of fully qualified name ''{0}'' due to existing {2}import ''{1}''"
|
||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/imports.html#UnnecessaryFullyQualifiedName">
|
||||
<description><![CDATA[
|
||||
Import statements allow the use of non-fully qualified names. The use of a fully qualified name
|
||||
|
@ -288,4 +288,34 @@ import a.*;
|
||||
public class Test {}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#1404 Java8 'Unnecessary use of fully qualified name' in Streams Collector</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>17</expected-linenumbers>
|
||||
<expected-messages>
|
||||
<message>Unnecessary use of fully qualified name 'Collectors.toList' due to existing static import 'java.util.stream.Collectors.toList'</message>
|
||||
</expected-messages>
|
||||
<code><![CDATA[
|
||||
// https://github.com/FenixEdu/fenixedu-learning/blob/master/src/main/java/org/fenixedu/learning/domain/executionCourse/components/InitialPageComponent.java#L50
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class InitialPageComponent extends BaseExecutionCourseComponent {
|
||||
public void handle(Page page, TemplateContext componentContext, TemplateContext globalContext) {
|
||||
ExecutionCourse executionCourse = ((ExecutionCourseSite) page.getSite()).getExecutionCourse();
|
||||
globalContext.put(
|
||||
"professorships",
|
||||
executionCourse
|
||||
.getProfessorshipsSet()
|
||||
.stream()
|
||||
.sorted(Comparator.comparing(Professorship::isResponsibleFor).reversed()
|
||||
.thenComparing(Professorship.COMPARATOR_BY_PERSON_NAME)).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
@ -28,6 +28,7 @@
|
||||
* [#1401](https://sourceforge.net/p/pmd/bugs/1401/): False positive for StringBuilder.append called with constructor
|
||||
* [#1402](https://sourceforge.net/p/pmd/bugs/1402/): Windows-Only: File exclusions are not case insensitive
|
||||
* [#1403](https://sourceforge.net/p/pmd/bugs/1403/): False positive UnusedPrivateMethod with JAVA8
|
||||
* [#1404](https://sourceforge.net/p/pmd/bugs/1404/): Java8 'Unnecessary use of fully qualified name' in Streams Collector
|
||||
* [#1405](https://sourceforge.net/p/pmd/bugs/1405/): UnusedPrivateMethod false positive?
|
||||
|
||||
**API Changes:**
|
||||
|
Reference in New Issue
Block a user