Create a new refactoring feature in Eclipse

Source: Internet
Author: User
Tags abstract add end execution extend final functions version
Creating strong support for refactoring is one of the most important reasons software developers love Eclipse. And Eclipse has an advantage that is at least as good as refactoring, and that is its near-unassailable scalability. The combination of the two means that we can create new refactoring capabilities based on our own needs.

   introduce

Refactoring plays an important role in the process of modern software development, it can reduce the workload of software developers and improve the productivity of software development. To illustrate the importance of refactoring, here's a quote from the tutorial on refactoring provided by David Carew on Developerworks:

Now, a developer's job is mostly to modify existing code, rather than to write new code. A simple modification might include adding to existing code. However, a change in the variety or expansion of the software will cause the internal structure to deteriorate. Refactoring changes the internal structure of the software to make the software easier to understand and makes it less costly to modify without changing its apparent behavior. In the Java software development process, we have at least the following benefits by using the refactoring tools provided by eclipse:

1. The final product is more robust: our changes to the program code will be less likely to go wrong, there may be fewer missing modifications, even if there is a problem can be returned through the Undo function to the state before the refactoring.

2. Increased production efficiency. Often, a refactoring can complete multiple changes to the program's code. The most obvious example may be the rename refactoring provided by Eclipse, which is able to change all references while modifying the name. Eclipse provides us with a variety of practical refactoring capabilities that can be of great benefit to us when we use these refactoring in the software development process. However, for the special needs of each developer, there are always some desperately needed features that are not available through refactoring. At this point, we can extend some of the eclipse platform to create refactoring that adapts to our own needs. If this refactoring happens to fit the needs of most people, we can also contribute our refactoring to the eclipse community like other Eclipse contributor.

Next, we'll show you an example of how to create a new refactoring feature in Eclipse. The refactoring we create here will be used to migrate the JUnit test cases. We know that in the current version of JUnit, a function for testing must start with the string "test" as the method name. In the upcoming JUnit 4, a "@Test" annotation is used to indicate that the method is a test method. The refactoring we're about to create will complete this migration work by adding the "@Test" tag before all the methods that start with "test". @Test annotation can also contain a timeout property to specify the maximum execution time for the method, we provide a page in the wizard for the user to choose whether to timeout properties.

   Results Preview

In order to give the reader an intuitive feeling, we first introduce the actual operation effect of the example in this article. After reading this article, the reader friend can also successfully complete similar functions.

After starting the refactor provided by the example program, we got a three-page wizard. In the first page, the user can choose whether the timeout parameter is required, and the user can set the value of the timeout parameter.


Figure 1 Input parameters
When the user enters the parameter, we will go to the next page by clicking the Next button. The wizard will conduct initial and final condition checks and feed back the results of the check to the user. As we can see in Figure 2, the initial and final conditions are normal, so we can go to the next step.


Figure 2 Display Condition check
Next to the preview window (Figure 3), the wizard uses an intuitive interface to show what changes we will make to the source code after the application Wizard. This page allows the user to determine whether the final changes are in line with their needs. In addition, users can selectively cancel changes to certain files.


When the user checks the preview page to confirm that there are no problems, the user can press the Finish button to complete the refactoring. At this point, the source code changes, and the final results are as follows:

Listing 1

Package main;

public class Testsomething {
@Test (timeout=500)
public void Testsomething () {}
}

   overall structure and process

In Eclipse, a refactoring operation consists mainly of the following three parts:

1. Refactoringwizard class: Refactoringwizard provides a wizard-style user interface to guide users through refactoring work. No need for us to do any work, Eclipse has provided us with the preview page, the conditional check page, and the Undo/redo function through Refactoringwizard. We need to inherit this class to provide a specific user interface for the refactoring process.

2. Refactoring class: Refactoring class to complete the specific positioning and modify the code function. To create a new refactoring, we need to inherit this class and implement the logical part of refactoring.

3. AST and Astparser: In the refactoring class, we need to locate and modify the code, which can be done through the AST mechanism. The AST is the abbreviation for Abstract syntax tree, which can parse Java code into a structure. After taking advantage of the AST tree, changes to the source code become the traversal of the AST tree, change the node properties, and insert and delete nodes.

A typical refactoring operation flow is shown below:

1. The user selects the object to be refactored and initiates the refactoring operation through a menu item or button.

2. Create a specific refactoring class, pop-up Refactoringwizard.

3. Refactoringwizard interacts with the user, directs the user to enter the necessary parameters, Refactoringwizard invokes the refactoring class's function for conditional checking.

4. The refactoring class creates an AST and uses it to locate and modify the source code. The modifications made here are not applied directly to the source code, but are saved as change objects for use by the refactoring framework.

5. Refactoringwizard calls the function of the refactoring class to obtain a detailed description of the refactoring content (that is, the change object generated in step 4th), which is displayed on the preview screen for user confirmation.

6. After the user confirms the refactoring frame will modify the code, the refactoring operation ends.

Next, we'll detail the steps for creating a new refactoring type.

   Create a plug-in project

After a general understanding of the system architecture, our introduction begins with the creation of the project. As you all know, Eclipse provides great extensibility, and by creating plug-ins we can seamlessly insert the refactoring capabilities we are adding to the eclipse platform. The method of creating a plug-in project is introduced in many places and is not explained in detail here.

Select Plug-in Project by using the menu File-> new-> Project. Click Next, the dialog box appears, enter the project name Manage.annotation, and accept the default values for the other options. Click Next to enter the dialog box for the Plugin property settings and continue to accept the default values. Click Next to select the Plugin Template dialog box, which will add a new menu item to the Refactor menu, so here we use the "Hello,world" plug-in template. Click Next to change the value of the action class name to Annotationmanageaction and click the Finish button. At this point, a plug-in project for the most basic Eclipse Workbench was created. After the plug-in project is created, the default goes into the Plug-in development perspective, and the Plug-in manifest editor automatically opens to show the basic information about the plug-in project, such as dependencies on other plug-ins, extension points, build configuration information, and so on. Because the project needs to use the functionality of other plug-ins, it must be added to the dependencies of other plug-ins. Click on the Dependencies page in the plug-in manifest Editor and add the following plugins through the Add button in the required Plug-ins list on the page:

Listing 2

Org.eclipse.jface.text
Org.eclipse.ltk.core.refactoring
Org.eclipse.ltk.ui.refactoring
Org.eclipse.jdt
Org.eclipse.jdt.core


Or it can be done by directly modifying the MANIFEST.MF file. Look at the MANIFEST.MF file after the operation is complete, and note whether the newly added items appear in the Require-bundle list. MANIFEST. MF file is as follows:

Listing 3

manifest-version:1.0
Bundle-manifestversion:2
Bundle-name:annotation Plug-in
Bundle-symbolicname:manage.annotation; Singleton:=true
bundle-version:1.0.0
Bundle-activator:manage.annotation.annotationplugin
Bundle-localization:plugin
Require-bundle:org.eclipse.ui,
Org.eclipse.core.runtime,
Org.eclipse.jface.text,
Org.eclipse.ltk.core.refactoring,
Org.eclipse.ltk.ui.refactoring,
ORG.ECLIPSE.JDT,
Org.eclipse.jdt.core
Eclipse-autostart:true


In plug-in The plugin manifest file Plugin.xml is opened in the manifest editor, as you can see, this plugin expands the org.eclipse.ui.actionSets extension point, which is the extension point of a basic Eclipse workbench, and by extending it, Plug-ins can be very simple to eclipse's menu , and the toolbar is extended. This plugin.xml is a list file for the "Hello,world" plug-in template, which we change to a file suitable for this project. The list is as follows:

Listing 4

