Eclipse Refactoring (GO)

Source: Internet
Author: User
Tags modifier modifiers

Refactoring types in eclipse
If you look at Eclipse's refactoring menu, you can see four of the parts. The first part is undo and redo. The other three sections contain the three types of refactoring that eclipse provides.

The first type of refactoring changes the physical structure of the code, like 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, like extract method and encapsulate Field. The refactoring list for these three parts is as follows.

Type 1 Physical Structure
L Rename
L Move
L Change Method Signature
L Convert Anonymous Class to Nested
L Convert Member Type to New File

Type 2 class hierarchy
L Push Down
L Push up
L Extract Interface
L Generalize Type (Eclipse 3)
L User supertype Where Possible
Type 3-Class internal structure
L Inline
L Extract Method
L Extract Local Variable
L Extract Constant
L Introduce Parameter
L Introduce Factory
L Encapsulate Field



Rename:
Rename is used to change the name of a Java element. Although you can manually change the Java file Java element Names, this does not automatically update all the files or Java elements that reference them. You must search for files in your project and replace them by hand. Chances are you'll miss one or change the wrong one. Rename refactoring intelligently updates all the places that have this reference.
Sometimes the name of the Java element is not very clear, or its functionality has changed. To keep the code readable, the element's name is also updated. With rename refactoring, it's very quick to update the name of the element and all the places that reference it.
To rename a Java element, select the element in the package browse 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 choose whether to update the element's reference. Clicking the Preview button will open the Preview window, where you can see that the content will be changed. Click the OK button and the refactoring is over.

Move
Move and rename are very similar. It is used to move elements from one position to another. It is primarily used to move classes from one package to another. Select the element you want to move, choose Move from the refactoring menu, or use the shortcut key, Alt+shift+v, and select the destination you want to move in the pop-up window. You can still check what's changed with the preview function, or you can press the OK button to make it work directly.


Change Method Signature
Changing the method signature can change the parameter name, parameter type, parameter order, return type, and the visibility of the method. You can also add, remove parameters.
To perform this refactoring, select the method that you want to refactor, select the Change method signature entry for the refactoring menu, and the Change Method signature dialog box appears.

In this dialog box, select the modifier of the method, return type, parameter. Parameter additions, modifications, moves, and deletions can be controlled by the right button. When a new parameter is added, the default value is automatically assigned. Any place that calls this method is entered with this default value as a parameter.
Changing the method signature may cause problems in the method, and if there is a problem, it will be flagged when you click Preview or OK.


Move members Type to New File
This refactoring converts nested classes to a single class. A new Java file will be created to contain this nested class. Select the class you want to refactor, select the Move Member type to New file entry on the refactoring menu, and add the name of the instance you want to create in the popup dialog box.



Push down
This refactoring moves the calculated methods and members from the parent class to its immediate subclasses, and all the push-down methods are optionally left in the parent class as an abstract method. The lower thrust is useful for rebuilding project designs.
Select several methods or members and select the push-down item from the refactoring menu to bring up the push-down dialog box.

In this dialog box, you can select the method or member individually, and all the selected elements will be moved to the subclass of the current class. When you click the Add Required button, all elements necessary for the selected element are also automatically selected, and this behavior does not guarantee that all necessary elements will be automatically selected, or that manual confirmation is required. When a method is selected, the Edit button is available, click the Edit button, and the Edit dialog box pops up. Where you can choose whether to leave abstract methods in the current class for the selected method or to delete them in the current class. Double-click one day to select the method, or open the Edit dialog box. When clicked on the Action column of the method, a drop-down list appears, in which you can select a legacy abstract method or delete a method in the current class. Press ENTER to confirm the result of the edit.


Pull Up
Moving up is similar to pushing down, and also moves methods and members between classes. Move up moves a method or member from one class to one of its parent classes. Select several methods or members and select Move Up in the refactoring menu, and the Move Up wizard appears immediately.
In the Select Target class multi-box, all parent classes that are inherited by the current class are listed. You can only move a method or member to one of them.
If the action column of the selected method is set to declare an abstract method in the target class, creating the required method option in the non-abstract subclass of the target class becomes optional. When it is selected, all subclasses of the target class, if they do not have a selected method, will create a selected method for them.
As in the push-down, select multiple methods, click the Edit button, or double-click a method to open the Edit Members dialog box. There are two options for moving up and declaring abstract methods in the target class. Move Up is just a simple copy method to the parent class, and provides a choice whether to delete the method in the current class. Declaring an abstract method in the target class creates an abstract method of the selected method in the parent class, and if the parent class is not an abstract class, it is placed as an abstract class, and the last method is left in the current class. As in the push-down, you can also click the Action column and select it in the drop-down list that appears.
If the action column of the method is selected as up, in the next wizard you will be asked to choose whether to delete the methods in the current class, and the selected method will be deleted in the current class.
At any step in the wizard, you can press the Finish button to end the refactoring operation, which is refactored by default rules.


