. Extract Method. The function Refactor > Extract (Web design course)
Sunday, October 21st, 2007. Extract Method. The function Refactor > Extract Method encapsulates the selected code into a new method definition. Eclipse performs a data flow control analysis for the selected code section. From that it determines the parameters and the result type of the new method. The new method is inserted behind the current method, and the selected code is replaced by a corresponding method call. In some cases, however, it is not possible to apply this function, for example, if there are multiple result values of the selected code section. In cases where the function cannot be applied, Eclipse tells you the reason for the rejection. Here is an example. In the following method we select the bold line and apply the Extract Method function: public static void main(String[] args) { System.out.println(”Hello World”); System.out.println(”Hello Eclipse”); } In the dialog that appears, specify helloEclipse as the name for the new method, and you will receive the following: public static void main(String[] args) { System.out.println(”Hello World”); helloEclipse(); } public static void helloEclipse() { System.out.println(”Hello Eclipse”); } This function detects all occurrences in the current compilation unit where such a substitution can be applied. You can apply the substitution to the current selection only or to all matching occurrences. Vice-versa, you can resolve methods by applying the function Refactor > Inline. Factory . Introduce Factory. Using the function Refactor > Introduce Factory … you can generate a static factory method from a given constructor. At the same time, all calls to this constructor are replaced by calls to the new factory method. Types and Classes . Extract Interface.With the function Refactor > Extract Interface you can generate a corresponding interface for an existing class. For example, if you select the class name HelloWorld and invoke this function, you are asked for a name for the new interface. If you enter IHelloWorld and press OK, a Java interface IHelloWorld is generated and the class definition of HelloWorld is completed with the clause implements IHelloWorld. In addition, Eclipse determines which references to HelloWorld can be replaced with a reference to the interface IHelloWorld. As it happens, the interface generated in this example is empty, because the class HelloWorld contains only static methods. . Generalize Type. When you select a type name and invoke this function, a dialog with the hierarchy of supertypes appears. You may select one from the tree to replace the originally selected type name. 40 Chapter 2
Need a managed web hosting provider to help maintain your website? Our web hosting service is the preferred choice of thousands of demanding customers.