Merge pull request #4128 from OlegAndreych:Fix_#4103

[java] Fix False-positive UnnecessaryFullyQualifiedName when nested and non-nest… #4103 #4128
This commit is contained in:
Andreas Dangel 2022-09-29 16:24:15 +02:00
commit 5a08ad736a
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
5 changed files with 123 additions and 53 deletions

View File

@ -6835,6 +6835,15 @@
"contributions": [
"financial"
]
},
{
"login": "OlegAndreych",
"name": "Oleg Andreych",
"avatar_url": "https://avatars.githubusercontent.com/u/2041351?v=4",
"profile": "https://github.com/OlegAndreych",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,8 @@ from Lua. This means, that the Lua language in PMD can now parse both Lua and Lu
* doc
* [#4109](https://github.com/pmd/pmd/pull/4109): \[doc] Add page for 3rd party rulesets
* [#4124](https://github.com/pmd/pmd/pull/4124): \[doc] Fix typos in Java rule docs
* java-codestyle
* [#4085](https://github.com/pmd/pmd/issues/4085): \[java] UnnecessaryFullyQualifiedName false positive when nested and non-nested classes with the same name and in the same package are used together
### API Changes
@ -44,6 +46,7 @@ Many thanks to our sponsors:
* [#4066](https://github.com/pmd/pmd/pull/4066): \[lua] Add support for Luau syntax and skipping literal sequences in CPD - [Matt Hargett](https://github.com/matthargett) (@matthargett)
* [#4116](https://github.com/pmd/pmd/pull/4116): \[core] Fix missing --file arg in TreeExport CLI example - [mohan-chinnappan-n](https://github.com/mohan-chinnappan-n) (@mohan-chinnappan-n)
* [#4124](https://github.com/pmd/pmd/pull/4124) : \[doc] Fix typos in Java rule docs - [Piotrek Żygieło](https://github.com/pzygielo) (@pzygielo)
* [#4128](https://github.com/pmd/pmd/pull/4128): \[java] Fix False-positive UnnecessaryFullyQualifiedName when nested and non-nest… #4103 - [Oleg Andreych](https://github.com/OlegAndreych) (@OlegAndreych)
* [#4131](https://github.com/pmd/pmd/pull/4131): \[doc] TooFewBranchesForASwitchStatement - Use "if-else" instead of "if-then" - [Suvashri](https://github.com/Suvashri) (@Suvashri)
{% endtocmaker %}

View File

@ -17,7 +17,9 @@ import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTNameList;
@ -186,9 +188,12 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
if (matches.isEmpty()) {
if (isJavaLangImplicit(node)) {
addViolation(data, node, new Object[] { node.getImage(), "java.lang.*", "implicit "});
asCtx(data).addViolation(node,
node.getImage(), "java.lang.*", "implicit ");
} else if (isSamePackage(node, name)) {
addViolation(data, node, new Object[] { node.getImage(), currentPackage + ".*", "same package "});
if (!hasSameSimpleNameInScope(node)) {
asCtx(data).addViolation(node, node.getImage(), currentPackage + ".*", "same package ");
}
}
} else {
ASTImportDeclaration firstMatch = findFirstMatch(matches);
@ -199,11 +204,29 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
String importStr = firstMatch.getImportedName() + (firstMatch.isImportOnDemand() ? ".*" : "");
String type = firstMatch.isStatic() ? "static " : "";
addViolation(data, node, new Object[] { node.getImage(), importStr, type });
asCtx(data).addViolation(node, node.getImage(), importStr, type);
}
}
}
private boolean hasSameSimpleNameInScope(TypeNode node) {
final ASTCompilationUnit root = node.getRoot();
final List<ASTClassOrInterfaceDeclaration> declarationDescendants = root.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class);
final Class<?> nodeType = node.getType();
if (nodeType == null) {
return false;
}
for (ASTClassOrInterfaceDeclaration declarationDescendant : declarationDescendants) {
if (nodeType.getSimpleName().equals(declarationDescendant.getSimpleName())
&& !nodeType.getName().equals(declarationDescendant.getQualifiedName().toString())) {
return true;
}
}
return false;
}
private ASTImportDeclaration findFirstMatch(List<ASTImportDeclaration> imports) {
// first search only static imports
ASTImportDeclaration result = null;
@ -404,7 +427,7 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
// Is it a conflict with a class in the same file?
final Set<String> qualifiedTypes = node.getScope().getEnclosingScope(SourceFileScope.class)
.getQualifiedTypeNames().keySet();
.getQualifiedTypeNames().keySet();
for (final String qualified : qualifiedTypes) {
int fullLength = qualified.length();
if (qualified.endsWith(unqualifiedName)

View File

@ -622,6 +622,40 @@ public class UnnecessaryFullyQualifiedName {
]]></code>
</test-code>
<test-code>
<description>False positive when same package inner class is referenced (not enum) #4085</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname;
public class OuterTestClass {
public static class TestClass{
private final net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname.TestClass test;
public TestClass(net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname.TestClass test){
this.test = test;
}
}
}
]]></code>
</test-code>
<test-code>
<description>Should report fully-qualified name usage of a class in itself #4085</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname;
public class TestClass {
public static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname.TestClass INSTANCE(){
return new TestClass();
}
}
]]></code>
</test-code>
<test-code>
<description>#2098 false positive with annotated package</description>
<expected-problems>0</expected-problems>