Anyone can Reconstruction
Abstract: Eclipse provides a powerful set of automatic reconfiguration (refactoring) functions, which interludes in other functions, so you can rename the Java element, mobile and packet type, create a specific type interface will be nested category into the top category, as well as from the old methods of code fragments removed Analysis of the new method. You are familiar with the Eclipse reconstruction tools, the hands of a good way to increase productivity. In this paper, Overview of the Eclipse Reconstruction, and through an example of the use of these methods and reasons.
Why remodeling?
Reconstruction is in the process function does not change under the premise of changing its structure. Reconstruction is a powerful technology, but the need to implement more carefullyæ‰è¡Œ. The main risk is that we may inadvertently introduce some mistakes, especially in the case of manual reconfiguration. This triggered a dangerous technology on the Reconstruction of the general criticism: When the code will not collapse when Why should we change it?
You need to code may be the reasons for the Reconstruction of the following: legends of the reason is that: the need for a succession of old products and the development of age-old code, or suddenly encounter these codes. The initial development team has been dead. We must increase the creation of a new version of the new software, but they have been unable to understand the code. New development team to work day and night, and then deciphering the code mapping code, a lot of planning and design, people will be the code division to pieces. After many tribulations, all these things are in accordance with the requirements of the new version of the reparations. This is the heroic story of the reconstruction, almost no one in the experience of those living on after such stories.
There is also a realistic situation is a new item added to the demand, the need for design changes. As is because, in the initial planning process for carelessness, or due to the iterative development process (such as Agile development, or test-driven development) in the development process for intentional introduction into the needs of both the substantive and not distinction. This remodeling of a much smaller, generally involving its content through the introduction of an abstract class or interface type changes to the inheritance relationship, and a partition and re-organization, and so on.
Reconstruction of the last reason is that when there can be automatically reconfigurable tool can be used for a pre-generated code shortcuts - like if you are unable to determine how to spell a word, we can use a spelling This tool check the importation of words. For example, you can use this method of generating prosaic Reconstruction getter and setter methods, once familiar with this kind of tool, it can save you a lot of time.
Eclipse tool for the Reconstruction of a hero has no intention of Reconstruction - this scale for almost no tools - but whether or not use Agile development, the Eclipse tool for the average programmer to amend the work in the code impossible to measure the value of . After all, as long as the operation of any complexity can be automatic, and can be a less depressed. Eclipse to achieve as long as you know what kind of remodeling tools, and their understanding of the application, your productivity will be greatly improved.
Of the code to reduce the risk of damage, there are two important ways. The first method is the code for a complete and thorough unit testing: both before and after reconstruction must pass this test. The second method is to use automated tools to carry out reconstruction, for instance, the reconstruction of Eclipse.
Will be thoroughly tested and integrated automation reconstruction will be more effective, such remodeling it from a mysterious art has become a useful tool for the day-to-day. In order to add new functions or improve the maintainability of the code, we can not affect the original code in the function on the basis of quickly and safely change its structure. This capability will design and develop your code greatly influence the way, even if you do not combine their official Approach to have no relationship.
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
Eclipse of the type of reconstruction
Reconstruction of Eclipse tools can be divided into three categories (Below is the order of these tools Refactoring menu in the order):
- Re-naming of the code as well as the code changes the physical structure, including property, variables, as well as interface to rename category, there are categories such as mobile and packet.
- Changes in the code of a logical structure, including nested anonymous for the type of change will be nested class into the top category, according to specific categories to create interfaces, as well as from a class or method will be moved to the sub-categories or attributes Father category.
- Change a category within the code, including local variables into a category of properties, a method selected will be part of the code into an independent, as well as properties generate getter and setter methods.
Several tools can not be fully reconstructed into three types, in particular Change Method Signature, but in this paper or this tool will be included in the third category. Apart from such exceptions, this paper the following sections are in accordance with the above order to discuss reconstruction Eclipse tools.
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
Physical restructuring and renaming
Obviously, even if you did not have any particular tool can also rename the file system is a mobile document or documents, but if the operation is targeting Java source code files, you will need to edit many of the documents, including the import or update package statement. A similar, with a text editor to search and replace functions can be easily addressed to the category, methods and re-named variables, but this time we must be very careful, because different classes may have names similar to the method or variable ; If the project from start to finish inspection of all the documents to ensure that every thing's logo and revising the correctness, it can be really boring.
Rename and the Eclipse tools can be very clever Move in the entire project completed by the middle of such changes, without the need for user intervention. This is because the Eclipse can understand the semantic code, which can be identified on a particular method, variable or class name are cited. Simplify this task to ensure that, like the names of variables and clear instructions to its use.
We often can be found code name is not appropriate or easily misunderstood, because the code with the original design features vary. For example, in a document to find a specific word in the procedure may be extended to the Web page URL access through InputStream operation. If the input flow initially called the file, then it should amend its name to reflect its new, more general characteristics, say sourceStream. Developers often fail to successfully amend these names, because the process is very confusing and dull. This, of course, the next one will be forced to operate on these categories of developers confused.
Elements of Java to a re-naming, should simply from the Package Explorer view click on the elements, or from Java source code for the selected elements in the document, and then select menu items Refactor> Rename. In the dialog box, enter a new name, and then choose whether or not to Eclipse also need to change the name of the application. The actual show the exact contents of your choice and the types of elements. For example, if you select the attributes of getter and setter methods, then it can be updated at the same time the names of these methods, to reflect the new attributes. Figure 1 shows a simple example.
Like all the Eclipse reconstruction operation, when you specify to the implementation of all the necessary information after the remodeling, you can click the Preview button, and a dialog box in contrast to what Eclipse intend to change, you can reject or confirm respectively every document affected by the changes in each one. If you have the correct execution of Eclipse have confidence in the ability to change it, you can only press the OK button. Obviously, if you are not sure what has been done remodeling in the end things, you would like to preview what will be, but the Rename and Move such a simple remodeling, usually do not have the necessary preview.
Move operation and Rename very similar: you choose a Java elements (usually a), designated for a new position, and whether the need to update the definition used. You can then choose Preview inspection changes, or select OK immediate implementation of reconstruction, as shown in Figure 2.
In some platforms (especially Windows), you can also Package Explorer view through simple drag-and-drop method of the package or from one folder to another folder or packet. All references will be automatically updated.
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
Redefinition of the relationship between the
Eclipse of the Reconstruction of a large number of tools that enable you to automatically change the relationship between the categories. Reconstruction of these tools do not provide the Eclipse other tools so common, but of great value because they can perform very complex task. It can be said that the time when they are useful, will be very useful.
Anonymous and upgrading of nested class
Convert Anonymous Class (conversion anonymous) and Convert Nested Type (conversion nested) two more similar Reconstruction Method, which will be a category of its current move to contain the scope of this category.
Anonymous is a grammatical category Sketch marker, which enables you to the need to achieve a certain abstract class or interface to create a local instance of the class, without the need to provide explicit type name. For example, in the creation of the user interface sniffers, they often use anonymous category. 1 in the list, in other assumptions Bag is the definition of a local interface, which states the two approaches, get () and set ().
List 1. Bag category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class BagExample (void processMessage (String msg) (new Bag Bag bag = () (Object o; public Object get () (return o;) public void set (Object o) (this.o = o;)); bag . set (msg); MessagePipe pipe = new MessagePipe (); pipe.send (bag);))
</ Td> </ tr> </ table>
When the anonymous class become great, the code difficult to read the time, you should consider this type become anonymous in the strict sense; to maintain encapsulation (in other words, it is hidden, making do not have to know It external category do not know it), you should make it a nested class, rather than the top category. You can be anonymous in this type of internal click, and then select Refactor> Convert Anonymous Class to Nested on it. When a confirmation dialog box, enter a name for this category, such as BagImpl, then select OK or Preview. In this way, such as the code becomes a list of 2 shows the situation.
List 2. After remodeling category Bag
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class BagExample (private final class BagImpl implements Bag (Object o; public Object get () (return o;) public void set (Object o) (this.o = o;)) void processMessage (String msg) (Bag bag = new BagImpl (); bag.set (msg); MessagePipe pipe = new MessagePipe (); pipe.send (bag);))
</ Td> </ tr> </ table>
When you want to use a certain other category nested class, Convert Nested Type to Top Level very useful. For example, you can use one type of object, like the above category as BagImpl. If you decide later in a number of categories should be shared between the data, then reconstruction operations can be nested from the creation of a new category of documents. You can source selected categories highlighted in the document name (or in the Outline view click on the category name), then select Refactor> Convert Nested Type to Top Level, thereby bringing about Reconstruction.
Reconstruction of this case require you to provide a name loaded. Reconstruction tools will be provided, the proposed names, such as example, you can accept this name. The name on the meaning clear in a moment. Click OK, it will become the outer BagExample category list 3 shows the victims.
List 3. After Reconstruction of the Bag
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class BagExample (void processMessage (String msg) (Bag bag = new BagImpl (this); bag.set (msg); MessagePipe pipe = new MessagePipe (); pipe.send (bag);))
</ Td> </ tr> </ table>
Please note that when a category is nested class when it can be to visit their members of the outer class. In order to retain this function, remodeling process will be put into a category BagExample examples that nested on the previous category. This is required before you enter the name of the instance variables. At the same time created this instance variables used to set the constructor function. Reconstruction of the process to create a new category 4 BagImpl as shown in the list.
List 4. BagImpl category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Final class BagImpl implements Bag (private final BagExample example; / ** * * @ paramBagExample / BagImpl (BagExample example) = (this.example example; / / TODO Auto-generated constructor stub) Object o; public Object get () (return o;) public void set (Object o) (this.o = o;))
</ Td> </ tr> </ table>
If you have the same situation with this example, do not retain BagExample visit, you can safely delete the instance variables and the constructor function will be BagExample category code without changing the default constructor function parameters.
Inheritance, in the category of Mobile
There are also two remodeling tools, and Push Down Pull Up, respectively, to achieve the type of the methods or attributes from one category to move to the father of her son type or category. Suppose you have an abstract entitled Vehicle category, as defined list of 5 below.
5 list. Abstract Vehicle category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public abstract class Vehicle (protected int passengers; protected String motor; public int getPassengers () (return passengers;) public void setPassengers (int i) (passengers = i;) public String getMotor () (return motor;) public void setMotor ( String string) (motor = string;))
</ Td> </ tr> </ table>
You have a Vehicle sub-categories, called Automobile category, as shown in the list 6.
List 6. Category Automobile
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class Automobile extends Vehicle (private String make; private String model; public String getMake () (return make;) public String getModel () (return model;) public void setMake (String string) (make = string;) public void setModel (String string) (model = string;))
</ Td> </ tr> </ table>
Please note that the attribute is a Vehicle motor. If you know that you will always deal only with the car, then just do so; but if you will allow a rowing or anything like that, then you need to motor attributes from the decentralization of Automobile Vehicle category. To that end, you can select the motor Outline view, and then select Refactor> Push Down.
Eclipse is very smart, it knows that you can not always simply a mobile attribute itself, it is also provided Add Required button, but in Eclipse 2.1, this function did not always work correctly. You need to test to see all this depends on whether the method attribute blame to the next level. In this case, there are two such methods, that is associated with the motor getter and setter methods, as shown in Figure 3.
In accordance with the OK button, motor and property getMotor () and setMotor () method will be moved to the Automobile category. 7 shows the list in this category Automobile Reconstruction after the case.
List 7. After remodeling Automobile category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class Automobile extends Vehicle (private String make; private String model; protected String motor; public String getMake () (return make;) public String getModel () (return model;) public void setMake (String string) (make = string; ) public void setModel (String string) (model = string;) public String getMotor () (return motor;) public void setMotor (String string) (motor = string;))
</ Td> </ tr> </ table>
Pull Up Reconstruction and Push Down almost the same, of course, Pull Up is a class of members from his father moved in the category, rather than sub-category. If you later change your mind, or decided to motor back to the Vehicle category, then you would probably use this reconstruction. You also need to be reminded, we must determine whether you choose all the necessary members.
Automobile class members with motor, which means that if you create another sub-category, for example, Bus, you also need to motor (and related methods) into the Bus category. There is a method can be said that such relations, namely, the creation of a named Motorized interface, Automobile and Bus to realize this interface, but do not realize RowBoat.
Motorized interface to create the simplest way is to use the Extract Interface Automobile Reconstruction. For this reason, you can choose Automobile Outline view, and then from the menu select Refactor> Extract Interface. You can pop up the dialog box, select the interface you want included in the method shown in Figure 4.
Click OK, to create a good interface, such as the list of 8 below.
8 list. Motorized Interface
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public interface Motorized (public abstract String getMotor (); public abstract void setMotor (String string);)
</ Td> </ tr> </ table>
At the same time, the Automobile category statement also look into the following:
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class Automobile extends Vehicle implements Motorized
</ Td> </ tr> </ table>
Father of use
Reconstruction of this type of tool is the last one User Supertyp Where Possible. Imagine a used car Xizhang management applications. It has the use of Automobile type of object. If you want to deal with all types of transport, then you can use this reconstruction will be all right Automobile citations have become the Vehicle references (see Figure 5). If you use instanceof operator in the code implementing any type of inspection, you will need to decide in these areas covered by the father of the original type or category, and then select the first option, "Use the selected supertype in 'instanceof' expressions" .
Father of the use of such demand in the Java language often occurs, especially in the use of a Factory Method mode of circumstances. This pattern typical of the ways is to create an abstract class, which create a static method (), this method is to achieve a return of the abstract category of a specific target. If the need to create specific object depends on the type of realization of the details, and calls for achieving the details of the case are not interested, you can use this model.
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
Change of the internal code
Reconstruction is the largest category of internal code to achieve the reorganization of the reconstruction. Reconstruction in all the methods, only the introduction of these methods allows you to remove or intermediate variables, according to the original method of code to create new methods, as well as attributes to create getter and setter methods.
Extraction and embedded
Some Extract reconstruction method is the beginning of the word: Extract Method, Extract Local Variable and Extract Constants. The first Extract Method means you may have guessed, and it's based on your chosen method of the creation of a new code. We list in that category of eight main () method as an example. It first made a command line option values, if any - the beginning of the D options, it will be in their name - in the form of the value stored in a Properties object.
8 list. Main ()
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Import java.util.Properties; import java.util.StringTokenizer; public class StartApp (public static void main (String [] args) (Properties props = new Properties (); for (int i = 0; i <args.length; i + +) (if (args [i]. startsWith ( "-D")) (String s = args [i]. substring (2); StringTokenizer st = new StringTokenizer (s, "="); if (st.countTokens () == 2) (props.setProperty (st.nextToken (), st.nextToken ());))) / / ...)) continue
</ Td> </ tr> </ table>
Will be part of the code from one of the ways out and put in another way, there are two reasons. The first reason is that this approach is too long, and completed more than two entirely different logic operations. (We do not know that the above main () method to deal with the things, but from the view of the evidence available now, this is not an extract from which the grounds.) Another reason is that there is a clear logic code, This code reuse can be other ways. For example, at certain times, you find yourself in many different ways in the preparation of the same repeat a few lines of code. It may be a need for reconstruction, but unless we really need this part of reusable code, or you may not execute very Reconstruction.
Suppose you also need a place in the analysis of other - of right, and put it in the Properties object, then you can and will contain the following statement StringTokenizer if Statements This code extracted. To that end, you can highlight selected this code, and then from the menu select Refactor> Extract Method. You need input method name, here addProperty input, and verify the parameters of the two methods, Properties prop and Strings. 9 shows a list extracted from Eclipse addProp () method after the class situation.
List 9. Extracted from the addProp ()
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Import java.util.Properties; import java.util.StringTokenizer; public class Extract (public static void main (String [] args) (Properties props = new Properties (); for (int i = 0; i <args.length; i + +) (if (args [i]. startsWith ( "-D")) (String s = args [i]. substring (2); addProp (props, s);))) private static void addProp (Properties props, String s) (StringTokenizer st = new StringTokenizer (s, "="); if (st.countTokens () == 2) (props.setProperty (st.nextToken (), st.nextToken ());)))
</ Td> </ tr> </ table>
Extract Local Variable reconstruction be used directly remove a section of the expression, then this expression first assignment to a local variables. Then use that expression in the original use of this variable. For example, in the above method, you can highlight the selected st.nextToken () call first, and then select Refactor> Extract Local Variable. You will be prompted for a variable name, the importation of key here. Please note that there is one will be selected expression for all local variables are replaced by the application of the new options. This option is usually applied, but here nextToken () method does not apply, because this method (apparently) in each of the call at all times to return to a different value. Confirm this option not selected. See Figure 6.
Next, in the second call st.nextToken () local repeated remodeling, this time the call is a new local variables value. This list shows the 10 twice after Reconstruction code situation.
List 10. Reconstruction after the code
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Private static void addProp (Properties props, String s) (StringTokenizer st = new StringTokenizer (s, "="); if (st.countTokens () == 2) (String key = st.nextToken (); String value = st . nextToken (); props.setProperty (key, value);))
</ Td> </ tr> </ table>
In this way there are several benefits of the introduction of variables. First, by providing a meaningful expression to the names of the implementation of the code can be made more clear mandate. Second, the code debugging much easier because we can easily check expression of the return of value. Finally, a variable can be used in the replacement of the same expression of a number of examples of circumstances, the efficiency will be greatly enhanced.
Extract Constant and Extract Local Variable similar, but you must choose static constant expression, it will be remodeling tools into the final static constants. This will be hard-coded in the number of strings and removed from the code when the very useful. For example, in the above code, we use "- D" command-line options to define who - value right. First "- D" to highlight selected, select Refactor> Extract Constant, then enter the name of DEFINE as constants. Reconstruction after the code as shown in the list 11:
List 11. Reconstruction after the code
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class Extract (private static final String DEFINE = "D-"; public static void main (String [] args) (Properties props = new Properties (); for (int i = 0; i <args.length; i + +) ( if (args [i]. startsWith (DEFINE)) (String s = args [i]. substring (2); addProp (props, s);))) / / ...
</ Td> </ tr> </ table>
For each of the Extract … kind of reconstruction, there are corresponding Inline … Reconstruction, the Executive contrary operation. For example, if you highlight selected variables in the code above s, select Refactor> Inline …, and then click OK, Eclipse will be calling addProp () when used directly args [i]. Substring (2) of this expression, as follows:
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
If (args [i]. StartsWith (DEFINE)) (addProp (props, args [i]. Substring (2));)
</ Td> </ tr> </ table>
Such than more efficient use of temporary variables, the code has become even more briefly, as the code is easy to read or vague, depending on your viewpoint on the. But generally speaking, no such embedded Reconstruction worth recommending place.
You can replace the variable expression embedded with the same method, a method chosen to highlight, or static final constants, and then from the menu select Refactor> Inline …, Eclipse will replace method code Method Invocation, or the constant use of the replacement value of the constant use.
Packaging attributes
Usually, we believe that the internal structure of objects will be exposed is a bad practice. This is exactly Vehicle categories and sub-categories have protected or private property, and with public setter and getter methods to attribute the reasons for the visit. These methods can be used in two different ways automatically generated.
The first generation of these methods way is to use the Source> Generate Getter and Setter menu. This will display a dialog box, which contains all outstanding exist getter and setter methods. But because this approach does not use new methods to update these attributes are cited, as there is no reconstruction; when necessary, you must use their own completion of the work of updating. This approach can save a lot of time, but preferably at the beginning of the creation, or to add a new category to attribute the use of the time, because they did not exist when the property used, so there is no need to amend other code .
The second generation getter and setter methods is the way a selected attribute, and then from the menu select Refactor> Encapsulate Field. This way one can only attribute to a generation getter and setter methods, but with the Source> Generate Getter and Setter On the contrary, will be used to change the attributes of the new method of paired call.
For example, we can first create a new version of Jane Automobile category, as shown in the list 12.
List 12. Simple Automobile category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class Automobile extends Vehicle (public String make; public String model;)
</ Td> </ tr> </ table>
Next, create a category of the Automobile examples of the category, and direct access to make attributes, such as shown in the list 13.
List 13. Example of the Automobile
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class AutomobileTest (public void race () (Automobilecar1 = new Automobile (); car1.make = "Austin Healy"; car1.model = "Sprite" / / ...))
</ Td> </ tr> </ table>
Packaging now make attributes. To highlight selected attribute name, and then select Refactor> Encapsulate Field. In the pop-up dialog box input getter and setter methods name - If you expected, the default method is the name getMake () and setMake (). You can also choose with this property in the same category method is direct access to the attributes, or as other types of methods that use these visits. (There are some people who are inclined to use these two methods of a certain kind, but by chance in such circumstances you choose a way which did not, because of Automobile not make attributes are cited.)
Click OK, make the Automobile category would become the property of private property, and also has a getMake () and setMake () method.
>
List 14. After remodeling Automobile category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class Automobile extends Vehicle (private String make; public String model; public void setMake (String make) = (this.make make;) public String getMake () (return make;))
</ Td> </ tr> </ table>
AutomobileTest category must also be updated to use the new access methods, such as shown in the list of 15.
> List of 15. AutomobileTest category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class AutomobileTest (public void race () (Automobilecar1 = new Automobile (); car1.setMake ( "Austin Healy"); car1.model = "Sprite" / / ...))
</ Td> </ tr> </ table>
Change the method signature
This paper presents the final reconstruction is the most difficult to use: Change Method Signature (change method signature). This method features obvious - change method parameters, and return values of that type. And a call for such a change this method of code or other means would have any effect on, it would not be so obvious. There is no such a Rubik's cube. If the code change in the method of remodeling triggered internal problems - undefined variable, or type does not match - Reconstruction of operation will mark these issues. You can choose to accept reconstruction, later corrected these problems, or cancel reconstruction. If this reconstruction in other ways in the lead, directly overlook these problems, you must personally in the reconstruction after the revision.
To clarify this point, consider the list of 16 listed in the classes and methods.
List 16. MethodSigExample category
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public class MethodSigExample (public int test (String s, int i) (int x = i + s.length (); return x;))
</ Td> </ tr> </ table>
Above this type of test () method is another type of method invocation, as shown in the list 17.
List 17. CallTest method
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public void callTest () (MethodSigExample eg = new MethodSigExample (); int r = eg.test ( "hello", 10);)
</ Td> </ tr> </ table>
In the first category to highlight selected test, and then select Refactor> Change Method Signature. You will see the dialog box shown in Figure 8.
The first option is to change the method of visibility. In this case, the change will be the private or protected so that the second category of callTest () method will not be able to visit this method has. (If these two different categories in the package, will visit the methods set default values will lead to such a problem.) Eclipse during Reconstruction when these issues will not be marked, you have only to choose the appropriate value .
Below is an option to change the return value type. If the return value to float, which will not be marked as wrong, because test () method returns the sentence will be automatically converted into int float. Even so, in the second category of callTest () method can also cause problems because not float into int. You need to test () return value to int, or will callTest () in the r to float.
If the first parameter of type int from a String, then have to consider the same issue. In the process of reconstruction in these issues will be identified because they will be in the reconstruction method of internal source of the problem: no way int length (). If, however, by turning it into a StringBuffer, the problem is not marked, because StringBuffer indeed has a length method (). Of course, this will be callTest () method has caused problems because it calls test () when a String or to transfer any more.
Mentioned earlier, triggered a problem in the reconstruction of the circumstances, regardless of whether the problems were identified, one by one you can to correct these problems to continue. Another way is to amend these first mistake. If you plan to remove the parameters i no longer needed, it may be the first to carry out reconstruction methods used to delete it. This deletion parameters of the process even more smoothly.
Finally a need to explain what is Default Value Options. This option only applies to the value added method signature parameters of the situation. If, for example, we have added a parameter of type String, called n parameters, the default values for the world, then callTest () method call in the test () code to look into the following:
<table Bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"> <tr> <td>
Public void callTest () (MethodSigExample eg = new MethodSigExample (); int r = eg.test ( "hello", 10, "world");)
</ Td> </ tr> </ table>
In this the Change Method Signature remodeling seemingly terrible discussion, which we do not hide the problem, but it has not been mentioned, this reconstruction is a very powerful tool, it can save a lot of time, usually you must carefully plan will be able to successfully use it.
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
Concluding remarks
Eclipse provides the tools to reconstruction simple, familiar with these tools will help you improve efficiency. Agile development methods using iterative process of increase, it was necessary to rely on technology to change the remodeling and expansion of the design process. But even if you did not use a formal request remodeling, the Eclipse tools or remodeling in general can modify the code to provide time-saving method. If you spend some time with these tools, then they can be used when there is the situation in which you will be able to realize that the time spent is worth it.
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
About the author <table border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td colspan="3"> </ td> </ tr> <tr align = " left "valign =" top "> <td>
</ Td> <td> </ td> <td width="100%">
David Gallardo on Studio B is a writer, he is an independent software consultant and writer, international expertise for software, Java Web applications and database development. He has been a professional software engineer with 15 years, he has many operating systems, programming languages and network protocols experience. He recently e-commerce companies in a BtoB TradeAccess, Inc database and international leadership development. Prior to this, he was Lotus Development Corporation's International Product Development Group senior engineer, is responsible for Lotus products (including Domino) to provide Unicode and international language support for cross-platform development. David is Eclipse In Action: A Guide for Java Developers (2003) co-author of the book. David@gallardo.org through contact with David.
</ Td> </ tr> </ table>
<table Border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td>
</ Td> </ tr> </ table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tr align="right"> <td>
<table Border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="middle">
</ Td> <td valign="top" align="right"> </ td> </ tr> </ table> </ td> </ tr> </ table>
↑ Back






