Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 318437

Summary: Organize Imports ignores Number of Imports needed for .*
Product: [Eclipse Project] JDT Reporter: ruland
Component: CoreAssignee: John Glassmyer <eclipse>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, daniel_megert, eclipse.sprigogin, eclipse, jarthana, markus.kell.r, Olivier_Thomann, shankhba, wangchaobin
Version: 3.7   
Target Milestone: 4.5 M6   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 430303    

Description ruland CLA 2010-06-30 07:01:11 EDT
Build Identifier: 20100617-1415

The Number of Imports needed for .* is ignored when organizing imports via Ctrl+Shift+O.

The Number of Imports is configured to 2. My class has 3 imports of the same package, which could be replaced by .*. When the imports are organized, nothing is happenening.

If I replace the 3 imports manually with .* and then organize the imports via Ctrl+Shift+O again, .* is replaced with the 3 imports again.

Reproducible: Always

Steps to Reproduce:
1. Configure Number of Imports to 2 (Preferences/Java/Code Style/Organize Imports/Number of Imports needed for .*)
2. Create a Class with more than 2 imports of the same package.
3. Organize the imports via Ctrl+Shift+P.
4. Nothing is happening, although the imports should be replaced with .*.
Comment 1 Ayushman Jain CLA 2010-06-30 07:44:40 EDT
I couldn't reproduce this using the following case and preferences set as per comment 0:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class Test {
	ArrayList a;
	Arrays b;
	LinkedList l;
}

CTRL-SHIFT-O leads to import java.util.*

Can you please provide a testcase?
Comment 2 Jay Arthanareeswaran CLA 2010-06-30 07:55:08 EDT
(In reply to comment #0)
> Build Identifier: 20100617-1415
> 3. Organize the imports via Ctrl+Shift+P.
> 4. Nothing is happening, although the imports should be replaced with .*.

I don't see the bug either. And I am assuming "Ctrl+Shift+P" is not a typo here.
Comment 3 ruland CLA 2010-06-30 08:07:03 EDT
> Can you please provide a testcase?

I am sorry, my bug description was not good enough. Here is a testcase with two classes in two different packages to reproduce:

package MyPackage;

public class Constants
{
    public static final class SomeConstants
    {
        public static final String CONSTANT_A = "A";
    }

    public static final class OtherConstants
    {
        public static final String CONSTANT_B = "B";
    }
}

package OtherPackage;

import MyPackage.Constants.OtherConstants;
import MyPackage.Constants.SomeConstants;

public class Test 
{
    String str = SomeConstants.CONSTANT_A + OtherConstants.CONSTANT_B;
}

Ctrl+Shift+O does not lead to the import "MyPackage.Constants.*" as it did with Eclipse 3.5.
Comment 4 Olivier Thomann CLA 2010-06-30 08:16:31 EDT
I'll take a look.
Comment 5 Olivier Thomann CLA 2010-06-30 13:27:28 EDT
The problem comes from the fact that the import groups are created with the package name as the key. In this case, we should create an import group using a toplevel name.
I don't know yet how to fix it without breaking other cases.

Markus, is it intended to work for top level type names?

In 3.5, it was working because it never tried to find the package name for the import group key. It was just using the type name - the last segment.
Comment 6 Olivier Thomann CLA 2010-06-30 13:34:42 EDT
The current code was added in 3.6 to be able to handle java.util.Map.Entry and adds this kind of import inside the same group as java.util.Map.
This is why it retrieves the package as the qualifier for an import and not the type name.

The only way I see to fix this would be to add some logic to replace some imports with a '*' import within the group itself.

Markus, any thoughts on this ?
Comment 7 Markus Keller CLA 2010-06-30 15:44:49 EDT
We have the same problem if the import group is "MyPackage.Constants".

And if you add
	Constants c;
, then you get an "import MyPackage.*;", although that import only stands for the single type MyPackage.Constants.

I guess the problem is that you use the same data structure PackageEntry for 2 different purposes:
1) grouping of imports from the same package
2) combining multiple imports to an on-demand import

Before bug 194358, these used to be the same, but now a PackageEntry no longer contains only entries with the same qualifier. I guess you'll have to separate these concerns, but I don't know that code well enough to be able to just pull out a clever trick out of my sleeve.
Comment 8 ruland CLA 2010-07-05 08:50:19 EDT
Any news on this?
Comment 9 Markus Keller CLA 2010-07-05 09:50:11 EDT
> Any news on this?

You would have heard it here.

To speed things up, you can provide a patch that fixes the issue. Code is in ImportRewriteAnalyzer (org.eclipse.jdt.core) and tests are in ImportRewriteTest (org.eclipse.jdt.core.tests.model). If you want to work on this, please tell it here, so that we don't waste resources working on the same problem in parallel.
Comment 10 John Glassmyer CLA 2014-06-06 05:36:16 EDT
I have nearly finished a fix for this.
Comment 11 Markus Keller CLA 2015-03-05 14:54:56 EST
Fixed via bug 430303.
Comment 12 shankha banerjee CLA 2015-03-18 04:05:44 EDT
Verified for 4.5 M6 with build I20150316-2000.