Java refactoring and Source Actions
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. Join VS Code at Microsoft Ignite or catch the sessions online. A year of VS Code, wrapped. Explore the highlights. Java refactoring and Source Actions View as Markdown Ask in VS Code Ask in VS Code Insiders 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 Assign to variable 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 Extract to constant Extract to field Extract to method Extract to local variable Inline refactorings Inline constant Inline local variable Inline method Invert boolean Invert conditions Invert local variable Move Rename Type change Change resolved type to var type Change var type to resolved type Source Actions Generate constructors Generate delegate methods Override/implement methods Organize imports Generate getters and setters Generate hashCode() and equals() Generate toString() Change modifiers to final where possible Other Code Actions supported Fix non-accessible reference Create non-existing package More...
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... : 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; } }; } } After 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); } } 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 public void method () { Runnable runnable = () -> { // do something }; } After public void method () { Runnable runnable = new Runnable () { @ Override public void run () { // do something } }; } Also see: Convert to lambda expression Convert to enhanced for loop Converts the simple for loop to for-each style.
Example Before public void order ( String [] books) { for ( int i = 0 ; i < books . length ; i++) { // do something } } After public void order ( String [] books) { for ( String book : books) { // do something } } Convert to lambda expression Converts an anonymous class creation to the lambda expression. Example Let's convert the anonymous class Runnable(){...} to a lambda expression. Before public void method () { Runnable runnable = new Runnable (){ @ Override public void run () { // do something } }; } After public void method () { Runnable runnable = () -> { // do something }; } 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 import org.junit.Assert; ... public void test () { Assert . assertEquals (expected, actual); } After import static org.junit.Assert.assertEquals; ... public void test () { assertEquals (expected, actual); } 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 public double getArea ( double r) { return 3.14 * r * r; } After private static final double PI = 3.14 ; public double getArea ( double r) { return PI * r * r; } 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 class Square { public void calculateArea () { int height = 1 ; int width = 2 ; int area = height * width; } } After class Square { private int area ; public void calculateArea () { int height = 1 ; int width = 2 ; area = height * width; } } 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 public void method () { int height = 1 ; int width = 2 ; int area = height * width; } After 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 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 public void method () { if ( platform . equalsIgnoreCase ( "MAC" )) { // do something } } After public void method () { boolean isMac = platform . equalsIgnoreCase ( "MAC" ); if (isMac) { // do something } } 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 private static final double PI = 3.14 ; public double getArea ( double r) { return PI * r * r; } After private static final double PI = 3.14 ; public double getArea ( double r) { return 3.14 * r * r; } 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 public void method () { boolean isMac = platform . equalsIgnoreCase ( "MAC" ); if (isMac) { // do something } } After public void method () { if ( platform . equalsIgnoreCase ( "MAC" )) { // do something } } 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 public void method () { int height = 1 ; int width = 2 ; int area = getArea (height, width); } private int getArea ( int height, int width) { return height * width; } After public void method () { int height = 1 ; int width = 2 ; int area = height * width; } Also see: Extract to method Invert conditions Inverts the boolean expression in the conditions. Example Let's invert th
The rest of this page is on the live site.