Merge pull request #4083 from abyss638:issue-4082-unnecessaryimport-nested-classes
[java] UnnecessaryImport false positive for on-demand imports of nested classes (fix for #4082) #4083
This commit is contained in:
6 files changed
+112
-37
No files matched your search
@@ -6768,6 +6768,15 @@
|
||||
"code",
|
||||
"financial"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "abyss638",
|
||||
"name": "Simon Abykov",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/90252673?v=4",
|
||||
"profile": "https://github.com/abyss638",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 7,
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -16,9 +16,14 @@ This is a {{ site.pmd.release_type }} release.
|
||||
|
||||
### Fixed Issues
|
||||
|
||||
* java-codestyle
|
||||
* [#4082](https://github.com/pmd/pmd/issues/4082): \[java] UnnecessaryImport false positive for on-demand imports of nested classes
|
||||
|
||||
### API Changes
|
||||
|
||||
### External Contributions
|
||||
|
||||
* [#4083](https://github.com/pmd/pmd/pull/4083): \[java] UnnecessaryImport false positive for on-demand imports of nested classes (fix for #4082) - [@abyss638](https://github.com/abyss638)
|
||||
|
||||
{% endtocmaker %}
|
||||
|
||||
+28
@@ -210,6 +210,20 @@ public class UnnecessaryImportRule extends AbstractJavaRule {
|
||||
}
|
||||
}
|
||||
|
||||
// check on-demand imports for inner classes
|
||||
it = imports.iterator();
|
||||
while (it.hasNext()) {
|
||||
ImportWrapper i = it.next();
|
||||
if (!i.isStaticOnDemand() && i.isOnDemand()) {
|
||||
String possibleClassName = i.getFullName() + "$" + candName;
|
||||
Class<?> possibleClazz = referenceNode.getRoot().getClassTypeResolver()
|
||||
.loadClassOrNull(possibleClassName);
|
||||
if (possibleClazz != null) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check static on-demand imports
|
||||
it = imports.iterator();
|
||||
while (it.hasNext()) {
|
||||
@@ -220,6 +234,20 @@ public class UnnecessaryImportRule extends AbstractJavaRule {
|
||||
}
|
||||
}
|
||||
|
||||
// check static on-demand imports for static inner classes
|
||||
it = imports.iterator();
|
||||
while (it.hasNext()) {
|
||||
ImportWrapper i = it.next();
|
||||
if (i.isStaticOnDemand()) {
|
||||
String possibleClassName = i.getFullName() + "$" + candName;
|
||||
Class<?> possibleClazz = referenceNode.getRoot().getClassTypeResolver()
|
||||
.loadClassOrNull(possibleClassName);
|
||||
if (possibleClazz != null) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (referenceNode instanceof TypeNode && ((TypeNode) referenceNode).getType() != null) {
|
||||
Class<?> c = ((TypeNode) referenceNode).getType();
|
||||
if (c.getPackage() != null) {
|
||||
|
||||
+4
@@ -7,5 +7,9 @@ package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.package2;
|
||||
public class C {
|
||||
private C() { }
|
||||
|
||||
public class IC { }
|
||||
|
||||
public static class ISC { }
|
||||
|
||||
public static final String V = "";
|
||||
}
|
||||
+28
@@ -916,6 +916,34 @@ public class U {
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[java] UnnecessaryImport false positive for on-demand imports of non-static nested classes</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.package1;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.package2.C.*; // SUPPRESS CHECKSTYLE needed for test case
|
||||
|
||||
public class U {
|
||||
IC c;
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[java] UnnecessaryImport false positive for static on-demand imports of static nested classes</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.package1;
|
||||
|
||||
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.package2.C.*; // SUPPRESS CHECKSTYLE needed for test case
|
||||
|
||||
public class U {
|
||||
ISC sc;
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>Necessary imports for @snippet tags introduced with JEP 413 in Java 18</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
|
||||
Reference in new issue
Block a user