Refactoring and unit testing are two magic weapons for programmers. They act like air and water for people, ordinary, inconspicuous, but significant. This article describes how to refactor in eclipse. This article describes the types of refactoring supported by ECLIPSE, their meanings, and how to refactor them. This article can also be used as a quick manual for learning and restructuring knowledge. What is refactoring refers to the process of changing the program structure on the basis of maintaining all the functions of the program. There are many types of refactoring, such as changing the class name, changing the method name, or extracting code into the method. Each reconstruction requires a series of steps to ensure that the Code is consistent with the original code. Why is refactoring very important when manual refactoring, it is easy to introduce errors in the Code, such as spelling errors or missing a refactoring step. To prevent the introduction of errors, a full test must be performed before and after each refactoring. You may wonder whether refactoring is worthwhile. There are many reasons for refactoring. You may want to update a program with bad code. Or the initial design team is gone, and now everyone in the team understands these procedures. To update, you must redesign the build program to meet your needs. Another reason is that the original design does not allow you to add new features. To add the code, you need to refactor the code. The third reason is that an automatic refactoring tool can automatically generate code for you, such as the refactoring function in eclipse. With refactoring, you can rewrite as few code as possible and keep software functions while making code logic better. Testing is very important in refactoring. The code structure should be changed for refactoring. Make sure that the function of the Code after reconstruction is not changed. A good test set is required for manual refactoring. The use of the automatic refactoring tool is necessary for testing, but does not need to be frequent. The automatic refactoring tool should not generate errors such as spelling errors during manual refactoring. In eclipse, you can use JUnit to easily create test code for the program. The specific method is not described in this article. The refactoring jdt in eclipse and the Java Plug-in eclipse can automatically refactor Java projects, classes, or members of various types. You can use multiple methods to quickly refactor an element in a Java project. To refactor some elements, You must select them. You can select these elements from multiple views, such as Outline View or package view. You can press Ctrl or shift to select multiple elements in the view. Another method is to highlight the editing area of the element or move the mouse to the source program file. After selecting the element you want to refactor, you can select reconstruction from the drop-down list of the refactoring menu, or you can choose reconstruction sub-menu from the shortcut menu. Eclipse also provides shortcut keys for refactoring. Some Refactoring can be applied to any element, while others can only be used on specific elements, such as classes or methods. The final table in this article lists the element types that can be applied for refactoring and the shortcut keys for refactoring. In eclipse, all Refactoring can be previewed before it is officially executed. In the refactoring dialog box, click "preview" to view all the changes to be made. The only refactoring without the preview button is pull up. In its refactoring wizard, the preview panel will always appear at the end. Some of these changes can be reversed so that these changes will not take effect. Undo and redo items are undo and redo items in the refactoring menu. They are different from undo redo in the editing menu. Even if the refactoring changes many files, undo redo in the editing menu will only change the current file. The undo and redo operations in the refactoring menu will undo and redo all the reconstructed files. However, they have certain restrictions during use. After reconstruction, whether the reconstruction changes the file or not, if any file is changed and saved, you cannot cancel Or redo the reconstruction. If a file is modified and edited but not saved, an error message is displayed. If you want to cancel or redo the refactoring, unsaved files must be removed. As long as you notice the preceding restrictions, you can undo or redo the refactoring as you like. You can even compile, run your program to test, and then cancel the refactoring, As long as you haven't changed and save any files. For the refactoring type in eclipse, if you look at the refactoring menu of Eclipse, You can see four parts. The first part is undo and redo. The other three parts contain three types of refactoring provided by eclipse. The first type of refactoring changes the physical structure of the Code, such as rename and move. The second is to change the code structure at the class level, such as pull up and push down. The third is to change the code inside the class, such as extract method and encapsulate field. The refactoring list of the three parts is as follows. Type 1 physical structure l renamel movel change method signaturel convert Anonymous class to nestedl convert member type to new file type 2 type hierarchies l push downl push upl extract interfacel generalize type (eclipse 3) l user supertype where possible type 3 Internal Structure l inlinel extract methodl extract local variablel extract constantl introduce parameterl introduce factory l encapsulate field rename: Rename is used to change the name of a Java element. Although you can manually change the name of the Java element in the Java file, it cannot automatically update all files or Java elements that reference them. You must search for files in the project and manually replace these references. It is very likely that you will miss one or make a mistake. Rename refactoring intelligently updates all places with this reference. Sometimes, the name of a Java element is not very clear, or its function has changed. To keep the code readable, the element name must be updated. Using rename refactoring, you can quickly update the name of an element and all the places where it is referenced. To rename a Java element, select the element in the package browsing view or outline view, select the rename item from the refactoring menu, or use the shortcut key Alt + Shift + R. The rename dialog box appears. Add a new name here and select whether to update the reference of the element. Click the preview button to open the preview window. Here, you can see that the content will be changed. Click OK to complete the reconstruction. Move move is similar to rename. It is used to move an element from one position to another. It is mainly used to move a class from one package to another. Select the element to be moved, select move from the refactoring menu, or use the shortcut key Alt + Shift + V to select the destination to be moved in the pop-up window. You can still use the preview function to check for any changes, or press the OK button to make them take effect. Change Method signature changing a method signature can change the parameter name, parameter type, Parameter order, return type, and method visibility. You can also add or delete parameters. To perform this reconstruction, select the method to be restructured and select the change method signature item in the Reconstruction menu. The change method signature dialog box is displayed. In this dialog box, select the modifier, return type, and parameter of the method. You can click the button on the right to add, modify, move, and delete parameters. When a new parameter is added, the default value is automatically assigned. This default value is used as the parameter input when this method is called. Changing the method signature may cause problems in the method. If there is a problem, it will be marked when you click preview or OK. Move members type to new file This refactoring converts the nested class into a separate class. A new Java file will be created to include this nested class. Select the class to be restructured, select the move Member type to new file option from the refactoring menu, and add the name of the instance to be created in the pop-up dialog box. Push down This refactoring moves the calculation methods and members from the parent class to its direct subclass. All push-down methods can be left in the parent class as an abstract method. Push-down refactoring is very useful for rebuilding the project design. Select several methods or members and select push-down items from the refactoring menu. the push-down dialog box is displayed. In this dialog box, you can select methods or members respectively. All selected elements will be moved to the subclass of the current class. When you click the Add required button, all the elements required for the selected elements are automatically selected. This action does not ensure that all required elements can be automatically selected, or you need to manually confirm. When a method is selected, the edit button is available. Click Edit to bring up the edit dialog box. You can choose to leave abstract methods in the current class for the selected method, or delete these methods in the current class. Double-click the method selected on a day to open the edit dialog box. Click the action column of the method to display a drop-down list. You can select the left-over abstract method or delete the method in the current class. Press enter to confirm the editing result. Pull up is similar to push-down. It is also a member and method for moving between classes. Move up moves methods or members from a class to one of its parent classes. Select several methods or members, and select the "Move Up" option from the refactoring menu. The "Move Up" Wizard will appear immediately. In the select target class multi-choice box, all parent classes inherited by the current class are listed. You can only move methods or members to one of them. If the action column of the selected method is set to declare the abstract method in the target class, the required method options created in the non-Abstract subclass of the target class become optional. When it is selected, all the child classes of the target class, if they do not have the selected method, will create the selected method for them. Select multiple methods, click Edit, or double-click a method. The edit member dialog box is displayed. There are two options: move up and declare abstract methods in the target class. Moving up is just a simple copy method to the parent class, and you can choose whether to delete the method in the current class. Declaring an abstract method in the target class creates an abstract method for the selected method in the parent class. If the parent class is not an abstract class, it is set to an abstract class, and the selected method is left in the current class. Like in the push-down process, you can also click the action column to select from the drop-down list that appears. If the action column of the method is selected as "Move Up", you will be asked to select whether to delete these methods in the current class in the next wizard. The selected methods will be deleted in the current class. In any step of the wizard, you can click the finish button to end the refactoring operation. At this time, the reconstruction is performed according to the default rules. The extract interface extraction interface can be used to create an interface from an existing class. You can choose the methods that contain classes in the interface. Select a class and select extract interface items from the refactoring menu to open the extract interface dialog box. In this dialog box, add the Interface Name and select the method to be included. Only public methods are listed in this list. Select and change the application of the class [current class name] to the interface reference selection box, and update all references to the current class as references to this interface. Generalize type generalized type reconstruction can change the type of a declared object to its superclass, select variables, parameters, object members, and return type of the method, and then select the generalized type item in the Reconstruction menu. In the displayed general type dialog box, select the expected new type, and click Finish to end the reconstruction. Use supertype where possible uses a superclass to change a reference to a specific type to a reference to its superclass. Select a class and select the use of superclass In the refactoring menu. The use of superclass dialog box is displayed. Select the expected superclass and click "finish" to complete the refactoring. After reconstruction, the instanceof expression will be replaced accordingly. Inline inline is the place where code or value is used to replace the call method, static final object member, or local variable. For example, if you inline a method call, the call will be replaced with the method body. To inline a method, static final object Member, local variable, select these elements, select inline items in the refactoring menu, or use the shortcut key Alt + Ctrl + I. In the subsequent inline dialog box, you can choose whether to inline all calls or the selected calls. If you select all calls, you can also choose whether to delete the Declaration itself.