>


Point= "Org.eclipse.ui.actionSets" >
Label= "Annotation Action Set"
Visible= "true"
Id= "Manage.annotation.actionSet" >
Label= "%refactoring.menu.label"
Path= "Source"
Id= "Org.eclipse.jdt.ui.refactoring.menu" >


Class= "Manage.annotation.actions.AnnotationManageAction"
icon= "Icons/sample.gif"
Id= "Manage.annotation.actions.AnnotationManageAction"
Label= "%annotation.manage"
Menubarpath= "Org.eclipse.jdt.ui.refactoring.menu/reorggroup"
Toolbarpath= "Reorggroup"
tooltip= "Manage Annotation in Java Project"/>



The manifest file indicates that a new menu item "Annotation Manage" has been added to the refactor menus, and a button is added to the toolbar accordingly. Clicking on a menu item or button event is handled by the class "Manage.annotation.actions.AnnotationManageAction".

The last thing to be modified is the Manage.annotation.actions.AnnotationManageAction class. It inherits the Org.eclipse.ui.IWorkbenchWindowActionDelegate interface, which is used to handle various operations that are added through extension points. When a menu item or button is clicked, the class is loaded by the Eclipse console, processing the forwarded request, and the next action.

Once the annotationmanageaction is created, once the user's selection has changed, the selectionchanged function of the interface is triggered, informing the user of the selected part, You can modify the availability of the operation or other display properties according to the user's selection in this function. For example, in the engineering of this article, we want to use this operation only if the user chooses a Java model element, then you need to add the following code to the SelectionChanged:

Listing 5

public void SelectionChanged (IAction action, iselection selection) {
if (Selection.isempty ())
select = NULL;
else if (selection instanceof istructuredselection) {
Istructuredselection strut = ((istructuredselection) selection);
if (Strut.size ()!= 1)
select = NULL;
if (Strut.getfirstelement () instanceof ijavaelement)
select = (ijavaelement) strut.getfirstelement ();
} else
select = NULL;

action.setenabled (select!= null);
}


The parameters of the SelectionChanged function selection The user selection, we first determine if the number of selections is one, and then determine if the unique selection is a Java model element. Neither of these conditions will result in the execution of action.setenabled (false), which will pop up the following dialog box stating that the action is not available, and that the menu items and buttons will appear dimmed until the user selects the appropriate part, and the menu items and buttons are displayed. You can do a specific operation.


Figure 4 shows a dialog box that the action cannot execute at this time


The execution of the operation is implemented in the Annotationmanageaction run function, for example, in the engineering of this article, which is to eject the Refactoringwizard dialog box to guide the user through refactoring, which we will cover in the following sections.

   Extended Refactoring Class

With the introduction of the previous system architecture, you know that refactoring and Refactoringwizard are the basic classes to complete eclipse refactoring. After the creation of the plug-in project, we will extend the refactoring to achieve specific functions.

Refactoring is the abstract parent class of all classes that support code conversion, and it plays a very important role in interacting with refactoringwizard throughout the process to complete the refactoring function. These classes need to provide the following two types of methods:

The method used for condition checking is to judge whether the reconstruction operation can be carried out and whether the transformation can be completed.

Creates a method for the change object that describes all the modifications to the current code that will be performed.

The typical flow of the refactoring class is as follows:

1. The specific refactoring class is created.

2. Obtain the user-selected object to be refactored and initialize the refactoring class. The corresponding method is given by the concrete implementation class.

3. When the refactoring operation begins, the first call to refactoring Checkinitialconditions (Iprogressmonitor) is to perform an initial check based on the user's chosen object, which is usually performed automatically by the interface. The return refactoringstatus.fatal indicates that the initial check did not pass and the refactoring operation cannot continue.

4. Other parameters for refactoring are obtained, for example, for renaming operations to refer to a new name. This is usually provided by the interface according to the user's input. The corresponding method is given by the concrete implementation class.

