Java refactoring and Source Actions

Visual Studio Code provide many options to refactor your source code as well as Source Actions to generate code and fix issues while you’re coding. To access them, click on the light bulb 💡 whenever you see it. Or right-click the editor view and pick Source Action….

Refactoring

The goal of the Java program refactoring is to make system-wide code changes without affecting behavior of the program. The Java Language Support for VS Code provides a lot of easily accessible refactoring options.

Invoke refactoring

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...:

Invoke Refactoring

Then you will see all the available refactoring options.

List of Supported Code Actions

Assign to variable

Assigns an expression to a local variable or a field.

Example

Before
  1. Arrays.asList("apple", "lemon", "banana");
After
  1. List<String> fruits = Arrays.asList("apple", "lemon", "banana");

It can also be used to assigns a parameter to a new field for unused parameter(s) in a constructor.

Change modifiers to final

Adds final modifier to all the variables and parameters in the current source file.

Example

Before
  1. public class Clazz {
  2. public void method(int value) {
  3. boolean notValid = value > 5;
  4. if (notValid) {
  5. // do something
  6. }
  7. }
  8. }
After
  1. public class Clazz {
  2. public void method(final int value) {
  3. final boolean notValid = value > 5;
  4. if (notValid) {
  5. // do something
  6. }
  7. }
  8. }

Convert anonymous to nested class

Converts an anonymous inner class to a member class.

Example

Let’s convert the anonymous class Interface(){...} to a member of the class Clazz.

Before
  1. public class Clazz {
  2. public Interface method() {
  3. final boolean isValid = true;
  4. return new Interface() {
  5. public boolean isValid() {
  6. return isValid;
  7. }
  8. };
  9. }
  10. }
After
  1. public class Clazz {
  2. private final class MyInterface extends Interface {
  3. private final boolean isValid;
  4. private MyInterface(boolean isValid) {
  5. this.isValid = isValid;
  6. }
  7. public boolean isValid() {
  8. return isValid;
  9. }
  10. }
  11. public Interface method() {
  12. final boolean isValid = true;
  13. return new MyInterface(isValid);
  14. }
  15. }

Convert to anonymous class creation

Converts lambda expression to anonymous class creation.

Example

The variable runnable is assigned with a lambda expression. Let’s convert it to an anonymous class creation.

Before
  1. public void method() {
  2. Runnable runnable = () -> {
  3. // do something
  4. };
  5. }
After
  1. public void method() {
  2. Runnable runnable = new Runnable() {
  3. @Override
  4. public void run() {
  5. // do something
  6. }
  7. };
  8. }

Also see: Convert to lambda expression

Convert to enhanced for loop

Converts the simple for loop to for-each style.

Example

Before
  1. public void order(String[] books) {
  2. for (int i = 0; i < books.length; i++) {
  3. // do something
  4. }
  5. }
After
  1. public void order(String[] books) {
  2. for (String book : books) {
  3. // do something
  4. }
  5. }

Convert to lambda expression

Converts an anonymous class creation to the lambda expression.

Example

Let’s convert the anonymous class Runnable(){...} to a lamda expression.

Before
  1. public void method() {
  2. Runnable runnable = new Runnable(){
  3. @Override
  4. public void run() {
  5. // do something
  6. }
  7. };
  8. }
After
  1. public void method() {
  2. Runnable runnable = () -> {
  3. // do something
  4. };
  5. }

Also see: Convert to anonymous class creation

Convert to static import

Converts the field or method to static import.

Example

Let’s transform the Assert.assertEquals() invocation to a static import.

Before
  1. import org.junit.Assert;
  2. ...
  3. public void test() {
  4. Assert.assertEquals(expected, actual);
  5. }
After
  1. import static org.junit.Assert.assertEquals;
  2. ...
  3. public void test() {
  4. assertEquals(expected, actual);
  5. }

Extract to constant

Creates a static final field from the selected expression and substitutes a field reference, then rewrites other places where the same expression occurs.

Examples

Let’s extract the value of π: 3.14 to a constant.

Before
  1. public double getArea(double r) {
  2. return 3.14 * r * r;
  3. }
After
  1. private static final double PI = 3.14;
  2. public double getArea(double r) {
  3. return PI * r * r;
  4. }

Also see: Inline constant

Extract to field

Declares a new field and initializes it with the selected expression. The original expression is replaced with the usage of the field.

Examples

Let’s extract the variable area to a field of the class Square.

Before
  1. class Square {
  2. public void calculateArea() {
  3. int height = 1;
  4. int width = 2;
  5. int area = height * width;
  6. }
  7. }
After
  1. class Square {
  2. private int area;
  3. public void calculateArea() {
  4. int height = 1;
  5. int width = 2;
  6. area = height * width;
  7. }
  8. }

When selecting a variable declaration, convert the variable to field.

Extract to method

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.

Examples

Let’s extract the expression height * width to a new method.

Before
  1. public void method() {
  2. int height = 1;
  3. int width = 2;
  4. int area = height * width;
  5. }
After
  1. public void method() {
  2. int height = 1;
  3. int width = 2;
  4. int area = getArea(height, width);
  5. }
  6. private int getArea(int height, int width) {
  7. return height * width;
  8. }

Also see: Inline method

Extract to local variable

Creates a new variable assigned to the expression currently selected and replaces the selection with a reference to the new variable.

Examples

Let’s extract the expression platform.equalsIgnoreCase("MAC") to a new variable.

Before
  1. public void method() {
  2. if (platform.equalsIgnoreCase("MAC")) {
  3. // do something
  4. }
  5. }
After
  1. public void method() {
  2. boolean isMac = platform.equalsIgnoreCase("MAC");
  3. if (isMac) {
  4. // do something
  5. }
  6. }

After the extraction, you can also perform a rename in the same transaction.

Also see: Inline local variable

Inline constant

Replaces a constant reference with its defined value.

Examples

Let’s replace the constant PI to its defined value: 3.14.

Before
  1. private static final double PI = 3.14;
  2. public double getArea(double r) {
  3. return PI * r * r;
  4. }
After
  1. private static final double PI = 3.14;
  2. public double getArea(double r) {
  3. return 3.14 * r * r;
  4. }

Also see: Extract to constant

Inline local variable

Replaces redundant variable usage with its initializer.

Examples

Let’s replace the variable isMac directly to the boolean expression.

Before
  1. public void method() {
  2. boolean isMac = platform.equalsIgnoreCase("MAC");
  3. if (isMac) {
  4. // do something
  5. }
  6. }
After
  1. public void method() {
  2. if (platform.equalsIgnoreCase("MAC")) {
  3. // do something
  4. }
  5. }

Also see: Extract to local variable

Inline method

Replaces calls to the method with the method’s body.

Example

Let’s replace the method getArea(int height, int width) directly to the expression height * width.

Before
  1. public void method() {
  2. int height = 1;
  3. int width = 2;
  4. int area = getArea(height, width);
  5. }
  6. private int getArea(int height, int width) {
  7. return height * width;
  8. }
After
  1. public void method() {
  2. int height = 1;
  3. int width = 2;
  4. int area = height * width;
  5. }

Also see: Extract to method

Invert conditions

Inverts the boolean expression in the conditions.

Example

Let’s invert the boolean expression in the if statement.

Before
  1. public void method(int value) {
  2. if (value > 5 && value < 15) {
  3. // do something
  4. }
  5. }
After
  1. public void method(int value) {
  2. if (value <= 5 || value >= 15) {
  3. // do something
  4. }
  5. }

Invert local variable

Inverts the local boolean variable.

Example

Let’s invert the variable valid.

Before
  1. public void method(int value) {
  2. boolean valid = value > 5 && value < 15;
  3. }
After
  1. public void method(int value) {
  2. boolean notValid = value <= 5 || value >= 15;
  3. }

Move

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

Example

Let’s move the static method print() from class Office to class Printer.

Before
  1. public class Office {
  2. public static void main(String[] args) {
  3. print();
  4. }
  5. public static void print() {
  6. System.out.println("This is printer");
  7. }
  8. static class Printer { }
  9. }
After
  1. public class Office {
  2. public static void main(String[] args) {
  3. Printer.print();
  4. }
  5. static class Printer {
  6. public static void print() {
  7. System.out.println("This is printer");
  8. }
  9. }
  10. }

Move refactoring on a static method if it is used more in another class than in its own class.

Move a class to another package. Currently, move refactoring is not supported from file explorer.

Move an inner class to new a new file.

Rename

Default shortcut: kb(editor.action.rename)

Renames the selected element and corrects all references to the elements (also in other files).

Example

Let’s rename the class Foo to Bar

Before
  1. public class Foo {
  2. // ...
  3. }
  4. public void myMethod() {
  5. Foo myClass = new Foo();
  6. }
After
  1. public class Bar {
  2. // ...
  3. }
  4. public void myMethod() {
  5. Bar myClass = new Bar();
  6. }

The shortcut to invoke the Rename refactoring is kb(editor.action.rename). When you invoke the shortcut on an identifier in the editor, a small box displays within the editor itself where you can change the identifier name. When you press kbstyle(Enter), all references to that identifier are changed too.

Rename refactoring is also supported from the File Explorer for folders and files. After requesting the change, a preview of impacted files will be provided and you can decide how to apply those changes.

Rename from Explorer

Change resolved type to var type

Uses var to declare local variables.

Example

Before
  1. String s = "";
After
  1. var s = "";

Also see: Change var type to resolved type


Change var type to resolved type

Uses the resolved type to declare local variables.

Example

Before
  1. var s = "";
After
  1. String s = "";

Also see: Change resolved type to var type

Source Actions

Source Actions could be used to generate common code structures and recurring elements. Some of them are quick fixes which helps you fix your code issues on the fly.

Generate constructor from super class

Add a constructor from super class.

Generate delegate methods

Generate delegate methods

Override/implement methods

With this Source Action, all the candidates are presented to you with a checklist. You can then decide what to override or implement.

Organize imports

You can use this Source Action to clean up your imports. It can also deal with ambiguous imports, in that case, a dropdown list will be presented for you to pick the right one. The code line with the unresolved type is also presented to you to help you decide.

Generate getters and setters

You can bulk generate getters and setters for all new member variables. If the class has more than one field, the Source Action will prompt a Quick Pick for you to select the target fields to use to generate the accessor methods.

Generate hashCode() and equals()

hashCode() and equals() can be generated with default implementations. All the non-static member variables are listed, and you can customize the generated code using the check list.

There are two options for you to customize the generated code:

  • If you use Java 7+, you can set java.codeGeneration.hashCodeEquals.useJava7Objects to true to generate shorter code that calls Objects.hash and Objects.equals.
  • You can also set java.codeGeneration.hashCodeEquals.useInstanceof to true to use instanceOf operator to check the object types instead of calling Object.getClass().

Generate toString()

There is a new Source Action to generate the toString() method. Customization is possible with a check list of all the member variables.

Fix non-accessible reference

This Quick Fix helps you fix non-accessible reference.

Create non-existing package

When your package name doesn’t match the folder name, you have the options to either change the package name in your source code, or move the folder in the file system (even when the destination folder doesn’t exist yet).

Other Code Actions supported

The list of Code Actions supported by VS Code keeps growing and only listed the most popular ones above. Other notable supported actions include (but not limited to):

  • Create unresolved types
  • Remove the final modifier
  • Remove unnecessary cast
  • Remove redundant interfaces
  • Add missing case labels in switch statements
  • Jump to definition on break/continue
  • Correct access to static elements