Extract Interface
The refinement interface can create an interface from an existing class. You can choose those methods that contain classes in the interface. Select a class and select the Refinement interface item from the refactoring menu to open the Refine interface dialog box.
This dialog box will add the name of the interface, select the method you want to include, and in this list only public methods are listed. Selecting Change the application of the class [current class name] to the reference selection box for the interface will update all references to the current class to a reference to this interface.


Generalize Type
Generalization type refactoring can change the type of a declared object to its superclass, select a variable, parameter, object member, method return type, and then select the generalization type entry for the refactoring menu. In the Open Generalization Type dialog box, select the new type you want, and then click the Finish button to end the refactoring.


Use supertype Where Possible
Using a superclass changes a reference to a specific type to a reference to its superclass. Select a class and select the use superclass item for the refactoring menu to open the Use Super Class dialog box. Select the desired superclass type and click the Finish button to complete the refactoring. After refactoring, the instanceof expression is replaced accordingly.



Inline
An inline is a static final object member, or local variable, that replaces the calling method with code or value. For example, if you inline a method call, the place of the call is replaced with the method body. To inline a method, static final object members, local variables, select the elements, select inline items in the refactoring menu, or use the shortcut key ALT + Ctrl + I. In the inline dialog box that is subsequently opened, you can choose whether to inline all calls, or to select the calls. If you select all calls, you can also choose whether to delete the declaration itself.

Extract Method
If the method contains too many specific actions, the method is too long, or one of the code is used multiple times, you can use the refinement method to refactor this part of the code into a separate method. This refactoring is quick and easy to apply in eclipse.
Select the snippet you want to refine, select the Refinement method item from the refactoring menu, or use the shortcut key ALT + Shift + M.
In the Refinement Method dialog box, enter the name of the new method, select the modifier, and choose whether to let the new method throw a run-time exception. A preview of the new method is provided at the bottom.


Extract Local Variable
There are many advantages to using a variable instead of an expression. If the expression is used in multiple places, this can improve performance and improve the readability of the code. To refine an expression to a local variable, select the expression to refine, select Refine local variable from the refactoring menu, or use the shortcut key ALT + Shift + L.
In the Refine Local Variables dialog box, enter the name of the new variable, choose whether to replace all expressions, and make this variable final. Provides a preview of the variable at the bottom of the dialog box.

Extract Constant
Refining constants are similar to refining local variables, except that refining constant refactoring allows you to select modifiers for the extracted constants, and this constant will be used as a member variable for the class.

Introduce Parameter
Describes parameter refactoring creates a new parameter in the method, and then replaces the local variable or instance of the member variable with this new parameter. If you use this refactoring, select a reference to a member variable or a local variable in the method, and then choose the Introduction parameter item from the refactoring menu.


Introduce Factory
The factory is used to create a new object, returning the method of the newly created object. You can choose the constructor of a class, choose the Introduction Factory item from the refactoring menu, apply this refactoring, and create a factory method for this class.

In the Introduction Factory dialog box, enter the name of the factory method and the name of the object that needs to be created by the factory method. Select whether the modifier for the constructed method is private.
This specified factory method appears in the specified class when the OK button is clicked. This method creates an instance of the current class, and then returns this instance.

Convert Local Variable to Field
Convert a local variable to a member variable refactoring, moving a variable declaration inside a method into the same class as the method, making the variable visible to the entire class. Select a local variable, choose convert local variable to member variable item from the refactoring menu, and then open the configured dialog box.

In this dialog box, add the name of the member variable, select the modifier, and choose where to instantiate this member variable. Subsequent declarations are static and declared as final select whether they can be used, depending on the selection of the instantiated location.

Encapsulate Field
To practice object-oriented programming correctly, you should set the modifiers of the member variable to private and provide the appropriate accessors to access the member variables. But these operations are cumbersome. This is handy if you are using encapsulated member variable refactoring. Select a member variable and select Encapsulate member variable item from the refactoring menu.

In the Encapsulate Local Variables dialog box, add the getter, the name of the setter method, and select the method after which the new method appears. Select the appropriate modifier. This refactoring is applied to create two new methods, to make the modifiers of this member variable private, and to change the reference to this member variable to a reference to the new method.

To refactor a list of items:
The following table, extracted from the eclipse Help, lists the types of Java resources supported by various refactoring, and the corresponding shortcut keys.









This article describes the various refactoring provided by Eclipse. These refactorings are easy to use and ensure code refactoring is easier and more secure. And you can automatically generate code to increase productivity.

Some refactoring alters the structure of some classes, but does not alter the structure of other classes in the project, such as push-down and move-up refactoring. At this point, make sure that all references to the changed elements in the project are updated. That's why there's a good test set. Also, you should update the reference to the change element in the test suite. So, how important is the combination of refactoring and unit testing for software development http://ldzyz007.iteye.com/blog/1157075

Eclipse Refactoring (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.