5. After obtaining the user input parameters, call refactoring's checkfinalconditions (Iprogressmonitor) for the rest of the check, which is usually performed automatically by the interface, The return refactoringstatus.fatal indicates that the final check did not pass and the refactoring operation cannot continue.

6. Call Refactoring's Createchange (iprogressmonitor) to obtain the change object, which is typically performed automatically by the interface, which displays the preview interface according to the change object.

Based on the above introduction, in order to realize the reconstruction operation in this project, we need to extend the refactoring class, add a constructor for it, and implement Checkinitialconditions, Checkfinalconditions and Createchange three functions.

First, the dialog box that creates the class is popped through the menu file-> New->class. Enter the package name Manage.annotation.refactor, the class name annotationrefactoring, enter the parent class org.eclipse.ltk.core.refactoring.Refactoring, and select Inherit abstract method check box, click the Finish button, a basic class annotationrefactoring that extends the refactoring is created.

The constructor is added first to the user-selected Java model element as an argument. Refactoring analyzes this parameter to get all the relevant writable Java files, as the object of the refactoring operation, if the model element is contained in the Java file, locate the file node that contains it, and if the model element contains a Java file, finds all its child Java files. The constructor code is as follows:

Listing 6

Public annotationrefactoring (ijavaelement element) {
while (Element.getelementtype () > Ijavaelement.compilation_unit) {
element = Element.getparent ();
if (element = = null)
Return
}
if (element.getelementtype () = = Ijavaelement.compilation_unit) {
if (!element.isreadonly ())
Compilationunits.add (Element);
}
if (Element.getelementtype () ijavaelement.compilation_unit)
Findwritablecompilationunits (Element);
}


Then completes the Checkinitialconditions function, realizes the initial inspection the concrete operation. As an example, in this project we do not carry out any specific inspection operations, only simple to give the initial check the success of the information, return to Refactoringstatus.

Info so that the refactoring operation continues to execute. The Checkinitialconditions function code is as follows:

Listing 7

Public Refactoringstatus Checkinitialconditions (iprogressmonitor pm)
Throws Coreexception, OperationCanceledException {
Return Refactoringstatus.createinfostatus ("Initial Condition is ok!");
}


Then completes the Checkfinalconditions function, realizes after obtains the user input parameter the follow-up inspection operation. In this project, we first collect all the methods that need to be annotated to start with test to determine if there is no such method, if there is no error message, Returns the Refactoringstatus.fatal to end the refactoring operation, and if so, gives information about the success of subsequent checks and returns Refactoringstatus.

INFO. The Checkfinalconditions function code is as follows:

Listing 8

Public Refactoringstatus Checkfinalconditions (iprogressmonitor pm)
Throws Coreexception, OperationCanceledException {
Collectchanges ();
if (fchangemanager.size () = = 0)
Return Refactoringstatus.createfatalerrorstatus ("No Testing Methods found!");
else return Refactoringstatus.createinfostatus ("Final condition is ok!");
}


Finally, the Createchange function that creates the change object is the most core code in the entire refactoring operation, and its implementation is described in the following sections.

  Using the AST to construct a change object

When we find the modified location, we have two choices:

1. Scan the code through the Iscanner interface and modify the code directly via the Ibuffer interface

2. Structured modification by traversing and editing the AST tree

Developerworks provides an example of using the Ibuffer interface in the article "Extending the Java development tools for Eclipse". Now we're going to talk about using the AST to traverse and modify Java source code.

The AST is an abbreviation for abstract syntax tree. It is an extremely powerful source code resolution and editing tool that is provided by the Java Development Environment (JDT) in Eclipse.

Before using the features provided by the AST tree, we first create an AST tree. Since the construction of an AST tree is a time-consuming operation, JDT does not resolve the source code to the AST tree by default. The following code demonstrates the process of obtaining an icompilationunit corresponding AST tree. In the API provided by JDT, the Icompilationunit interface is used to represent a source code file that can be compiled. In the example program we provided, we resolved the entire file into an AST tree through the following code.

