Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Pankraz76
Copy link
Contributor

@Pankraz76 Pankraz76 commented Jun 26, 2025

#5852: [java] UnnecessaryImport with multiple static imports from same class

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@Pankraz76
Copy link
Contributor Author

added test, could not fix impl. myself. Feel free to take over @oowekyala.

@Pankraz76 Pankraz76 closed this Jun 26, 2025
@Pankraz76 Pankraz76 reopened this Jun 26, 2025
@adangel adangel changed the title fix #5852: [java] UnnecessaryImport with multiple static imports from same class Fix #5852: [java] UnnecessaryImport with multiple static imports from same class Jun 27, 2025
]]></code>
</test-code>
<test-code>
<description>#5852: [java] UnnecessaryImport with multiple static imports from same class</description>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue:

image

@Pankraz76 Pankraz76 force-pushed the fix-UnnecessaryImport branch 2 times, most recently from c0df6dc to f609c55 Compare June 27, 2025 21:10
@Pankraz76 Pankraz76 force-pushed the fix-UnnecessaryImport branch from f609c55 to ea678bd Compare June 27, 2025 21:12
Copy link
Contributor Author

@Pankraz76 Pankraz76 left a 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.

@Pankraz76 Pankraz76 marked this pull request as ready for review June 27, 2025 21:13
Copy link

Documentation Preview

Compared to main:
This changeset changes 24 violations,
introduces 1 new violations, 0 new errors and 0 new configuration errors,
removes 697 violations, 0 errors and 0 configuration errors.

Regression Tester Report

(comment created at 2025-06-27 21:29:24+00:00 for ea678bd)

@Pankraz76
Copy link
Contributor Author

Pankraz76 commented Jun 28, 2025

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.

Comment on lines +1707 to +1717
<!-- 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>-->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!-- 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>
Copy link
Member

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 package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport
  • This class should have the nested static class Foo which itself has two public static fields fail and warn.
  • 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;
Copy link
Member

@adangel adangel Jul 18, 2025

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;
Copy link
Member

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());
Copy link
Member

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);
Copy link
Member

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;
Copy link
Member

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) {
Copy link
Member

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;
Copy link
Member

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.

Comment on lines +1681 to 1689
import java.time.LocalDate;
public class Foo {
String test(String name) {
return """
Hello %s!
Today is %s
""".formatted(name, LocalDate.now());
}
}
Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[java] UnnecessaryImport with multiple static imports from same class
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy