Professional Documents
Culture Documents
1. 2. 3. 4. 5. 6. 7. Overview Installing and Running Building and Running Java Classes Refactoring Lecture 2 Debugging Testing with JUnit Version Control with CVS
v1.6
08/02/2006
Building and Running Java Classes Developing Java applications Projects, packages, classes Browsing Java code Searching Java code Organizing Java code Using Code Assist Running Java applications Scrapbook
Refactoring Debugging Testing with JUnit Version Control with CVS
v1.6 08/02/2006
4. 5. 6. 7.
When activated, code assist opens a list of available code completions Code Assist activates by Crtl+Space
Activates automatically when a message needs to be sent to an object (after the dot is typed)
v1.6
08/02/2006
v1.6
08/02/2006
Searching for Java Classes When developing Java application a good search mechanism is very important
You often search for class, method declarations, and references It is important to be able to find things quickly
Eclipse Java Search mechanism is very extensive It allows you to search for:
Types, methods, constructors, packages, and fields Declarations, Implementers, References In the scope of Workspace, Working Set, or Selected Resources
v1.6 08/02/2006
v1.6
08/02/2006
Console View
Represents standard Java console Opens by default when standard Java output is used
Can also be open from Window Show View menu System.out.println("Hello World");
v1.6
08/02/2006
Scrapbook
Executed
v1.6
08/02/2006
Scrapbook
v1.6
08/02/2006
Scrapbook
To open the scrapbook page just click on the resource It opens up like a Java source file editor Type Java code and select the context menu to Display or Inspect
v1.6
08/02/2006
10
Building and Running Java Classes Class names must be fully qualified in what you type Set imports to make life easier Think of your scrapbook as a page that Eclipse will take the source you type, wrap it in a class with your source in the main menu, then compile and execute
Scrapbook
v1.6
08/02/2006
11
Summary
v1.6
08/02/2006
12
Exercise 2
ssalCweN raVm
Add a private integer field (member variable) to the class . .
v1.6
08/02/2006
1. 2. 3.
v1.6
08/02/2006
ppAgE
ssalCweN
dleiFtnirp
Call the
routine of the
object.
13
14
ssalCniaM
)(niam
ssalCweN
in the
routine of the
raVm
dleiFtnirp
dleiFtnirp
raVm
Add a constructor method to accept an integer to initialize the private integer field
ppAgE
715csc
ssalCweN
in the project
Refactoring
Eclipse comes with extensive support for organizing and refactoring Java code It is possible to:
Generate getters and setters for the fields Organize missing import statements Move fields, methods, classes Rename methods, classes, packages
v1.6
08/02/2006
15
Refactoring
What is Refactoring?
v1.6
08/02/2006
16
Refactoring
Why Refactoring?
Methods might no longer do (only) what their name suggests. Functionality that should be in two different classes might be in the same class. Functionality that should be in one class might be duplicated in two or more classes. Improve the design of existing code. Gain a better understanding of the code.
v1.6 08/02/2006
17
Refactoring
Example
18
Refactoring
Example
Of course, to print out a bill for a customer, we also need to include incidentals and taxes
v1.6
Refactoring
Better: Changing the name of the method (for example, calculateCustomerCharge calculateCustomerCharge). Does this fix the problem?
No, We also need to change the name at all call sites. We need to update the documentation. If this method overrides a method in another class, the other name may need to be changed too. Ditto if this method implements an interface.
v1.6
;egrahCmoor nruter ...xat eht etupmoc ot edoc fo senil lareves... egrahc eht ot moor eht rof xat eht dda won// ... slatnedicni pu dda ot edoc ... egrahCmoor ot slatnedicni eht dda won//
08/02/2006
19
Example
08/02/2006
20
10
Refactoring
Example
We defined additional methods to compute incidentals, tax, etc. In order to do this, we added local variables for the quantities that are being calculated in the new methods. Some pre-existing local variables ended up being parameters to the new method. The returned value is different from what was returned in the preexisting method.
v1.6
21
Refactoring
Common Refactorings
Rename
Methods, Fields, Packages, Projects, Parameters, or Local Variables
Encapsulate Field (generate getter and setter) Pull up a Field or Method (into superclass) Push down a Field or Method (into subclass) Extract Method, Local Variable, or Constant from an Expression Change Method Signature
v1.6
08/02/2006
22
11
Refactoring
In a Java view showing methods (e.g., the Outline view) select the method to be renamed. From the view's pop-up menu, select Refactor Rename, or select Refactor Rename from the global menu bar or In a Java editor, select a reference to or the declaration of the method to be renamed. From the editor's pop-up menu, select Refactor Rename, or select Refactor Rename from the global menu bar. This pops up the Rename Method dialog box. Click Preview to preview the changes, or click OK to perform the refactoring.
v1.6
08/02/2006
23
Refactoring
Encapsulating a Field
The Encapsulate Field refactoring can be used to convert a public instance variable into a private instance variable with accessor functions. Example: Inelegant code
v1.6
} } } } } } ;)eman.elpmaxe + " si eman yM"(nltnirp.tuo.metsyS ;)eman.e pmaxe + " s eman yM"(n tn rp.tuo.metsyS ;)eman.ellpmaxe + " sii eman yM"(nlltniirp.tuo.metsyS ;"eoJ" = eman.elpmaxe ;"eoJ" = eman.e pmaxe ;"eoJ" = eman.ellpmaxe ;)(ssalCdleiFcilbuP wen = elpmaxe ssalCdleiFcilbuP ;)(ssa Cd e Fc buP wen = e pmaxe ssa Cd e Fc buP ;)(ssallCdlleiiFciillbuP wen = ellpmaxe ssallCdlleiiFciillbuP {)sgra ][gnirtS(niam diov citats cilbup {)sgra ][gn rtS(n am d ov c tats c bup {)sgra ][gniirtS(niiam diiov ciitats ciillbup {ssalCrehtO cilbup {ssa CrehtO c bup {ssallCrehtO ciillbup
08/02/2006
} } } ; eman gnirtS cilbup ; eman gn rtS c bup ; eman gniirtS ciillbup {ssalCdleiFcilbuP cilbup {ssa Cd e Fc buP c bup {ssallCdlleiiFciillbuP ciillbup
24
12
Refactoring
Encapsulating a Field
} } } } ; ))(emaNteg.elpmaxe + " sii eman yM"(nlltniirp.tuo.metsyS + " s eman yM"(n tn rp.tuo.metsyS + " sii eman yM"(nlltniirp.tuo.metsyS + " s eman yM"(n tn rp.tuo.metsyS ; )"eoJ"(emaNtes.elpmaxe ; )"eoJ"(emaNtes.e pmaxe ; )"eoJ"(emaNtes.ellpmaxe )(ssalCdleiFdetaluspacnE wen = elpmaxe ssalCdleiFdetaluspacnE {)sgra ][gnirtS(niam diov citats cilbup {ssalCrehtO cilbup
v1.6 08/02/2006
} } ;emaNwen = eman ;emaNwen = eman ;emaNwen = eman ;emaNwen = eman {)emaNwen gniirtS(emaNtes ciillbup {)emaNwen gn rtS(emaNtes c bup {)emaNwen gniirtS(emaNtes ciillbup {)emaNwen gn rtS(emaNtes c bup } } } } ;eman nruter ;eman nruter ;eman nruter {)(emaNteg gnirtS cilbup ;eman gnirtS etavirp {ssalCdleiFdetaluspacnE cilbup
25
Refactoring
Select the field in one of the Java views (e.g., Outline, Package Explorer or Members view). From the field's pop-up menu, select Refactor Encapsulate Field , or from the menu bar, select Refactor Encapsulate Field Alternatively, in the Java editor, select the field. From the menu bar, select Refactor Encapsulate Field , or from the editor's pop-up menu, select Refactor Encapsulate Field This pops up the Encapsulate Field dialog. Type the names of the accessor routines in the Getter name and Setter name text fields. Click Preview to preview the changes or Click OK to perform refactoring.
v1.6
08/02/2006
26
13
Refactoring
Pull Up Method
Moves a field or method to a superclass of its declaring class. Suppose you have the same methodor nearly the same methodin two different classes in your system. It may be a good idea to centralize the behavior in a superclass.
Refactoring
} } ... { ) ( e m a Nt e g g ni rt S { n o s r e P s d n et x e e e y o l p m E s s al c ci l b u p } } .. . { ) ( e m a Nt e g g ni rt S { n o s r e P s d n et x e t n e d u t S s s al c ci l b u p
v1.6 08/02/2006
27
Pull Up Method
After the Pull up Method refactoring is applied public class Person { String getName() { ... } }
v1.6
08/02/2006
28
14
Refactoring
In a Java view (e.g., Outline, Package Explorer, Members), select the members that you want to pull up. From the menu bar, select Refactor Pull Up or from the pop-up menu, select Refactor Pull Up. This pops up the Pull up dialog. Select the methods to pull up and their new declaring class. Click Next. Select the methods to be removed in the subtypes after pull up and click Next to review the changes.
v1.6
08/02/2006
29
Refactoring
Reverse of Pull up Method. Moves a set of methods and fields from a class to its subclasses. Can be used when some of the subclasses do not use a method defined in the superclass.
v1.6
08/02/2006
30
15
Refactoring
In a Java view (e.g., Outline, Package Explorer, Members), select the members that you want to push down. From the menu bar, select Refactor Push Down or from the pop-up menu, select Refactor Push Down. The Push Down dialog will open. Click Preview to preview the changes or click OK to perform the refactoring.
v1.6 08/02/2006
31
Refactoring
An expression that occurs in more than one place is replaced with a local variable, whose value is calculated only once. If a program needs to use the same value in multiple places, it can be calculated only once and then used wherever needed. Advantages
Makes the code more efficient. Makes the code more readable. Creates a single point of maintenance for the logic of computing the expression.
v1.6 08/02/2006
32
16
v1.6
08/02/2006
33
Refactoring
Extracting a Method
Creates a new method containing the statements or expression currently selected and replaces the selection with a reference to the new method. Advantages
Code readability Minimize code duplication
v1.6
08/02/2006
34
17
Refactoring
In an editor, select a set of statements or an expression from a method body. From the pop-up menu in the editor, select Refactor Extract Method from the menu bar, select Refactor Extract Method. This opens the Extract Method dialog box. Type the method name in the Method name text field. In the Access Modifier list, specify the method's visibility (public, default, protected, or private). Click Preview to preview the changes or click OK to perform the refactoring.
v1.6 08/02/2006
35
Refactoring
Select the method in a Java view (e.g. Outline, Package Explorer, Members). From the menu bar, select Refactor Change Method Signature or from the method's pop-up menu, select Refactor Change Method Signature. This opens the Change Method Signature dialog box. Use the Access Modifier drop-down to control the method's visibility. Change the method's return type or name by editing the provided text fields. Select one or more parameters and use the Up and Down buttons to reorder the parameters (you can see a signature preview below the parameter list). Use the Add button to add a parameter; you can then edit its type, name and default value in the table.
Switch to the Exceptions tab to add or remove thrown exceptions. Click Preview to preview the changes
v1.6
08/02/2006
36
18
Converting a local variable to a field an anonymous inner class to a nested class a nested type to a top level type Replacing references to a type with references to one of its supertypes a single reference to a type with a reference to one of its supertypes an expression with a method parameter constructor calls with factory method invocations
37
Converting a local variable to a field an anonymous inner class to a nested class a nested type to a top level type Replacing references to a type with references to one of its supertypes a single reference to a type with a reference to one of its supertypes an expression with a method parameter constructor calls with factory method invocations
38
19
Exercise 3
Change the name of the class NewClass that was created in Exercise 2 in the project EgApp to RefactoredClass. Change the name of the method printField in the class RefactoredClass to refactoredPrintField. Review the changes that will be made by Eclipse using the Preview option. Compile and Run the project EgApp.
v1.6 08/02/2006
39
20