Listing 9

Astparser parser = Astparser.newparser (AST. JLS3);
Parser.setsource (CU);
Astnode root = Parser.createast (null);


Each node in the AST tree is a astnode type, and through visit mode we can access all the nodes that a astnode contains. The following code shows a way to access an AST node and get all of the methoddeclaration nodes in it.

Listing 10

private void GetMethods (Astnode cuu, Final List methods) {
Cuu.accept (New Astvisitor () {
public boolean visit (methoddeclaration node) {
Methods.add (node);
return false;
}
});
}


After all the methoddeclaration nodes have been collected, we can modify the AST tree by inserting and deleting nodes in the AST tree or by modifying existing nodes. The following code demonstrates the ability to add @test annotation to a method using the AST tool.

Listing 11

Private Boolean Collectchanges (Compilationunit Root,methoddeclaration method) {
if (Method.getname (). Getfullyqualifiedname (). StartsWith ("test")) {
AST ast = Method.getast ();
if (needtimeout) {
Normalannotation na = Ast.newnormalannotation ();
Na.settypename (Ast.newsimplename ("Test"));
Membervaluepair pair = Ast.newmembervaluepair ();
Pair.setname (Ast.newsimplename ("timeout"));
Pair.setvalue (Ast.newnumberliteral ("500"));
Na.values (). Add (pair);
Method.modifiers (). Add (0, NA);
} else {
Markerannotation na = Ast.newmarkerannotation ();
Na.settypename (Ast.newsimplename ("Test"));
Method.modifiers (). Add (0, NA);
}
return true;
}
return false;
}


In the refactoring framework, we require that changes to the AST tree are not immediately reflected in the source code. Instead, we need a change object that records the entire modification process. The refactoring framework will use this change object to display Priveiw windows, Undo and redo operations. In general, we record the process of modifying an AST tree to generate a change object as shown in the following code.

Listing 12

Root.recordmodifications ();

Here to modify the AST tree ...

TextEdit edits = root.rewrite (document, Cu.getjavaproject ()
. GetOptions (True));
Textfilechange change = new Textfilechange ("", (IFile) CU
. GetResource ());
Change.setedit (edits);


Finally, because the Createchange method of the refactoring class returns only a change object, if we need to modify multiple source code files, We can use the Compositechange class to encapsulate more than one change object into a Move object. This process may resemble a process executed by the following code

Listing 13

Public Change Createchange (iprogressmonitor pm) throws Coreexception,operationcanceledexception {
change[] changes = new change[fchangemanager.size ()];
System.arraycopy (Fchangemanager.toarray (), 0, changes, 0,fchangemanager.size ());
Compositechange change = new Compositechange ("Add @Override Annotation", changes);
return change;
}

   Extended Refactoringwizard Framework

The Refactoringwizard framework in Eclipse extends the wizard framework for Eclipse, and an Introduction to the wizard framework can be found in Eclipse's help system, Here we only discuss the Refactoringwizard framework from the perspective of OO design and architecture.

We start with several classes related to wizard:

1. Wizardpage class

Wizardpage is a combination of interfaces that contains multiple interface elements, such as text boxes, button buttons. Each page is independent and can be loaded dynamically. The duties of the Wizardpage category are:

• Combine SWT interface elements to construct an interface page.

• Define the operational behavior of the interface elements.

Two common property pages are preset in the Refactoringwizard framework: Previewwizardpage and Errorwizardpage. The Previewwizardpage class is used to preview changes after refactoring, comparing code or other resources. The Errorwizardpage class is used to handle conditional checks and error status notifications. We can simply extend the Refactoringwizard framework to automatically capture these two powerful features.

2. Wizard Class

