7

In Eclipse (3.4+ upwards), I was searching for shortcut which converts the if {} else {} condition block to the java ternary operator (or the ?: operator).

How can I do this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Narayan
  • 6,031
  • 3
  • 41
  • 45

3 Answers3

5

There is no such shortcut. Refactoring statements into expressions isn't a trivial process, and isn't always possible to begin with. The process would be too complicated to be automated.

That said, Ctrl + Shift + L will list ALL Eclipse shortcuts.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • thanks for reply,but wouldn't it be possible if the assignment is done only once? by eclipse-template configuration or something else? – Narayan Aug 25 '10 at 13:39
  • @Narayan: check this out: http://ideone.com/66SrI ; the rules to do this automatic refactoring would have to be very complicated. – polygenelubricants Aug 25 '10 at 13:55
  • 1
    ok, i take your point.; but every time i need to check something as simple like this http://ideone.com/p0GC1 , i feel the need for it, like Select the expression and press that magical key =] – Narayan Aug 26 '10 at 03:49
  • @Narayan: as I said in the comment to the question, I'd be happy to see this implemented in Eclipse too. It can be done of course, I just don't think it's that easy. – polygenelubricants Aug 26 '10 at 03:58
4

Excuse me if this is not relevant anymore.

I have written an eclipse plug-in that does exactly this:

http://marketplace.eclipse.org/content/spartan-refactoring

Feel free to report back if you have problems with installation or using this plug-in.

Edit: Re-written version by other developers is available at https://marketplace.eclipse.org/content/spartan-refactoring-0

Artium
  • 5,147
  • 8
  • 39
  • 60
  • @Narayan I assume you know how to install addons through update sites. The update site for the plugin: http://update.nihamkin.com/spartan Once installed, you will have new submenu under the "Refactor" menu called "Spartanization". If you have selected text, the refactoring will be performed on the selected text. If you have no selected text, the refactoring will be performed on the whole project. – Artium Aug 18 '13 at 18:36
  • I installed it, now when i goto the Refactor menu -> Spartanization-> Convert Condition to ternary, it donot do the job, it says `The Refactoring does not change any source code. the code is the same as that present in the screenshot tab, what could be wrong? – Narayan Aug 19 '13 at 01:20
  • @Narayan 1) What eclipse version do you have? 2) What happens if you select all the text in the file and then try to do the refactoring? – Artium Aug 19 '13 at 19:40
  • Version: Helios Service Release 1, i did select them all and try the Refactoring as mentioned! – Narayan Aug 19 '13 at 20:29
  • @Narayan Sorry, It seems that I can not reproduce your problem. It works fine with the helios eclipse I have downloaded. i.imgur.com/zuMtVKL.png – Artium Aug 20 '13 at 19:37
  • @Artium, I installed the plugin (Eclipse Mars.sr1) and it will find matches when I select all the java code in a file, and it will find for the entire project if I select nothing.. but when I select the following block, it says "The refactoring does not change any source code" if (color == null) { color = myObject.getColor(); } I expected it to respond with... color = (color == null) ? myObject.getColor() : color; – rogodeter Oct 13 '15 at 20:20
  • @rogodeter Indeed this is a case that the plugin does not deal with. The problem here is the lack of the else statement. I might fix it in the weekend, will post an update here. – Artium Oct 14 '15 at 19:53
  • @rogodeter A new version is available re written by other developers. You might want to try. It is located in a different update site: https://marketplace.eclipse.org/content/spartan-refactoring-0 – Artium Dec 12 '15 at 21:10
  • 1
    @narayan A new version is available re written by other developers. You might want to try. It is located in a different update site: https://marketplace.eclipse.org/content/spartan-refactoring-0 – Artium Dec 12 '15 at 21:10
  • @Artium Thanks! i will check it out! – Narayan Dec 14 '15 at 01:09
2

Well, you could add a Template to Eclipse like this:

${condition:field(boolean)}? ${positive:field(void)}: ${negative:field(void)};

Name it something like tern and you can have it auto-create the ternary operator for you.

(Not sure if this is what you want)

jjnguy
  • 136,852
  • 53
  • 295
  • 323