Bug 272536 - [quick fix] Quick Fix incorrectly offers to import nested classes when in default package
Summary: [quick fix] Quick Fix incorrectly offers to import nested classes when in def...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.5   Edit
Hardware: All All
: P5 minor with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-16 13:55 EDT by Chris West (Faux) CLA
Modified: 2023-10-03 16:33 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris West (Faux) CLA 2009-04-16 13:55:15 EDT
Build ID: I20090313-0100

Steps To Reproduce:
1. Create a /single file/ with two classes; one with a third class nested, and the second with an (illegal) reference to the nested class:

class A
{
  class B
  {
  }
}

class C
{
  B d; // B cannot be resolved to a type
}


2. Quick fix the compile error.

One of the options is "Import B (A)", which does nothing at all.

Not affected by not being in the default package.

--
java.version=1.6.0_13
java.vendor=Sun Microsystems Inc.
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_GB
Command-line arguments:  -os win32 -ws win32 -arch x86_64
Comment 1 Olivier Thomann CLA 2009-04-16 14:16:35 EDT
Moving to JDT/UI
Comment 2 Eclipse Genie CLA 2019-05-16 14:40:23 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 3 Dani Megert CLA 2019-05-30 10:41:06 EDT
Still a problem but very minor as default packages are discouraged.
Comment 4 Felix Pahl CLA 2021-04-01 17:45:28 EDT
The behaviour has changed in the meantime. The quick fix now, instead of not doing anything, does insert an import statement:

import Test.B;

but this import statement is invalid and results in the error "The import Test cannot be resolved". Section 7.5.1 of the JLS in its current form (SE 16) (https://docs.oracle.com/javase/specs/jls/se16/html/jls-7.html#jls-7.5.1) says:

"The class or interface must be either a member of a named package, or a member of a class or interface whose outermost lexically enclosing class or interface declaration (§8.1.3) is a member of a named package, or a compile-time error occurs."

Incidentally, the JLS has changed several times in this respect since the bug was first reported. (What follows is mostly of historical interest.)

At the time (2009), the JLS for SE 6 (https://docs.oracle.com/javase/specs/jls/se6/html/packages.html#7.5.1) didn't seem to prohibit such an import from an unnamed package.

In the JLS for SE 7, this sentence was added to Section 7.5 (https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5):

"A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package."

However, this seems to have been inconsistent with Section 6.7 (https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.7), which did define the canonical name of types in an unnamed package.

In the JLS for SE 8, this sentence was deleted again, and instead the sentence "The name must be qualified (§6.5.5.2), or a compile-time error occurs." was added in Section 7.5.1 (https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.5.1). But the definition of qualified names in Section 6.5.5.2 seems to cover the present case, so this import statement would have been allowed again.

Now, since SE 9, Section 7.5.1 again prohibits imports from an unnamed package (see above).
Comment 5 Felix Pahl CLA 2021-04-01 17:50:15 EDT
This may or may not be related to bug 321464.
Comment 6 Fred Bricon CLA 2021-10-12 10:15:04 EDT
This bug is not specific to default package. Given this:

package foo.bar;

import java.util.Iterator;

public class Demo implements Iterable<Nested> {
    public class Nested {
    }

    @Override
    public Iterator<Nested> iterator() {
        return null;
    }
}

The Quickfix on <Nested> to "import 'Nested' (foo.bar.Demo)" is a no-op.

Also reported in vscode-java (which consumes JDT via jdt.ls): https://github.com/redhat-developer/vscode-java/issues/2154
Comment 7 Eclipse Genie CLA 2023-10-03 16:33:56 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.