Java refactoring and Source Actions

Visual Studio Code provides 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....

List of Supported Code Actions

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

Assign to variable

Assigns an expression to a local variable or a field.

Example

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

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

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
public class Clazz {
  public Interface method() {
    final boolean isValid = true;
    return new Interface() {
      public boolean isValid() {
        return isValid;