Skip to content

[Bug]: ImportCleaner - multiple wildcard import statements with same class name in their packages issue #6450

@beijiaxu

Description

@beijiaxu

Describe the bug

While there are >= 2 wildcard imports, and same class name like Foo.java in them. Then ImportCleaner will remove the explicit import statement.
e.g:

project structure:

  • com
    • a
      A.java
    • b
      A.java

class Foo content:

package com;

import com.a.*;
import com.b.*;
import com.a.A;

class Foo {
   A a;
}

Source code you are trying to analyze/transform

Source code for your Spoon processing

launcher.getEnvironment().setNoClasspath(true);
            launcher.getEnvironment().setIgnoreDuplicateDeclarations(true);
            launcher.getEnvironment().setPrettyPrinterCreator(() -> {
                SniperJavaPrettyPrinter printer = new SniperJavaPrettyPrinter(launcher.getEnvironment());
                List<Processor<CtElement>> preprocessors = List.of(
                        //try to import as much types as possible
                        new ForceImportProcessor(),
                        //remove unused imports first. Do not add new imports at time when conflicts are not resolved
                        new ImportCleaner().setCanAddImports(false),
                        //solve conflicts, the current imports are relevant too
                        new ImportConflictDetector(),
                        //compute final imports
                        new ImportCleaner().setImportComparator(new DefaultImportComparator())
                );
                printer.setIgnoreImplicit(false);
                printer.setPreprocessors(preprocessors);
                return printer;
            });

Actual output

package com;

import com.a.*;
import com.b.*;

class Foo{
 A a;
}

Expected output

package com;

import com.a.*;
import com.b.*;
import com.a.A;

class Foo{
 A a;
}

Spoon Version

11.2.1

JVM Version

17

What operating system are you using?

mac 15.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions