Importing the implementation The banking model is provided in a JAR file that you can import into the ItsoProGuideJava project, which becomes our utility project: ? Select the ItsoProGui
Trang 1 The TransRecord class provides no special methods.
The Bank class provides:
– Customer getCustomer(customerID)—retrieve customer information – Account getAccount(accountID)—retrieve account information – Account[] get Accounts(customerID)—retrieve the accounts of one customer as an array
– TransRecord[] getTransactions(accountID)—retrieve the transactions records of one account as an array
– BigDecimal deposit(accountID, amount) – BigDecimal withdraw(accountID, amount) – BigDecimal ransfer(accountID1, accountID2, amount)
The BankingTest facade class provides the same methods as the Bank class and forwards the execution to the Bank class
Importing the implementation
The banking model is provided in a JAR file that you can import into the ItsoProGuideJava project, which becomes our utility project:
Select the ItsoProGuideJava project and Import (context)
Select ZIP file and click Next
Click Browse and locate the file:
\sg246957\sampcode\dev-java\BankingModel.jar
Select Overwrite existing resources without warning
Click Finish
After importing the code you find the five packages in the ItsoProGuideJava project
We will use the ItsoProGuideJava project as a utility project in Chapter 7,
“Developing Web applications” on page 179
Testing the model
To test the model, select the ItsoProGuideJava project and Run -> Run As -> Java Application When prompted, select the BankMain class and click OK Alternatively, you can run the BankMain program using the instruction in “Running your programs” on page 103
The program executes a few of the business methods and displays the output in
Trang 2Programming assists
Application Developer contains a number of Java programming assist features These features are designed to make life easier for both experienced and novice Java programmers by simplifying or automating many common tasks
The following topics are covered:
Pluggable JDK
Java Scrapbook
Code assist
Navigating through your code
Import generation
Tasks view
Refactoring
Code generation actions
Smart compilation
Java search and working sets
Bookmarks
Pluggable JDK
To provide support for different JDK levels and run-time environments, new JREs can be added to the Workbench For each project you can then select which particular JRE you would like to use By default the current version of Application Developer supports the IBM JDK 1.3.1 The corresponding JRE will be used for all projects unless it has been specified differently
See “Installed JREs” on page 39 of Chapter 2, “Setting up your Workbench and workspace preferences” on page 21 for more information regarding the JRE environment of Application Developer and how to add a new JRE
Java Scrapbook
Snippets of Java code can be entered in a Scrapbook window and evaluated by simply selecting the code and running it This feature can be used to quickly test code without having to modify any actual Java source file
These scrapbook pages can be added to any project The extension of a scrapbook page is jpage, to distinguish them from normal Java source files
Tip: Content assist (such as code assist) is also available on scrapbook
pages
Trang 3To create a scrapbook page, select a folder (itso.java) and New -> Scrapbook Page (context) Make sure the correct folder is selected and enter a file name (JavaTest) for the new page (Figure 5-22)
Figure 5-22 Create Java Scrapbook Page dialog Click Finish to create the scrapbook page After the page has been created and opened in the source editor, you can start entering code snippets in it
To test a scrapbook page, we use code similar to the BankingTest class from the banking model (Figure 5-23) The code is available in:
sg246957\sampcode\dev-java\JavaTest.jpage
After you have added the code, you can run one of the snippets by selecting the code and Run Snippet (context) or click the Run the Selected Code icon in the toolbar The result are displayed in the Console view
Tip: All class names in a scrapbook page must be fully qualified or you have to
set import statements:
Select Set Imports from the context menu anywhere in the scrapbook
For our example, select the itso.bank.model, itso.bank.util, and java.util packages
Trang 4Figure 5-23 Scrapbook code sample
You can also select Display from (context) to display the result expression or
Inspect to bring up the Expressions view, which allows you to inspect the result like a variable in the debugger (see Chapter 16, “Testing and debugging” on page 553 for more information about debugging and inspecting)
Change one of the account numbers for the transfer in snippet 2 to an invalid account and run the code again The debugger opens when the exception is
// - snippet 1
Bank bank = Bank.getInstance();
for (int i=101; i<107; i++) {
String id = String.valueOf(i);
Customer customer = bank.getCustomer(id);
System.out.println("Customer: " + id + " " + customer.getLastName()); Account[] accounts = bank.getAccounts(id);
for (int j=0; j<accounts.length; j++) {
Account account = accounts[j];
System.out.println(" - Account: " + account.getId() + " "
+ AmountConverter.fromDecimal(account.getBalance()));
}
}
// - snippet 2
Bank bank2 = Bank.getInstance();
itso.bank.facade.BankingTest banking = new itso.bank.facade.BankingTest(); String acct1 = "101-1001";
String acct2 = "101-1002";
System.out.println("Transfer 33 from " + acct1 + " to " + acct2);
banking.transfer(acct1, acct2, new java.math.BigDecimal("33"));
System.out.println("Account: " + acct1 + " "
+ AmountConverter.fromDecimal(banking.getAccount(acct1).getBalance())); TransRecord[] tx = banking.getTransactions(acct1);
for (int j=0; j<tx.length; j++) {
TransRecord tr = tx[j];
System.out.println(" - Tx: " + tr.getTimeStamp() + " " +
tr.getTransType() + " "+
AmountConverter.fromDecimal(tr.getTransAmt()));
}
Note: You cannot run code in a scrapbook page until you have at least one
statement selected
Trang 5Code assist
When writing Java code, you can use the code assist feature of the Application Developer to display classes, methods, fields and variables that are valid to use
in the current context In the example in Figure 5-24, you want to use the method from the SQL Connection class to create a new SQL statement, but you cannot remember the exact name and parameters To see all valid methods, position the cursor at the point where you want to insert the method call and press
Ctrl-Space
A window is displayed and lists all methods and fields available for this object In addition, an infopop window is displayed, which shows the Javadoc associated with the selected item To insert a call to the method createStatement, simply double-click the method’s name or press Enter on your keyboard
Figure 5-24 Code assist feature The Java editor also supports syntax highlighting and hover help for the Java code, which displays the Javadoc associated with the selected code
Application Developer also provides templates, which helps the user to add occurring source code patterns For more information regarding templates see
“Templates” on page 43
Trang 6Navigating through your code
By default, the Java perspective contains an Outline view on the right hand side
of the Java source editor (Figure 5-25) The Outline view displays an outline of a structured file that is currently open in the editor area, and lists structural elements The contents of the Outline view are editor-specific For a Java source file (our example), the structural elements are classes, fields, and methods
By selecting elements in the Outline view, you can navigate to the corresponding point in your code This allows you to easily find methods and field definitions without scrolling the editor window
Figure 5-25 Using the Outline view for navigation
The Package Explorer view, which is available by default in the Java perspective, can also be used for navigation (Figure 5-26)
The Package Explorer view provides you with a Java-specific view of the resources shown in the Navigator The element hierarchy is derived from the project's build paths
Tip: If you have a source file with many fields and methods, you can use the
Show Source of Selected Element Only icon from the toolbar to limit the edit view to the element that is currently selected in the Outline view
Trang 7Figure 5-26 Package Explorer view
Import generation
The Application Developer Java Editor simplifies the task of finding the correct import statements to use in your Java code
Simply select the type name in the code and select Add Import from the context menu If the type name is unambiguous, the import will be pasted at the correct place in the code If the type exists in more than one package, a window with all the types is displayed and you can choose the correct type for the import statement
Figure 5-27 shows an example where the selected type (Statement) exists in several packages Once you have determined that the java.sql package is what you want, double-click the entry in the list and the import statement is generated
in the code
Trang 8Figure 5-27 Import generation You can also add the required import statements for the whole compilation unit Open the context menu somewhere in the Java source editor and select Source -> Organize Imports The code in the compilation unit is analyzed and the appropriate import statements are added
You can control the order in which the imports are added and when package level imports should be used through the Preferences dialog See “Organize imports”
on page 42 for details about this feature
Tasks view
The Tasks view displays the following information:
System generated tasks errors
User-defined tasks that you add manually
System generated tasks are typically created by the various builders System generated tasks can be errors, warnings, or information associated with a resource For example, if you save a Java source file that contains syntax errors, the errors will automatically be logged in this view
Trang 9User-defined tasks are global and not related to a particular resource or folder Tasks in this context are similar to an item in a to-do list Any user task can be inserted into the Tasks view and tracked for completion
Figure 5-28 shows an example of the Tasks view with one user-defined task and five system generated tasks (a broken link warning in an HTML file and four compile errors in a Java file)
Figure 5-28 Tasks view Also, the Tasks view can be filtered to show only specific types of tasks For example, you may want to see only error or tasks related to a specific resource For more information on this issue, refer to “Locating compile errors in your code”
on page 108
A typical filter in a Java project would be to display the tasks that apply to any resource in same project (see Figure 5-18 on page 109)
Refactoring
When developing Java applications, it is often necessary to perform tasks such
as renaming classes, moving classes between packages, and breaking out code into separate methods The term refactoring is sometimes used to describe these types of changes In traditional programming environments such tasks are both time consuming and error prone, because it is up to the programmer to find and update each and every reference throughout the project code Application Developer provides functions to automate this process
The Java development tools (JDT) of Application Developer provides assistance for managing refactoring In the Refactoring wizard you can select:
Refactoring with preview—Click Next in the dialog to bring up a second dialog panel where you are notified of potential problems and are given a detailed preview of what the refactoring action will do
Refactoring without preview—Click Finish in the dialog and have the refactoring performed If a stop problem is detected, refactoring is halted and
a list of problems is displayed
Trang 10Table 5-1 shows the refactoring actions available in Application Developer Table 5-1 Refactoring actions
Rename Starts the Rename refactoring wizard Renames the selected
element and (if enabled) corrects all references to the elements (also in other files) Is available on methods, fields, local variables, method parameters, types, compilation units, packages, source folders, projects and on a text selection resolving to one of these element types
Move Starts the Move refactoring wizard Moves the selected elements
and (if enabled) corrects all references to the elements (also in other files) Can be applied on one or more static methods, static fields, types, compilation units, packages, source folders and projects and on a text selection resolving to one of these element types
Pull Up Starts the Pull Up refactoring wizard Moves a field or method to its
super class Can be applied on one or more methods and fields from the same type or on a text selection resolving to a field or method
Modify
Parameters
Starts the Modify Parameters refactoring wizard Changes parameter names and parameter order and updates all references
to the corresponding method Can be applied on methods with parameters or on text selection resolving to a method with parameters
Extract Method Starts the Extract Method refactoring wizard Creates a new
method containing the statements or expressions currently selected and replaces the selection with a reference to the new method
Extract Variable Starts the Extract Variable refactoring wizard Creates a new
variable assigned to the expression currently selected and replaces the selection with a reference to the new variable
Inline Local
Variable
Starts the Inline Local Variable refactoring wizard Replaces the references to the selected local variable with the variable's initializer expression and removes the variable
Self Encapsulate
Field
Starts the Self Encapsulate Field refactoring wizard Replaces all references to a field with getter and setter methods Is applicable to
a selected field or a text selection resolving to a field
Undo Does an Undo of the last refactoring