-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix #5852: [java] UnnecessaryImport with multiple static imports from same class #5854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
added test, could not fix impl. myself. Feel free to take over @oowekyala. |
]]></code> | ||
</test-code> | ||
<test-code> | ||
<description>#5852: [java] UnnecessaryImport with multiple static imports from same class</description> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c0df6dc
to
f609c55
Compare
f609c55
to
ea678bd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue ready to investigate by someone in touch with the impl.
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java
Show resolved
Hide resolved
...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml
Show resolved
Hide resolved
Compared to main: (comment created at 2025-06-27 21:29:24+00:00 for ea678bd) |
Thanks @iddeepak for the contribution on You pushed the code so far, the same success here would fix it all, thus the PMD appreciate community your efforts. The tests are working well, debug is easy. |
...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml
Show resolved
Hide resolved
...va/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml
Show resolved
Hide resolved
<!-- Test cases for imports used in module-info.java --> | ||
<!-- <test-code>--> | ||
<!-- <description>Import used in module-info requires</description>--> | ||
<!-- <expected-problems>0</expected-problems>--> | ||
<!-- <code><![CDATA[--> | ||
<!--import some.module.ModuleName;--> | ||
<!--module my.module {--> | ||
<!-- requires ModuleName;--> | ||
<!--}--> | ||
<!-- ]]></code>--> | ||
<!-- </test-code>--> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<!-- Test cases for imports used in module-info.java --> | |
<!-- <test-code>--> | |
<!-- <description>Import used in module-info requires</description>--> | |
<!-- <expected-problems>0</expected-problems>--> | |
<!-- <code><![CDATA[--> | |
<!--import some.module.ModuleName;--> | |
<!--module my.module {--> | |
<!-- requires ModuleName;--> | |
<!--}--> | |
<!-- ]]></code>--> | |
<!-- </test-code>--> |
imho dont invest on edge case.
]]></code> | ||
</test-code> | ||
<test-code> | ||
<description>#5852: [java] UnnecessaryImport with multiple static imports from same class - false negative</description> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we should add another test case. Your test case tests the behavior of PMD, if it is not used correctly - it is missing quarkus on the auxclass path - which means, we don't resolve the types and e.g. can't distinguish whether ConfigConfig
is a package name or a class (I assume, it is class because of naming conventions).
That's why you need to really reproduce the case correctly:
- Create a class
ConfigConfig
in src/test/java in packagenet.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
- This class should have the nested static class
Foo
which itself has two public static fieldsfail
andwarn
. - Maybe you can simplify the code a bit to get rid of unnecessary code lines which are irrelevant for this bug (e.g. no logger is required).
- In the end, you code sample should compile - this one doesn't...
Without a proper test, I'm not convinced, that the fix fixes the problem.
<description>Import used in annotation value</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import some.pkg.MyEnum; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should be improved: Please create the related classes/types in src/test/java in package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
so that we actually use type resolution correctly.
We probably also need to import MyAnnotation
.
Alternatively, you might find in the standard java api an enum and an anntoation, you can reuse in the test, so that you don't need to create your own types.
<description>Import used in array annotation value</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import some.pkg.MyEnum; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should be improved: Please create the related classes/types in src/test/java in package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
so that we actually use type resolution correctly.
import java.io.File; | ||
public class Foo { | ||
void test() { | ||
list.stream().map((File f) -> f.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't compile: list
is not known here, so PMD won't know, what the method stream()
will return etc...
import java.io.File; | ||
public class Foo { | ||
void test() { | ||
list.stream().map(File::getName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't compile: list
is not known here, so PMD won't know, what the method stream()
will return etc...
<description>Import used in switch expression</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import some.pkg.MyEnum; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should be improved: Please create the related classes/types in src/test/java in package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
so that we actually use type resolution correctly.
<code><![CDATA[ | ||
import some.pkg.MyEnum; | ||
public class Foo { | ||
String test(MyEnum e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, if this test case tests what you intended: The type MyEnum
is used here as the method parameter, so the import is for this line required.
The cases in the switch expression are unrelated and could be removed.
Maybe you want to test the reverse, e.g. the import static some.pkg.MyEnum.VALUE1
is unnecessary?
<description>Import used in sealed class hierarchy</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import some.pkg.Shape; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should be improved: Please create the related classes/types in src/test/java in package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
so that we actually use type resolution correctly.
import java.time.LocalDate; | ||
public class Foo { | ||
String test(String name) { | ||
return """ | ||
Hello %s! | ||
Today is %s | ||
""".formatted(name, LocalDate.now()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mabye this test case could be simplified? Text block is a string and text block is irrelevant here.
You can achieve the same effect with that:
import java.time.LocalDate;
public class Foo {
{
LocalDate.now();
}
}
<description>Import used in record pattern</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import some.pkg.Point; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should be improved: Please create the related classes/types in src/test/java in package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
so that we actually use type resolution correctly.
<description>Import used with unnamed pattern variable</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import some.pkg.Point; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should be improved: Please create the related classes/types in src/test/java in package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
so that we actually use type resolution correctly.
#5852: [java] UnnecessaryImport with multiple static imports from same class
Related issues
Ready?
./mvnw clean verify
passes (checked automatically by github actions)