A wizard is a container that loads a series of wizardpage pages, and the Duties of the wizard class are:

• Loading a series of wizardpage to construct a complex interface.

• Load domain classes to handle specific business logic. (This class is the refactoring class in the Refactoringwizard framework)

Maintains data transfer and state sharing between wizardpage pages, as well as between pages and domain classes. (to be added here, in fact, there are specialized classes in the implementation of the specific Refactoringwizard framework to share this part of the responsibility.) )

Our interface behavior can be varied (by combining different wizardpage), and the domain class that handles the business logic can be changed independently, and you can expand the wizard interface (-open to expansion) without modifying the existing Refactoringwizard framework ( -Closed to modifications, this is the most basic principle of OO design-OCP (open-close principle).

3. Wizarddialog class

The main responsibility of this dialog class is to construct a complete GUI interface and an operating interface. It presets some interface elements, such as buttons (Back,next,finish,cancel), which is responsible for loading the wizard class and switching between multiple wizardpage by button back and Next during operation.

Below we give the architecture diagram of the Refactoringwizard framework:


Fig. 5 Refactoring Wizard Frame composition


As we can see from Figure 5, if we think of each wizardpage page as a business, then refactoring is the control center that handles the business logic, encapsulating all the processing of the business logic, which can, of course, be delegated to the processing task. Note, however, that it is not responsible for implementing the business process, which means that the logical order relationships between the various business (individual page interfaces) are not maintained by it.

The Refactoringwizard framework takes full account of the scalability of the application, adding new schema elements based on SWT's MVC (model-view-control) meta schema pattern. The MVC pattern makes business logic and interface separation, interface and control behavior separate, and the Refactoringwizard framework enhances the separation of the interface itself, it splits a complete interface into multiple pages, users can dynamically combine these pages or add new pages to expand the interface behavior. This feature-the dynamic combination of interfaces, low coupling, cohesion well-structured classes, encapsulated good interfaces-gives us a glimpse of the essence of OO design.

Below we extend the Refactoringwizard framework by following several steps:

• Extended Refactoringwizardpage

• Extended Refactoringwizard

• Start Refactoringwizard

The first step is to extend Refactoringwizardpage: First we create a new class annotationrefactoringwizardpage, which needs to inherit the Userinputwizardpage class ( Its parent class is refactoringwizardpage, and Refactoringwizardpage finally implements the Idialogpage interface). The next step is to implement the CreateControl (...) of the Idialogpage interface. method, implement your interface behavior in this method, such as the timeout text box in our example, the code list is as follows:

Listing 14

/**
* Create composite to add UI elements
*/
public void CreateControl (composite parent) {
Define UI
Composite composite = new Composite (parent, SWT. NONE);
GridLayout lay = new GridLayout ();
Lay.numcolumns = 2;
Composite.setlayout (lay);

btncheck = new Button (composite, SWT. CHECK);
Btncheck.settext ("Add timeout parameter");
Griddata Gdbtncheck = new Griddata ();
Gdbtncheck.horizontalspan = 2;
Gdbtncheck.horizontalalignment = Griddata.fill;
Btncheck.setlayoutdata (Gdbtncheck);

Labname = new Label (composite, SWT. WRAP);
Labname.settext ("TimeOut:");
Griddata gdlabname = new Griddata ();
Gdlabname.horizontalalignment = griddata.beginning;
Gdlabname.grabexcesshorizontalspace = true;
Labname.setlayoutdata (Gdlabname);

Txttimeout = new Text (composite, SWT. Single | Swt. BORDER);
Griddata gdtxttimeout = new Griddata ();
Gdtxttimeout.horizontalalignment = Griddata.end;
Gdlabname.grabexcesshorizontalspace = true;
Txttimeout.setlayoutdata (gdtxttimeout);
Txttimeout.settext ("500");

Init status
Labname.setenabled (FALSE);
Txttimeout.setenabled (FALSE);
Add Listener
Definelistener ();

Control of incorporating composite into the framework
Setcontrol (composite);
Dialog.applydialogfont (composite);
}


One thing we should pay special attention to here is that after defining our interface elements, we need to incorporate the custom composite into the framework's control, which is this line of code: "Setcontrol (composite);"

Before we proceed to the next page with the input data check, we need to set the status of the page completion and pass the input data to the domain class refactoring. After we set up the page completion status with the following code, the next page errorwizardpage will handle the display logic:

Listing 15

private void Notifystatus (Boolean valid, String message) {
Setting error messages
Seterrormessage (message);
Set Page completion status
Setpagecomplete (valid);
}


Passing input data is handled by the following code:

Listing 16

private void Setrefactoring (Boolean selection, String text) {
Annotationrefactoring refactoring = (annotationrefactoring) getrefactoring ();
Refactoring.setneedtimeout (TRUE);
if (selection) {
Refactoring.settimeout (Integer.valueof (Txttimeout.gettext ()). Intvalue ());
}
}


where the Getrefactoring () method is inherited from Refactoringwizardpage, because our Refactoringwizard class loads the Refactoringwizardpage and refactoring classes, This method is obtained from the Refactoringwizard class, where the observer design pattern is used. So far, we have completed the refactoringwizardpage extension.

The second step is to extend the Refactoringwizard: first we create a new class Annotationrefactoringwizard, it needs to inherit the Refactoringwizard class, In this class we only need to load the well-defined Annotationrefactoringwizardpage class and the Annotationrefactoring class, and of course the complex processing has already been handled by the Refactoringwizard framework. Below we load the refactoring class in the constructor:

Listing 17

Public Annotationrefactoringwizard (Refactoring Refactoring) {
Super (refactoring, wizard_based_user_interface);
}


Then we load our Annotationrefactoringwizardpage class and simply overload the Adduserinputpages () method of the parent class Refactoringwizard to:

Listing 18

protected void Adduserinputpages () {
page = new Annotationrefactoringwizardpage ("Refactor annotation");
AddPage (page);
}


Step three, start Refactoringwizard. After you extend the Refactoringwizard, you need to pop this dialog when the user clicks on the menu item or button. Refactoringwizard It is best to use the Refactoringwizardopenoperation class to open (Refactoringwizarddialog, of course). Refactoringwizardopenoperation first to refactor the initial check, then open the Refactoringwinzard dialog box, or you will open the error dialog box. When we finished creating the plug-in project, we mentioned that the code that pops up the Refactoringwizard dialog box should be placed in the run function of the class that responds to the menu action. In this project, the following code is put into the annotationmanageaction run function. The code first constructs the refactoring and Refacoringwizard subclass Annotationrefactoring and Annotationrefactoringwizard in turn, and pass the annotationrefactoring reference to Annotationrefactoringwizard, Then use Refactoringwizardopenoperation to open the Annotationrefactoringwizard, Pop-up wizard dialog box.

Listing 19

public void Run (IAction action) {
Shell shell = Window.getshell ();

Annotationrefactoring refactor = new annotationrefactoring (select);
Annotationrefactoringwizard wizard = new Annotationrefactoringwizard (refactor);
Refactoringwizardopenoperation op = new Refactoringwizardopenoperation (wizard);
try {
Op.run (Shell, "inserting @Override Annotation");
catch (Interruptedexception e) {
E.printstacktrace ();
}
}


   Summary

The effective use of refactoring in eclipse can greatly reduce the workload of software developers and improve the robustness of software. However, the current reconstruction is still in an era of lack of tools. In eclipse, for example, only the refactoring tools provided by JDT are the most complete, and the development environments for other languages, such as C + +, Python, lack the corresponding refactoring capabilities. Through the methods provided in this article, we can effectively use the refactoring framework in eclipse to create new refactoring to further improve the efficiency of the existing development environment.

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.