7/30/2019 Overview of Eclipse
1/20
1v1.6 08/02/2006
Overview of Eclipse Lectures
1. Overview
2. Installing and Running
3. Building and Running Java Classes
4. Refactoring
5. Debugging
6. Testing with JUnit
7. Version Control with CVS
3. Building and Running Java Classes
4. Refactoring Lecture 2
2v1.6 08/02/2006
Module Road Map1. Overview2. Installing and Running
3. 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
Scrapbook4. Refactoring5. Debugging6. Testing with JUnit7. Version Control with CVS
7/30/2019 Overview of Eclipse
2/20
3v1.6 08/02/2006
Building and Running Java Classes Using Code Assist
When activated, code assist opens a list ofavailable 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)
4v1.6 08/02/2006
Building and Running Java Classes Using Quick Fix Useful if Java compiler shows errors
Gives options for fixing the errors
Activated through Edit Quick Fix menu option
Error indication
7/30/2019 Overview of Eclipse
3/20
5v1.6 08/02/2006
Building and Running Java Classes
Searching for Java Classes
When developing Java application a good searchmechanism is very important
You often search for class, method declarations, andreferences
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 SelectedResources
6v1.6 08/02/2006
Building and Running Java Classes Running Java Classes To Run Java
application
Choose Run Run from themenu
Standard Javaoutput isdisplayed in the
console window
7/30/2019 Overview of Eclipse
4/20
7v1.6 08/02/2006
Building and Running Java Classes Console View
Represents standard Java console Opens by default when standard Java output
is used
Can also be open from Window Show Viewmenu System.out.println("Hello World");
8v1.6 08/02/2006
Building and Running Java Classes Scrapbook Allows for writing and executing of Java code
Very useful for quick test of Java code that youwrite
The Java code in the Scrapbook can be:
Displayed as a string when evaluated
Inspected when evaluated
Opens an Inspector view where you can see returningobject from evaluation and all containing fields
Executed
7/30/2019 Overview of Eclipse
5/20
9v1.6 08/02/2006
Building and Running Java Classes Scrapbook
It is created by selecting aproject and choosing New Otherfrom the PackageExplorers context menu
This opens up the Newdialog box
Expand Java Expand Java Run/Debug
under Java Select Scrapbook page and
click Next. This will open New
Scrapbook page Enter the name of the page
Your scrapbook page willbecome a resource in yourproject
10v1.6 08/02/2006
Building and Running Java Classes Scrapbook To open the scrapbook
page just click on theresource
It opens up like a Javasource file editor
Type Java code andselect the context
menu to Display orInspect
7/30/2019 Overview of Eclipse
6/20
11v1.6 08/02/2006
Building and Running Java Classes Scrapbook
Class names must be fullyqualified in what you type
Set imports to make lifeeasier
Think of your scrapbook asa page that Eclipse willtake the source you type,wrap it in a class with yoursource in the main menu,then compile and execute
12v1.6 08/02/2006
Building and Running Java Classes Summary You have learned:
How to create projects, packages and classes
How to browse Java code and search for Java
code
How to use coding assistance
How to run Java code
How to use the Scrapbook
7/30/2019 Overview of Eclipse
7/20
13v1.6 08/02/2006
Exercise 2
Create a new class titled and add it to the package in the projectyou have created in Exercise 1.
Add a private integer field (member variable) to the class .
Add a constructor method to accept an integer to initialize the private integer field .
Add a public method that takes no parameters and returns void.
Add code to the method to print the value of the private variable to thestandard output.
Instantiate an object of type in the routine of the .
Call the routine of the object.
Run the project .
What do you see in the Console window?
14v1.6 08/02/2006
Module Road Map1. Overview
2. Installing and Running
3. Building and Running Java Classes
4. Refactoring
Why Refactoring?
Examples
Common Refactorings
5. Debugging6. Testing with JUnit
7. Version Control with CVS
7/30/2019 Overview of Eclipse
8/20
15v1.6 08/02/2006
Refactoring Organizing Java Code
Eclipse comes with extensive support fororganizing 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
16v1.6 08/02/2006
Refactoring What is Refactoring? Refactoring is the process of changing a
software system so that
the external behavior is not altered, but
the internal structure is improved.
Refactoring (http://www.refactoring.com/) is abehavior-preserving transformation.
7/30/2019 Overview of Eclipse
9/20
17v1.6 08/02/2006
Refactoring Why Refactoring?
Methods might no longer do (only) what theirname suggests.
Functionality that should be in two differentclasses might be in the same class.
Functionality that should be in one classmight be duplicated in two or more classes.
Improve the design of existing code.
Gain a better understanding of the code.
18v1.6 08/02/2006
Refactoring Example Consider a method for computing the room
charge for a hotel:public doublepublic doublepublic doublepublic double getRoomChargegetRoomChargegetRoomChargegetRoomCharge()()()()
{{{{
doubledoubledoubledouble roomChargeroomChargeroomChargeroomCharge = 0.0;= 0.0;= 0.0;= 0.0;
... code to compute room charge...... code to compute room charge...... code to compute room charge...... code to compute room charge...
returnreturnreturnreturn roomChargeroomChargeroomChargeroomCharge;;;;
}}}}
What other factors might go into computingthe room charge?
7/30/2019 Overview of Eclipse
10/20
19v1.6 08/02/2006
Refactoring Example
Of course, to print out a bill for a customer, we also need to includeincidentals and taxes
Whats inelegant about this method now?
3 sets of calculations in one function. Method does 3 things. The name is not illustrative of what the method does.
20v1.6 08/02/2006
Refactoring Example Better: Changing the name of the method (for
example, calculateCustomerChargecalculateCustomerChargecalculateCustomerChargecalculateCustomerCharge).
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, theother name may need to be changed too. Ditto if thismethod implements an interface.
This is known as the Rename Methodrefactoring.
7/30/2019 Overview of Eclipse
11/20
21v1.6 08/02/2006
Refactoring Example
Lets refactor our getRoomChargegetRoomChargegetRoomChargegetRoomCharge()()()() method.
What have we done? We defined additional methods to compute incidentals, tax, etc.
In order to do this, we added local variables for the quantities thatare 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 pre-
existing method.
22v1.6 08/02/2006
Refactoring Common Refactorings Rename
Methods, Fields, Packages, Projects, Parameters, orLocal Variables
Encapsulate Field(generate getter and setter)
Pull upa Fieldor Method(into superclass)
Push downa Fieldor Method(into subclass)
Extract Method, Local Variable, or Constantfrom
an Expression Change Method Signature
7/30/2019 Overview of Eclipse
12/20
7/30/2019 Overview of Eclipse
13/20
25v1.6 08/02/2006
Refactoring Encapsulating a Field
After refactoring, we have
26v1.6 08/02/2006
Refactoring Encapsulating a Field Using Eclipse Select the field in one of the Java views
(e.g., Outline, Package Explorer orMembers view).
From the field's pop-up menu, selectRefactor Encapsulate Field , or fromthe menu bar, select Refactor Encapsulate Field
Alternatively, in the Java editor, selectthe field.
From the menu bar, select Refactor Encapsulate Field , or from the editor'spop-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 textfields.
Click Preview to preview the changes orClick OK to perform refactoring.
7/30/2019 Overview of Eclipse
14/20
27v1.6 08/02/2006
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 centralizethe behavior in a superclass.
28v1.6 08/02/2006
Refactoring Pull Up Method After the Pull up Method refactoring is applied
public class Person {
String getName() {
...
}
}
7/30/2019 Overview of Eclipse
15/20
29v1.6 08/02/2006
Refactoring Pull Up Method Using Eclipse
In a Java view (e.g., Outline,Package Explorer, Members),select the members that youwant to pull up.
From the menu bar, selectRefactor Pull Up or from thepop-up menu, select Refactor Pull Up.
This pops up the Pull updialog.
Select the methods to pull upand their new declaring class.Click Next.
Select the methods to be
removed in the subtypes afterpull up and click Next to reviewthe changes.
30v1.6 08/02/2006
Refactoring Push Down Method Reverse of Pull up Method.
Moves a set of methods and fields from aclass to its subclasses.
Can be used when some of the subclassesdo not use a method defined in thesuperclass.
7/30/2019 Overview of Eclipse
16/20
31v1.6 08/02/2006
Refactoring Push Down Method Using Eclipse
In a Java view (e.g.,Outline, Package Explorer,Members), select themembers that you want topush down.
From the menu bar, selectRefactor Push Down orfrom the pop-up menu,select Refactor PushDown.
The Push Down dialog will
open. Click Preview to preview
the changes or click OK toperform the refactoring.
32v1.6 08/02/2006
Refactoring Extracting a Local Variable An expression that occurs in more than one place
is replaced with a local variable, whose value iscalculated only once.
If a program needs to use the same value inmultiple places, it can be calculated only once andthen used wherever needed.
Advantages
Makes the code more efficient. Makes the code more readable.
Creates a single point of maintenance for the logic ofcomputing the expression.
7/30/2019 Overview of Eclipse
17/20
33v1.6 08/02/2006
Refactoring Extracting a Local Variable Using Eclipse
In a Java editor, select theexpression that you want toextract to a local variable.
From the editor's pop-upmenu, select Refactor Extract Local Variable or fromthe menu bar, select Refactor Extract Local Variable.
This will open the ExtractLocal Variable dialog box.
Type the name of the variablein the Variable name textfield.
Click Preview to preview thechanges or click OK to
perform the refactoring.
34v1.6 08/02/2006
Refactoring Extracting a Method Creates a new method containing the statements
or expression currently selected and replaces theselection with a reference to the new method.
Advantages
Code readability
Minimize code duplication
7/30/2019 Overview of Eclipse
18/20
35v1.6 08/02/2006
Refactoring Extracting a Method Using Eclipse
In an editor, select a set ofstatements or an expressionfrom a method body.
From the pop-up menu in theeditor, select Refactor Extract Method from themenu bar, select Refactor Extract Method.
This opens the ExtractMethod dialog box.
Type the method name in theMethod 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 toperform the refactoring.
36v1.6 08/02/2006
Refactoring Change Method Signature Select the method in a Java view (e.g.
Outline, Package Explorer, Members).
From the menu bar, select Refactor Change Method Signature or from themethod's pop-up menu, selectRefactor Change Method Signature.
This opens the Change MethodSignature dialog box.
Use the Access Modifier drop-down tocontrol the method's visibility.
Change the method's return type orname by editing the provided textfields.
Select one or more parameters anduse the Up and Down buttons to
reorder the parameters (you can seea signature preview below theparameter list).
Use the Add button to add aparameter; you can then edit its type,name and default value in the table. Switch to the Exceptions tab to add or
remove thrown exceptions. ClickPreview to preview the changes
7/30/2019 Overview of Eclipse
19/20
37v1.6 08/02/2006
Other Refactorings Supported by Eclipse
Renaming a package
a compilation unit
a type
a local variable
method parameters
Extracting
a constant
an interface from a type
Inlining
a local variable
a method
a constant
static members betweentypes
an instance method to acomponent
Converting a local variable to a field
an anonymous inner class toa nested class
a nested type to a top level
type
Replacing
references to a type with
references to one of itssupertypes
a single reference to a type
with a reference to one of itssupertypes
an expression with a methodparameter
constructor calls with factory
method invocations
38v1.6 08/02/2006
Refactorings Supported by NetBeans (v 5.0) Renaming
a package
a compilation unit
a type
a local variable
method parameters
Extracting
a constant
an interface from a type
Inlining
a local variable
a method
a constant
static members betweentypes
an instance method to acomponent
Converting
a local variable to a field
an anonymous inner class toa nested class
a nested type to a top level
type
Replacing
references to a type with
references to one of itssupertypes
a single reference to a type
with a reference to one of itssupertypes
an expression with a method
parameter
constructor calls with factorymethod invocations
7/30/2019 Overview of Eclipse
20/20
39v1.6 08/02/2006
Exercise 3
Change the name of the class NewClass that wascreated in Exercise 2 in the project EgApp toRefactoredClass.
Change the name of the method printField in theclass RefactoredClass to refactoredPrintField.
Review the changes that will be made by Eclipseusing the Preview option.
Compile and Run the project EgApp.
Top Related