Bug 562275 - Code completion adds wrong generic type parameter in constructor call
Summary: Code completion adds wrong generic type parameter in constructor call
Status: CLOSED DUPLICATE of bug 461809
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.16   Edit
Hardware: PC Mac OS X
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-17 17:37 EDT by Knut Wannheden CLA
Modified: 2020-04-21 12:22 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Knut Wannheden CLA 2020-04-17 17:37:09 EDT
When invoking code completion in an example like the following (caret represented by "<|>"):

	private void foo() {
		Collection<?> c = new ArrayLis<|>;
	}

The result is this:

	private void foo() {
		Collection<?> c = new ArrayList<E>();
	}

This doesn't compile.

With the target language level being Java 7+ I think it would actually make sense to complete with the diamond operator. Alternatively one might here as well use "Object" instead of "E".
Comment 1 Stephan Herrmann CLA 2020-04-17 18:03:17 EDT
I assume this example has been simplified from smth more meaningful? There doesn't seem to be much that you can do with 'c' :)

More to the point: why would you invoke completion in this position at Java 7+? With a little less typing, completion would have offered exactly what you expect.
Comment 2 Knut Wannheden CLA 2020-04-17 18:22:57 EDT
> I assume this example has been simplified from smth more meaningful? There doesn't seem to be much that you can do with 'c' :)

Not that much simplified really. Would also apply to e.g. `Map<String, ?>`.

> With a little less typing, completion would have offered exactly what you expect.

