Bug 99511 - [organize import] Add option to prevent addition of imports for nested types
Summary: [organize import] Add option to prevent addition of imports for nested types
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows 2000
: P5 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 286632 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-06-11 11:23 EDT by Will Hains CLA
Modified: 2009-08-17 09:32 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Will Hains CLA 2005-06-11 11:23:36 EDT
Probably best explained using an example:

Let's say you have a class with a nested type, like this:

[com/blah/util/Foo.java]
package com.blah.util;

import java.util.*;

public class Foo
{
    private final List<Bar> _bars = new ArrayList<Bar>();

    public static class Bar
    {
        public final String id;

        private Bar(String id)
        {
            this.id = id;
        }
    }

    public void addBar(String id)
    {
        _bars.add(new Bar(id));
    }
    
    private static final Random _Rnd = new Random();
    
    public Bar getRandom()
    {
        return _bars.get(Math.abs(_Rnd.nextInt()) % _bars.size());
    }
}

...and you want to use it in another class, so you write this code:

[com/blah/myapp/FooClient.java]
package com.blah.myapp;

import com.blah.util.*;

public class FooClient
{
    private final Foo _f = new Foo();
    
    public void init()
    {
        _f.addBar("A");
        _f.addBar("B");
        _f.addBar("C");
    }
    
    public void start() throws InterruptedException
    {
        for(;;)
        {
            System.out.println("Foo.Bar.id=" + _f.getRandom().id);
            Thread.sleep(1000);
        }
    }
}

Now for some reason, you want to have the result of "_f.getRandom()" stored in 
a local variable. So you select "_f.getRandom()" and use the refactoring 
command "Extract Local Variable". Eclipse adds an import and a line of code, 
like this:

import com.blah.util.*;
import com.blah.util.Foo.*;
...
            final Bar randomFooBar = _f.getRandom();
            System.out.println("Foo.Bar.id=" + randomFooBar.id);

Exactly the same thing happens for other operations, too - 
eg. "Override/Implement Methods", and when you create a new class that 
overrides/implements an abstract class/interface.

I for one would prefer it not to add that import, opting instead to refer 
to "Bar" by its full name, "Foo.Bar", like this:

import com.blah.util.*;
...
            final Foo.Bar randomFooBar = _f.getRandom();
            System.out.println("Foo.Bar.id=" + randomFooBar.id);

I think it would be nice to have a checkbox in the "Organize Imports" 
preference page that says something like "Import packages only". (Might have to 
rethink the wording if there will automatically generated static imports, which 
I would still like to have.)

Also, it would be nice if the automatic import feature of code completion took 
care of this as well. For example, let's say that instead of using Eclipse's 
refactoring support to do the above, you start typing the "final..." variable 
declaration/assignment line manually. Right now if you type "final Bar" and 
press Ctrl+Space you get nothing (well, not Foo's nested class "Bar" anyway). 
It would be very useful if the code completion list included "Foo.Bar", and 
when selected, inserted either the full "Foo.Bar", or just "Bar" and an import, 
depending on the preference described above.
Comment 1 Olivier Thomann CLA 2005-06-11 12:50:49 EDT
Move to JDT/UI
Comment 2 Dirk Baeumer CLA 2005-06-12 06:23:34 EDT
Not important for 3.1. Postponing...
Comment 3 Markus Keller CLA 2009-08-17 09:30:49 EDT
*** Bug 286632 has been marked as a duplicate of this bug. ***
Comment 4 Markus Keller CLA 2009-08-17 09:32:16 EDT
Reopening, since LATER is deprecated.