Quick assists perform local code transformations. They are invoked on a selection or a single cursor in the Java editor and
use the same shortcut as quick fixes (Ctrl+1), but quick assist are usually hidden when an error is around.
To show them even with errors present on the same line, press Ctrl+1 a second time.
A selection of quick assists can be assigned to a direct shortcut. By default, these are:
Assign more shortcuts or change the default shortcuts on the
General > Keys preference page (in the 'Source' category).
A quick assist light bulb can be turned on on the
Java > Editor preference page.
Name | Code example | Invocation location | ||
---|---|---|---|---|
Inverse if statement | if (x) a(); else b(); |
> | if (!x) b(); else a(); |
On 'if' statements with 'else' block |
Inverse boolean expression | a &&
!b |
> | !a || b |
On a boolean expression |
Remove extra parentheses | if ((a == b)
&& (c != d) {} |
> | if (a == b && c !=
d) {} |
On selected expressions |
Add paranoidal parentheses | if (a == b
&& c != d) {} |
> | if ((a == b) && (c != d) |
On selected expressions |
Join nested if statements | if (a) { if
(b) {} } |
> | if (a && b) {} |
On a nested if statement |
Swap nested if statements | if (a) { if
(b) {} } |
> | if (b) { if (a) {} } |
On a nested if statement |
Split if statement with and'ed expression | if (a
&& b) {} |
> | if (a) { if (b) {} } |
On an and'ed expression in a 'if' |
Split if statement with or'd expression | if (a || b)
x(); |
> | if (a) x(); if (b) x(); |
On an or'd expression in a 'if' |
Inverse conditional expression | x ? b : c |
> | !x ? c : b |
On a conditional expression |
Pull negation up | b && c |
> | !(!b || !c) |
On a boolean expression |
Push negation down | !(b
&& c) |
> | !b || !c |
On a negated boolean expression |
If-else assignment to conditional expression | if (a) x= 1;
else x= 2; |
> | x= a ? 1 : 2; |
On an 'if' statement |
If-else return to conditional expression | if (a) return
1; |
> | return a ? 1 : 2; |
On an 'if' statement |
Conditional expression assignment to If-else | x= a ? 1 : 2; |
> | if (a) x= 1; else x= 2; |
On a conditional expression |
Conditional expression return to If-else | return
a ? 1 : 2; |
> | if (a) return 1; else return 2; |
On a conditional expression |
Switch to If-else | switch (kind) {
|
> | if (kind == 1) {
|
On a statement |
Add missing case statements on enums | switch (e){ |
> | switch (e){ |
On a switch statement |
Exchange operands | a + b |
> | b + a |
On an infix operation |
Cast and assign | if (obj
instanceof Vector) { |
> | if (obj instanceof Vector) { } |
on a instanceof expression in an 'if' or 'while' statement |
Pick out string | "abcdefgh" |
> | "abc" + "de" + "fgh" |
select a part of a string literal |
Convert string concatenation to StringBuilder (J2SE 5.0) or StringBuffer | "Hello " + name |
> | StringBuilder builder= new StringBuilder(); |
select a string literal |
Convert string concatenation to MessageFormat | "Hello " + name |
> | MessageFormat.format("Hello {0}", name); |
select a string literal |
Split variable | int i= 0;
|
> | int i; i= 0; |
On a variable with initialization |
Join variable | int i; i= 0; |
> | int i= 0 |
On a variable without initialization |
Assign to variable | foo()
|
> | X x= foo(); |
On an expression statement |
Extract to local | foo(getColor()); |
> | Color color= getColor();
|
On an expression |
Assign parameter to field | public A(int
color) {}
|
> | Color fColor; |
On a parameter |
Add finally block | try { |
> | try { |
On a try/catch statement |
Add else block | if (a) b();
|
> | if (a) b(); else { } |
On a if statement |
Replace statement with block | if (a) b();
|
> | if (a) { b(); } |
On a if statement |
Invert equals | a.equals(b)
|
> | b.equals(a) |
On a invocation of 'equals' |
Array initializer to Array creation | int[]
i= { 1, 2, 3 } |
> | int[] i= new int[] { 1, 2, 3 } |
On an array initializer |
Convert to 'enhanced for loop' (J2SE 1.5) | for (Iterator
i= c.iterator();i.hasNext();) { |
> | for (x : c) { |
On a for loop |
Create method in super class | On a method declaration | |||
Unwrap blocks | { a() } |
> | a() |
On blocks, if/while/for statements |
Rename in file | On identifiers | |||
Rename in workspace | On identifiers | |||
Extract to local variable |
a= b*8;
|
> | int x= b*8;
|
On expressions |
Extract to constant |
a= 8;
|
> | final static int CONST= 8;
|
On expressions |
Extract method |
int x= p * 5;
|
> | int x= getFoo(p);
|
On expressions and statements |
Inline local variable |
int a= 8, b= a;
|
> | int b= 8;
|
On local variables |
Convert local variable to field |
void foo() { int a= 8; }
|
> | int a= 8; void foo() {}
|
On local variables |
Convert anonymous to nested class |
new Runnable() { };
|
> | class RunnableImplementation implements Runnable { }
|
On anonymous classes |
Replace with getter and setter (Encapsulate Field) | p.x;
|
> | p.getX();
|
On fields |
Java Editor
Quick Fix and Quick Assist