Can you elaborate on this? What would I have to type? If I use code completion after typing "List<?> l = new " it doesn't offer "ArrayList()", which surprises me, because if I type "Map<String, ?> = new " it offers "HashMap()".
Comment 3 Stephan Herrmann CLA 2020-04-18 06:00:42 EDT
(In reply to Knut Wannheden from comment #2)
> > I assume this example has been simplified from smth more meaningful? There doesn't seem to be much that you can do with 'c' :)
> 
> Not that much simplified really. Would also apply to e.g. `Map<String, ?>`.

In both cases you have an empty collection into which you cannot add any element. Am I missing anything?


> > With a little less typing, completion would have offered exactly what you expect.
> 
> Can you elaborate on this? What would I have to type? If I use code
> completion after typing "List<?> l = new " it doesn't offer "ArrayList()",
> which surprises me, because if I type "Map<String, ?> = new " it offers
> "HashMap()".

In both cases, I need to type at least one character of the type name and then the diamond constructor is offered:
  Map<String, ?> = new | <- does not offer diamond
  Map<String, ?> = new H| <- offers "HashMap<>()"

The same for Collection<?> & ArrayList<>().

If you see different, we'll need to do the typical walk through all preference options & plugins installed to find out why you see different results than I do.
Comment 4 Knut Wannheden CLA 2020-04-20 03:04:11 EDT
> In both cases you have an empty collection into which you cannot add any element. Am I missing anything?

Yes, I would only be able to add "null", unless the object is first cast (which I think is a valid and not too uncommon use case). But I think this is still somewhat beside the point. The same problem is also present if I for example substitute "?" with "? super Integer" as in "List<? super Integer>".

> In both cases, I need to type at least one character of the type name and then > the diamond constructor is offered

I only see proposals like "ArrayList()" which, when selected, get expanded to "ArrayList<E>()". I can't get content assist to insert "ArrayList<>()", which is what I would really want.

> If you see different, we'll need to do the typical walk through all preference
> options & plugins installed to find out why you see different results than I do.

I am using Eclipse 2020-03 and do my testing in a Java 11 project (I have checked that the JDK compliance is properly set for this project).
Comment 5 Stephan Herrmann CLA 2020-04-20 18:45:27 EDT
(In reply to Knut Wannheden from comment #4)
> > If you see different, we'll need to do the typical walk through all preference
> > options & plugins installed to find out why you see different results than I do.
> 
> I am using Eclipse 2020-03 and do my testing in a Java 11 project (I have
> checked that the JDK compliance is properly set for this project).

Which package (e.g. "IDE for Java Developers")?

Any additional plugins?

Which options are enabled at Preferences > Java > Editor > Content Assist > Advanced (upper box)?
Comment 6 Knut Wannheden CLA 2020-04-21 04:12:16 EDT
> Which package (e.g. "IDE for Java Developers")?

Yes, IDE for Java Developers.

> Any additional plugins?

None.

> Which options are enabled at Preferences > Java > Editor > Content Assist > Advanced (upper box)?

Standard settings with "Java Proposals (Task-Focused)", "SWT Template Proposals", and "Template Proposals".

Changing from the default "Java Proposals (Task-Focused)" to plain "Java Proposals" (or alternatively to "Java Non-Type Proposals") resolved the issue. So it seems like the "Java Proposals (Task-Focused)" is broken. (I even wonder how many people use Mylyn these days and if it should be part of this distro, but that's a separate topic.)

Further investigation shows that somehow the defaults for content assist settings are behaving very oddly. When I create a new workspace and check the preferences I can see the following being selected: "Java Proposals", "Java Proposals (Task-Focused)", "SWT Template Proposals", and "Template Proposals".

But as soon as I create a Java project, create a Java class and start using content assist in there (Ctrl+Space), the preference has changed to show only "Java Proposals (Task-Focused)", "SWT Template Proposals", and "Template Proposals" (i.e. no more "Java Proposals").

If I now in the Preferences dialog click on "Restore Defaults" the selection changes to "Java Proposals", "SWT Template Proposals", and "Template Proposals"! Do "Java Proposals" and "Java Proposals (Task-Focused)" possibly use the same ID?

In addition to "Java Proposals (Task-Focused)" not inserting the diamond operator it also seems like it interferes with the standard "Java Proposals" somehow.
Comment 7 Stephan Herrmann CLA 2020-04-21 05:13:20 EDT
(In reply to Knut Wannheden from comment #6)
> > Which package (e.g. "IDE for Java Developers")?
> 
> Yes, IDE for Java Developers.
> 
> > Any additional plugins?
> 
> None.
> 
> > Which options are enabled at Preferences > Java > Editor > Content Assist > Advanced (upper box)?
> 
> Standard settings with "Java Proposals (Task-Focused)", "SWT Template
> Proposals", and "Template Proposals".
> 
> Changing from the default "Java Proposals (Task-Focused)" to plain "Java
> Proposals" (or alternatively to "Java Non-Type Proposals") resolved the
> issue. So it seems like the "Java Proposals (Task-Focused)" is broken. (I
> even wonder how many people use Mylyn these days and if it should be part of
> this distro, but that's a separate topic.)

This is bug 461809 (where we currently think that a JDT bug is exposed by enabling Mylyn).
 
> Further investigation shows that somehow the defaults for content assist
> settings are behaving very oddly. [...]

I can't say much about these. Could be a bug in JDT/UI or in Mylyn.

Once completion is fixed (by workaround or by code fix) for
    Map<String, ?> = new H| <- offers "HashMap<>()"
do you still think the original request for completion between < and > is necessary?
Comment 8 Knut Wannheden CLA 2020-04-21 08:12:56 EDT
> This is bug 461809 (where we currently think that a JDT bug is exposed
> by enabling Mylyn).

Yes, looks like it.

> Once completion is fixed (by workaround or by code fix) for
>     Map<String, ?> = new H| <- offers "HashMap<>()"
> do you still think the original request for completion between < and > is necessary?

No, I think that corner case can be ignored, as the diamond operator is nicer anyway.

I assume 461809 will fix the issue so that the diamond operator completion works out of the box, without any custom settings required.
Comment 9 Stephan Herrmann CLA 2020-04-21 12:22:09 EDT
(In reply to Knut Wannheden from comment #8)
> > This is bug 461809 (where we currently think that a JDT bug is exposed
> > by enabling Mylyn).
> 
> Yes, looks like it.
> 
> > Once completion is fixed (by workaround or by code fix) for
> >     Map<String, ?> = new H| <- offers "HashMap<>()"
> > do you still think the original request for completion between < and > is necessary?
> 
> No, I think that corner case can be ignored, as the diamond operator is
> nicer anyway.
> 
> I assume 461809 will fix the issue so that the diamond operator completion
> works out of the box, without any custom settings required.

Thanks, closing.

*** This bug has been marked as a duplicate of bug 461809 ***