- Introduction
- Assign to variable
- Change modifiers to final
- Convert anonymous to nested class
- Convert to anonymous class creation
- Convert to enhanced for loop
- Convert to lambda expression
- Convert to static import
- Extract refactorings
- Inline refactorings
- Introduce Parameter
- Invert boolean
- Move
- Rename
- Type change
The goal of the Java program refactoring is to make system-wide code changes without affecting the behavior of the program. The Java Language Support for VSCODE_ENV_APPNAME_PLACEHOLDER provides assistance in easily refactoring code.
Refactoring commands are available from the context menu of the editor. Select the element you want to refactor, right-click to open the context menu and choose Refactor...:
Then you will see all the available refactoring options.
Assigns an expression to a local variable or a field.
Arrays.asList("apple", "lemon", "banana");List<String> fruits = Arrays.asList("apple", "lemon", "banana");Adds final modifier to all the variables and parameters in the current source file.
public class Clazz {
public void method(int value) {
boolean notValid = value > 5;
if (notValid) {
// do something
}
}
}public class Clazz {
public void method(final int value) {
final boolean notValid = value > 5;
if (notValid) {
// do something
}
}
}Converts an anonymous inner class to a member class.
Let's convert the anonymous class Interface(){...} to a member of the class Clazz.
public class Clazz {
public Interface method() {
final boolean isValid = true;
return new Interface() {
public boolean isValid() {
return isValid;
}
};
}
}public class Clazz {
private final class MyInterface extends Interface {
private final boolean isValid;
private MyInterface(boolean isValid) {
this.isValid = isValid;
}
public boolean isValid() {
return isValid;
}
}
public Interface method() {
final boolean isValid = true;
return new MyInterface(isValid);
}
}Converts lambda expression to anonymous class creation.
The variable runnable is assigned with a lambda expression. Let's convert it to an anonymous class creation.
public void method() {
Runnable runnable = () -> {
// do something
};
}public void method() {
Runnable runnable = new Runnable() {
@Override
public void run() {
// do something
}
};
}Also see: Convert to lambda expression
Converts the simple for loop to for-each style.
public void order(String[] books) {
for (int i = 0; i < books.length; i++) {
// do something
}
}public void order(String[] books) {
for (String book : books) {
// do something
}
}Converts an anonymous class creation to the lambda expression.
Let's convert the anonymous class Runnable(){...} to a lamda expression.
public void method() {
Runnable runnable = new Runnable(){
@Override
public void run() {
// do something
}
};
}public void method() {
Runnable runnable = () -> {
// do something
};
}Also see: Convert to anonymous class creation
Converts the field or method to static import.
Let's transform the Assert.assertEquals() invocation to a static import.
import org.junit.Assert;
...
public void test() {
Assert.assertEquals(expected, actual);
}import static org.junit.Assert.assertEquals;
...
public void test() {
assertEquals(expected, actual);
}Creates a static final field from the selected expression and substitutes a field reference, then rewrites other places where the same expression occurs.
Let's extract the value of π: 3.14 to a constant.
public double getArea(double r) {
return 3.14 * r * r;
}private static final double PI = 3.14;
public double getArea(double r) {
return PI * r * r;
}Also see: Inline constant
Declares a new field and initializes it with the selected expression. The original expression is replaced with the usage of the field.
Let's extract the variable area to a field of the class Square.
class Square {
public void calculateArea() {
int height = 1;
int width = 2;
int area = height * width;
}
}class Square {
private int area;
public void calculateArea() {
int height = 1;
int width = 2;
area = height * width;
}
}Creates a new method containing the statements or expressions currently selected and replaces the selection with a reference to the new method. This feature is useful for cleaning up lengthy, cluttered, or overly-complicated methods.
Let's extract the expression height * width to a new method.
public void method() {
int height = 1;
int width = 2;
int area = height * width;
}public void method() {
int height = 1;
int width = 2;
int area = getArea(height, width);
}
private int getArea(int height, int width) {
return height * width;
}Also see: Inline method
Creates a new variable assigned to the expression currently selected and replaces the selection with a reference to the new variable.
Let's extract the expression platform.equalsIgnoreCase("MAC") to a new variable.
public void method() {
if (platform.equalsIgnoreCase("MAC")) {
// do something
}
}public void method() {
boolean isMac = platform.equalsIgnoreCase("MAC");
if (isMac) {
// do something
}
}Also see: Inline local variable
Replaces a constant reference with its defined value.
Let's replace the constant PI to its defined value: 3.14.
private static final double PI = 3.14;
public double getArea(double r) {
return PI * r * r;
}private static final double PI = 3.14;
public double getArea(double r) {
return 3.14 * r * r;
}Also see: Extract to constant
Replaces redundant variable usage with its initializer.
Let's replace the variable isMac directly to the boolean expression.
public void method() {
boolean isMac = platform.equalsIgnoreCase("MAC");
if (isMac) {
// do something
}
}public void method() {
if (platform.equalsIgnoreCase("MAC")) {
// do something
}
}Also see: Extract to local variable
Replaces calls to the method with the method’s body.
Let's replace the method getArea(int height, int width) directly to the expression height * width.
public void method() {
int height = 1;
int width = 2;
int area = getArea(height, width);
}
private int getArea(int height, int width) {
return height * width;
}public void method() {
int height = 1;
int width = 2;
int area = height * width;
}Also see: Extract to method
Replaces an expression with a reference to a new method parameter, and updates all callers of the method to pass the expression as the value of that parameter.
Let's introduce a new parameter for the method public void addUser().
public void buildRegistry() {
addUser();
}
public void addUser() {
fUsers.add("Administrator");
}public void buildRegistry() {
addUser("Administrator");
}
public void addUser(String name) {
fUsers.add(name);
}Inverts the boolean expression in the conditions.
Let's invert the boolean expression in the if statement.
public void method(int value) {
if (value > 5 && value < 15) {
// do something
}
}public void method(int value) {
if (value <= 5 || value >= 15) {
// do something
}
}Inverts the local boolean variable.
Let's invert the variable valid.
public void method(int value) {
boolean valid = value > 5 && value < 15;
}public void method(int value) {
boolean notValid = value <= 5 || value >= 15;
}Moves the selected elements and corrects all references to the elements (also in other files). Available actions are:
- Move class to another package
- Move static or instance method to another class
- Move inner class to a new file
Let's move the static method print() from class Office to class Printer.
public class Office {
public static void main(String[] args) {
print();
}
public static void print() {
System.out.println("This is printer");
}
static class Printer { }
}public class Office {
public static void main(String[] args) {
Printer.print();
}
static class Printer {
public static void print() {
System.out.println("This is printer");
}
}
}Default shortcut: F2
Renames the selected element and corrects all references to the elements (also in other files).
Let's rename the class Foo to Bar
public class Foo {
// ...
}
public void myMethod() {
Foo myClass = new Foo();
}public class Bar {
// ...
}
public void myMethod() {
Bar myClass = new Bar();
}Uses var to declare local variables.
String s = "";var s = "";Also see: Change var type to resolved type
Uses the resolved type to declare local variables.
var s = "";String s = "";Also see: Change resolved type to var type
