Skip to content

[java] FP in UnusedImports for ambiguous static on-demand imports #2277

@kris-scheibe

Description

@kris-scheibe

Affects PMD Version: 6.21

Rule: UnusedImport

Description:
When multiple static on-demand imports match the same method in the code, a regular static import must be used to specify the actual method to use.
The rule may flag the static import as unused, even though it is required.

Also, the rule currently behaves differently, depending on the order of the statements.
In the example below, the "valueOf" method is matched to the "Integer.*" import, which leaves the the "Long.valueOf" import unmatched.
If the "valueOf" method is moved to the bottom of the method, all imports are matched and nothing is flagged.

Code Sample demonstrating the issue:

import static java.lang.Integer.*;
import static java.lang.Long.valueOf;
import static java.lang.Long.*;

public class Foo {
    public void foo() {
        // the "valueOf" method is both in Integer.* and Long.*
        // we need an explicit static import to specify one of them, e.g. Long.valueOf
        valueOf("123", 10);

        // covered by Integer.*
        int i = parseInt("123");

        // covered by Long.*
        long l = parseLong("123");
    }
}

Running PMD through: Other

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions