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?
The syntax of the Java ternary operator is as follows: (condition) ? (return if true) : (return if false); You will often see the Java ternary operator symbols ( ? : ) used in texts and tutorials as an abbreviation for the construct.
CTRL+2,L and ALT+CTRL+L can get the job done for you.
The ternary operator (? :) consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With