This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 243667 - Organize Imports hint fails within the same package
Summary: Organize Imports hint fails within the same package
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: All All
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-08 17:50 UTC by fsttesla
Modified: 2014-08-10 01:51 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description fsttesla 2014-04-08 17:50:27 UTC
Consider
1. a package names pkg with:
- a class named A
- a class named B that contains a star import for anotherpkg.*
2. a package anotherpkg with a class named A

The following cases do not depend on any possible personal settings regarding the imports formatting settings.

If B uses pkg.A (referenced by simple name), an Organize Imports hints appears; the hint adds an unnecessary import for pkg.A.
If B uses anotherpkg.A (again referenced by simple name, with an explicit import for it), an Organize Imports appears; the hint removes the (correct) import for anotherpkg.A, breaking the code.
Comment 1 Jiri Prox 2014-04-14 08:04:59 UTC
Can you please attach sample code?
I've tried to create it according to your description but it my case the organize import hint works fine. Probably the code was not same as yours.

Please also attach build number and version.

Thanks
Comment 2 fsttesla 2014-04-15 08:16:47 UTC
NB 8.0 Build 201403101706

Maybe this bug (partly) depends on the formatting settings (Options > Editor > Formatting > Category: Imports), so I write them here for easier reproducibility:
- Use Single Class Imports
- Class Count To Use Star Import: 3
- Members Count To Use Static Star Import: 3
- Separate Static Imports
All other radios/checkboxes are not selected.

### File pkg/Scanner.java ###
package pkg;
public class Scanner {
    public void doSomething() { }
}

### File pkg/User.java (1st case) ###
package pkg;
import java.util.*;
public class User {
    // The next 3 lines serve to keep the star import
    Date date;
    Formatter formatter;
    Random random;
    public void method(Scanner scanner) {
        scanner.doSomething();
    }
}

### File pkg/User.java (2nd case) ###
package pkg;
import java.util.*;
import java.util.Scanner;
public class User {
    // The next 3 lines serve to keep the star import
    Date date;
    Formatter formatter;
    Random random;
    public void method(Scanner scanner) {
        scanner.hasNext();
    }
}

Renamings from the initial bug report: anotherpkg -> java.util, A -> Scanner, B -> User.

In the 1st case, the simple name "Scanner" refers to pkg.Scanner and I receive an Organize Imports hint which adds a useless "import pkg.Scanner;". Of course, a Remove import from the same package hint appears afterwards.
In the 2nd case, the simple name "Scanner" refers to java.util.Scanner (by virtue of the explicit import); if I apply the Organize Imports hint, such explicit import is removed and the code gets broken.
Comment 3 Jiri Prox 2014-04-16 08:46:37 UTC
reproducible

thanks for code sample
Comment 4 Svata Dedic 2014-08-07 09:19:18 UTC
Fixed in jet-main#c958f8142610
Comment 5 Quality Engineering 2014-08-10 01:51:25 UTC
Integrated into 'main-silver', will be available in build *201408100001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/c958f8142610
User: Svata Dedic <sdedic@netbeans.org>
Log: #243667: fixed precedence of star/named/same-package imports