0

In Spring Tool Suite

(Version: 3.7.0.RELEASE
Build Id: 201506290649
Platform: Eclipse Luna SR2 (4.4.2))

Is there a shortcut to go from something like:

Function<String, Integer> func = str -> Integer.valueOf(str);

to something like this:

Function<String, Integer> func =  Integer::valueOf;

(and backwards) via a keyboard shortcut?

I am aware of Ctrl+1 key combo, but it doesn't seem to offer the method reference conversion, hence this inquiry:

enter image description here

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82

2 Answers2

2

Select the lambda expression "str -> Integer.valueOf(str)" and press Ctrl-1. The first entry in the quick-fix list should be something like "convert to method reference".

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
Martin Lippert
  • 5,988
  • 1
  • 15
  • 18
  • Ctrl-1 doesn't seem to work, see the screen shot above. – Simeon Leyzerzon Sep 09 '16 at 10:16
  • perhaps it is [eclipse bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=430656), though it is said it is fixed with luna 4.2.2 that you are using. also check this: http://stackoverflow.com/questions/24453083/eclipse-luna-r-no-content-assist-for-java-8-method-references – bartac Sep 09 '16 at 15:17
  • 1
    Stephan is, of course, right. You need at least an Eclipse 4.5-based development environment to use this content-assist. Sorry that I forgot to mention that. – Martin Lippert Sep 12 '16 at 07:23
2

First, you need Eclipse 4.5 or greater, because that's when this particular assist was first published.

Next you should avoid the "unused" warning on the variable, because otherwise content assist will jump to the nearest warning / error rather than proposing a change unrelated to any problem in the current code (this is not strictly necessary, but makes the next step easier).

Finally, you need to position the cursor on the ->, because that's the only piece of syntax definitely identifying the lambda.

convert to method reference assist

See also the first item in the 4.5 "New-&-Noteworthy", Section on JDT.

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
  • Thanks, I'll attempt 4.5 - what would be the cleanest/more appropriate way to upgrade the STS - can I simply upgrade all the suggested plugins or will I be better off installing a brand new version of STS and changing my shortcuts? – Simeon Leyzerzon Sep 09 '16 at 22:25
  • 1
    For upgrading STS please refer to this question: http://stackoverflow.com/q/29490233/4611488 (while you are at it: 4.6 has been released this year, so you may actually want to skip 4.5). – Stephan Herrmann Sep 10 '16 at 16:21
  • I didn't forget, simply didn't get to try re-installation yet, please stay tuned. – Simeon Leyzerzon Dec 11 '16 at 15:49
  • I have Neon 3 (4.6.3) - i followed the advice here, but the option still does not show up. – IARI May 12 '17 at 01:58
  • @Stephan Herrmann: Thank you. Seems to be working on STS ver. 4.0.0.M7. As for verifying the procedure to upgrade STS, I ended up just installing the brand new version from scratch so I will not attest to the steps there. Thanks again. – Simeon Leyzerzon Feb 06 '18 at 00:15