views:

77

answers:

2

Hello friends,

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 ]

can you guide me, how to do i do this ?

Thank you!

+4  A: 

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

polygenelubricants
@poly thanks for reply,but wouldn't it be possible if the assignment is done only once? by eclipse-template configuration or something else?
Narayan
@Narayan: check this out: http://ideone.com/66SrI ; the rules to do this automatic refactoring would have to be very complicated.
polygenelubricants
@polygenelubricants: 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
+1 for taking effort to write the code in ideone :)
Narayan
@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
+2  A: 

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
I think they are looking to highlight an existing if/else block and have it automagically converted to a ternary statement
Rulmeq
@Rul, I know. But this is the closest that is easily possible.
jjnguy
Well I gave it +1 because I've added it to my copy of eclipse :)
Rulmeq
@Rul, thanks! I added it to mine too.
jjnguy
@jinguy, +1 for the template but the template outputs something like `condition? positive: negative;`, isnt much helpful =[
Narayan
@Nar, yeah. Sorry.
jjnguy