Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse style code complete for generic types - Use Diamond [duplicate]

I just upgraded to Luna (from Kepler) and I noticed some little (yet annoying) problem:
The auto completion lacks the possibility to generate diamonds "<>" for already specified generics in variable initialisation. For Example, when I type:

Set<String> mySet = new HashSet 

I would expect, that it gets auto-completed to

Set<String> mySet = new HashSet<>();

rather than

Set<String> mySet = new HashSet<String>();

I am using JDK 1.7 and the compliance level is set to 1.7 also.

Its also very strange that when I hit STRG+Space multiple times during auto-completing till it reaches java-autocompletion again and then hit Enter it delivers the expected diamonds.

This looks very much like a bug to me.
Is this bug "known" and is there any workaround for this issue? Could not find anything related to this topic.

like image 390
Chris_D_Turk Avatar asked Jul 27 '14 18:07

Chris_D_Turk


3 Answers

You have most likely installed the Code Recommenders plug-in. Unfortunately this plug-in is affected by bug #435597 which breaks this functionality.

This bug appears to occur only if you have certain proposal kinds enabled in the “'default' content assist list” (Eclipse Preferences > Java > Editor > Content Assist > Advanced). For example I could get back the correct behavior by disabling the following proposal kinds:

  • Basic Proposals
  • JPA Proposals
  • Template Proposals

(the last one is likely the most annoying if you are using code templates…)

When you are cycling with ctrl+space, you are actually reaching the “Java Proposals” (instead of the “Intelligent Java Proposals (Code Recommenders)”), which is not affected by this bug.

This bug is scheduled for release 2.2.0 in June 2015. The best you can do for the moment is:

  • Play with the proposals to check which ones trigger the bug for you (maybe take a screenshot of your current setup before doing that);
  • Vote for bug #435597 and bug #435660 to get them fixed;
  • Propose patches for fixing those bugs :-)
like image 75
Didier L Avatar answered Sep 21 '22 09:09

Didier L


For anyone still experiencing this with the latest Eclipse version:

Open "Window" > "Preferences" > "Java" > "Editor" > "Content Assist" > "Advanced". In the opened dialog under "Default Proposal Kinds":

  • Check "Java Proposals"
  • Uncheck "Java Proposals (Task-Focused)"

Proposal kind selection

like image 45
Marcono1234 Avatar answered Sep 21 '22 09:09

Marcono1234


Set is an interface rather than an implementation so the IDE can only assume that you want an anonymous inner type which always requires the generic type parameter. You could do

Set<String> mySet = new HashSet + CTRL + SPACE
like image 35
Reimeus Avatar answered Sep 21 '22 09:09

Reimeus