Vikas Manoria IT Specialist – IBM Academic Initiative [email_address] Section -2) Java Development
Section 2 - Java Development (24%)
Create Java projects, packages, classes, and methods
Manage the Java Build Path
Use the Outline view
Use the refactoring features
Use the Java editor features such as content assist and code formatting
Add and organize import declarations
Use the Java search function
Use the Task and Problems views
Use the Hierarchy view
Use the resource and project property dialogues
The Java Perspective 1 2 3 4 5 6 7 8
Java Perspective Toolbar Commands
As an alternative to the New main menu command, you can create Java artifacts using the Java perspective toolbar.
1. Create Java Project 2. Create Java Package 3. Create Java Class a. Create Visual class b. Create Java class c. Create Java interface d. Create Java Enum e. Create Annotation f. Create JUnit Test Case 4. Launch New_configuration (for Code Analysis) 5. Debug Java Application 6. Run Java Application 7. Profile Java Application 8. Run External Tools 9. Open Type 10. Open Search Dialog 1 2 3 4 5 6 7 8 9 10 a b c d e f
Overview of Java Projects
Java projects contain source code and related files needed to create a Java application.
The workbench associates Java projects with an incremental builder that compiles Java code as it is edited.
Java projects also maintain models of their contents.
The model includes information about the type hierarchy, declarations, and references of Java elements.
After creating a new Java project, you can import a JAR file into the project.
Create a New Java Project
Start the Create Java Project wizard.
In the Java perspective, select File > New > Project .
Select Java Project .
Click Next .
Provide a name and location for the new Java project.
Specify a unique project name.
Set the location of the project within the workspace, or in any directory.
Decide to use the default JRE, or use a project-specific JRE.
Decide whether to keep source and class files together or separate.
Click Finish .
Java Build Path: Source and Projects
The Source tab specifies where to store the Java source files and output class files.
The Projects tab adds other projects from the workspace to the build path.
Add a project if the current Java application depends on classes in another project in the workspace.
Java Build Path: Libraries
The Libraries tab adds JAR files and class folders to the build path.
Add a JAR file located in the workspace with the Add JARs button.
Add a JAR file located outside of the workspace with the Add External JARs button.
Add classpath variables with the Add Variable button.
Add a library of Java resources using Add Library button.
Libraries are arbitrarily organized sets of JAR files.
Click the Add Class Folder button to add a directory with class files residing outside of your workspace.
Java Build Path: Order and Export
The Order and Export tab sets the order of JAR files and class folders in the Java build path.
Use the Up and Down buttons to arrange the order within the Java build path.
Select the check box next to an entry to make the source files visible to other projects dependent on the current Java project.
For example, TransferJava has a project reference to AccountJava.
In the Order and Export tab, the AccountJava project is selected.
If a third project, CustomerJava, references TransferJava, it has access to the classes in AccountJava as well.
Import Archived Resources into a Java Project
Start the Import Archived file wizard.
In the Java perspective, select File > Import .
From the Import wizard, select General > Archive file.
Click Next .
Select the source JAR, .zip, or TAR file and destination.
Enter the path filename of the Archive file.
Select one or more files for importing from the Archived file.
Import files by extension using the Filter Types feature.
Use the Into folder to select a
destination Java project.
Create a New Java Package
Start the New Java Package wizard.
In the Java perspective, select File > New > Package .
Enter the source folder and name of the new Java package.
Select a source folder as the root of the Java package structure.
If you do not have any source folders defined in the Java project, the Java project folder itself is the source folder.
Enter the full package name for the Java package.
Create a New Java Class
Start the New Java Class wizard.
Select File > New > Class .
Specify the source folder and package name for the Java class.
Set the name of the new Java class and class modifiers.
Specify a single superclass to extend, and zero or more interfaces to implement.
Set the wizard to generate:
Main method stub
Constructors from superclass
Abstract methods from abstract classes or interfaces
Click Finish .
The Java Editor
Syntax type categorized by color:
Java comments are green; Javadoc comments are cyan.
Keywords and built-in types are magenta.
Strings are navy blue.
Two indicator bars are placed on each side of the editor.
The left indicator bar is proportional to each line of code.
The right indicator bar is proportional to the scale of the entire Java class.
Changes since your last save are denoted by a light purple bar.
A task is a user-defined statement that can be either associated or unassociated.
Associated: Referencing a line in a resource
Unassociated: not related to any resource
A task is associated by opening the resource in an editor, right-clicking the left grey border and selecting Task.
If problems are detected, a message is logged
The message cannot be removed manually.
There are three types of problems:
Errors, Warning, and Information
1 2 3 4
Filtering the Problem View
Use the Filter Tasks wizard to temporarily hide messages in the Problems view.
You must fix the problem to remove it permanently
Since problems are reported by validators, disabling a validator in the project's Properties page will remove the messages it reports.
Examine Errors in the Java Code Editor
Icons on the left indicator column mark special locations.
Blue checkmarks indicate a task item.
Red circles with an ‘x’ indicate a syntax error.
Light bulb icons indicate that a quick fix is available.
Rectangles on the right indicator column display the same locations, in scale with the whole document.
Blue rectangles indicate the relative position of a to-do item.
Red rectangles indicate the relative position of a syntax error.
Hovering over errors
provides you with additional information.
Correct Errors with the Quick Fix Feature
The Quick Fix feature suggests solutions to the highlighted error.
Click the light bulb icon to show a list of suggested fixes.
Click one of the suggestions to preview the applied fix to your Java class.
Double-click the suggestions to add the particular fix to your application.
1 2 3
Code Assist / Content Assist
These features provide suggestions to complete code or script
Available for Java, HTML, JSP, XML and others
Type Ctrl+Space to invoke Code or Content Assist
Automatically triggered when you type a period (.) in a Java code fragment
Depends on Project container context and file type
Additional Content Assist Features
Comment code assist provides a list of:
XDoclet-style annotation tags
Variable name assist suggests a name for a variable based on the type name.
Content Assist Templates
Templates provide a skeleton of common code constructs:
Try/Catch blocks for exceptions
Anonymous inner classes
Displays the relationship between superclasses, subclasses, and implemented interfaces of the current type.
Show the Type Hierarchy displays all superclasses and subclasses of the current type.
Show the Supertype Hierarchy lists all supertypes and implemented interfaces.
Show the Subtype Hierarchy lists all
subtypes of the selected type.
If the current type is an interface, the list
shows all types that implement the interface.
The Javadoc view returns an HTML rendered Javadoc Help for the currently selected resource.
Previews Javadoc comments in your class in HTML format
This view also displays the Javadoc for a class or interface from the J2SE API
The Declaration view reveals the implementation for the currently selected class, method, or field in the Java editor.
This view allows you to quickly browse the underlying implementation for a method call or an object definition.
Bring up the pop-up menu within the Declaration view and select Open to view the referenced class in a new Java editor.
The Java Browsing Perspective 1 2 3 4 5
Start a search by selecting Search from the main menu.
The results of a search can be seen in the Search view.
Previous search results are stored, and can be retrieved in the Search view or Classic Search view.
Java Search performs a search of declarations, references, and other Java elements.
This search type understands the structure of a Java class and a Java interface.
For example, search for all classes that reference the verifyLogon method within the workspace.
Refactoring Java Code
Refactoring code allows you to make application-wide improvements while preserving the behavior of the program.
Developers often perform refactoring to make an application more manageable.
Rational Application Developer V7 and later has built-in support for common refactoring tasks.
The Java development tools will examine your entire application and update any references to your change.
You can choose to preview any changes from refactoring before committing the operation.
Move Java Class to Another Package
Start the Refactor Move wizard.
In the Java perspective, right-click a Java class and choose Refactor > Move .
Click Next .
Specify the new destination for the Java class.
Select a different package, or use the New button to create a new package.
Click Preview to examine the
changes before committing
Refactoring Functions (1 of 4) Function Description Shortcut Undo Reverses the last refactoring operation Alt+Shift+Z Redo Performs a refactoring operation that was previously reversed Alt+Shift+Y Rename Changes the name of a Java element, and updates all references to that element Alt+Shift+R Move Moves a Java element from between classes, interfaces, packages, or projects Alt+Shift+V Change Method Signature Changes the name or parameters of a method, and updates all references to the method Alt+Shift+C Convert Anonymous Class to Nested Provides a name for an anonymous class, thus changing the class into a nested class
Refactoring Functions (2 of 4) Function Description Move Member Type into New File Extracts a nested class and saves it as a separate class Push Down Moves a set of methods and fields from a class to its subclasses Pull Up Moves a set of methods and fields from a class to its superclass Extract Interface Creates a new interface based on a class. Optionally, you can make the source class implement the new interface Generalize Type Replaces a type in a variable, parameter, or field or on a method return type with one of its supertypes Use Supertype Where Possible Replaces all occurrences of a type with one of its supertypes
Refactoring Functions (3 of 4) Function Description Shortcut Inline Replace a method invocation with the implementation of the method itself. For static final fields, replace a reference to the field with the actual value Alt+Shift+I Extract Method Takes a section of code within a method and places it in a new method Alt+Shift+M Extract Local Variable Creates a local variable from an expression. Replaces an expression with a reference to the local variable Alt+Shift+L Extract Constant Creates a static final field from an expression. Replaces the expression with a reference to the static final field Introduce Parameter Within a method, extracts an expression and replaces it with a new method parameter
Refactoring Functions (4 of 4) Function Description Shortcut Introduce Factory Replaces a constructor with a factory method that invokes the constructor Convert Local Variable to Field Replaces a local variable with a field. If the local variable is initialized when it is created, this operation moves the initialization code to the method’s constructor. Alt+Shift+F Encapsulate Field Replaces all references to a field with getter and setter methods
Source Functions (1 of 4) Function Description Shortcut Toggle Comment Adds single line comments ( // ) to each line in the selection Ctrl+/ Add Block Comment Adds block comment tags ( /* */ ) around the selection Ctrl+Shift+/ Remove Block Comment Removes block comment tags ( /* */ ) around the selection Ctrl+Shift+ Shift Right Adds a tab character to the start of each line in the selection Tab Shift Left Removes a tab character from the start of each line in the selection Shift+Tab Format Changes the indentation, line spacing, and braces in the entire Java class according to the code formatter settings Ctrl+Shift+F
Source Functions (2 of 4) Function Description Shortcut Format Element Apply code formatting only to the first Java element in the selection Correct Indentation Adjust the indentation to every line in the current selection Ctrl+I Sort Members Arrange the member order according to the Member Sort Order in Java preferences Organize Imports Adds any missing import statements, and removes any unused import statements Ctrl+Shift+O Add Imports Adds an import statement for the currently selected type. Removes the package name for a qualified type, if possible. Ctrl+Shift+M Override/ Implement Methods Creates one or more methods defined in a superclass or interface
Source Functions (3 of 4) Function Description Shortcut Generate Getters and Setters Creates a get Field and set Field method for one or more fields in the current class Generate Delegate Methods For a field in the current class, creates a wrapper around methods from the field’s class Generate Constructor using Fields Creates a new constructor that initializes one or more fields in the class Add Constructor from Superclass Overrides one or more constructors defined in a superclass Add Javadoc Comment Adds a Javadoc comment and appropriate Javadoc tags for the selection Alt+Shift+J
Source Functions (4 of 4) Function Description Shortcut Surround with try/catch Block Adds a try/catch block around the selection to catch exceptions Externalize Strings Takes all static string declarations and saves them into a separate properties file. Also creates a method to retrieve strings in the properties file. Find Strings to Externalize Given a project, source folder, or package, searches for any strings that have not been externalized Convert Line Delimiters To
Changes the end-of-line indicator in the current document to one of the three formats:
Windows (Carriage return
, line feed
Unix, MacOS (Line feed
Classic MacOS (Carriage return
Preview and Commit the Move Operation
Preview the changes before committing the move operation.
Select one of the items in the Changes to be performed list.
To override a change made by the move operation, clear the check box next to the change list.
Examine the difference in the affected Java artifact using the Original Source and Refactored Source windows.
Click OK to commit all of the selected changes from the move operation.
UML Visualization Overview
The Unified Modeling Language (UML) provides a visual representation of the structures and relationships of elements in applications.
The UML visualization tools in Rational Application Developer V7 and later provides two main functions:
Code Visualization describes a Java application in a graphical format using UML notation.
Visual Editing applies changes in the UML diagram to the actual application code.
To start visualizing your Java application, create a UML diagram and document your application using UML notation.
Create a New Class Diagram File
Start the New Class Diagram wizard.
In the Java perspective, select File > New > Other .
Expand the Modeling folder and select Class Diagram.
Click Next .
Specify the name and location for the new Class Diagram file.
Specify a location for the class diagram file.
Choose a name for the class diagram file.
Click Finish .
Visualize Existing Java Resources
Open the new class diagram file.
Bring up the pop-up menu right-click in the class diagram and select Visualize Existing > Java Type .
Enter the name of the Java types that you want to visualize in the diagram.
Explore relationships between different Java types in the diagram.
Use the incoming and outgoing relationship arrows to create relationships between types.
Relationships created in the diagram affect Java types in your workspace.
Relationships between Java Types
There are four different types of relationships between Java types:
An Extends relationship exists when a class inherits the behavior of another class.
An Implements relationship exists when a class realizes the behavior of an interface.
An Association relationship exists when the objects of one classifier (class or interface) are connected to, and can navigate the objects of another classifier.
For example, the TransferBean class has a field of type AccountBean.
A Dependency relationship exists when a change in one class might cause a change in another class.
For example, the TransferBean class has a method that takes an AccountBean object as a parameter.
«use» +1 AccountBean
Java Project Properties
The Java project properties define how the workbench maintains files within the project:
Builders specify which incremental builds should run in the Java project.
For example, the Java Builder compiles source code into Java applications.
Java Build Path defines where the compiler should search for classes referenced in the Java application.
Java Compiler defines how the workbench compiles classes in the project.
Javadoc Location sets where the workbench should publish Javadoc files.
Validators automatically check the structure and syntax of certain file types, such as XML files.
To open the Java project properties, right-click the Java project and select Properties.
Java Build Path Settings
You can set Java Build Path settings within the New Java project wizard or the Java project properties.
The compiler searches the locations listed in the Java Build Path for the classes referenced by your application.
The Java Build Path properties also allow you to specify the location for the compiled classes and default output .
Workspace Settings in Java Preferences (1 of 2)
Modify the Java preferences to change settings for all of the Java resources in the workspace.
Appearance controls the appearance of Java elements in workbench views.
Type Filters removes Java packages from the Open Type command, code assist, and quick fix.
Build Path sets the default class build path for all Java projects.
Code Style sets conventions for Java element names, spacing, and sort order.
Compiler sets the JDK compliance level, and the severity of compilation problems.
Task Tags set task indicators in Java comments, such as the TODO tag.
Workspace Settings in Java Preferences (2 of 2)
Editor customizes the appearance and behavior of the Java editor.
Installed JREs list the Java run times on which you can run your application.
Which view in the Java perspective allows you to see the relationship between a type, its supertypes, and its subtypes?
What is the advantage of using the Refactor Move feature, as opposed to moving a Java source file manually outside of Rational Application Developer?
In the Java Editor, what information is shown by the left and right status rulers (bars)?
Name two features that add the correct import statement in the Java Editor.
What are the types of problems and the associated icons in the Problems view?
The Type Hierarchy view allows you to view the relationship between a type and its super and subtypes. The UML visualization tools can also diagram the relationship between types.
The Refactor Move command verifies every reference to the resource within the workspace and updates all references. Moving the file outside of the workbench will not update these references.
In the Java editor, the left indicator bar displays the tasks and errors in the same scale as the lines within the editor. The right indicator bar displays the tasks and errors as well, but in scale with the entire file.
The Organize Imports command scans the current Java class and suggests an import statement to fix any classes. The Quick Fix feature also suggests an import statement for a class that the compiler cannot resolve.
Error (red circle, white cross), warning (yellow triangle, exclamation mark), Information (blue exclamation mark)
Create Java Project, package and 2 classes
Problems & Tasks
Quick fix & Run
Other Java views
Java Browsing perspective
Create Class diagram
Source and Refactor options
Japanese Hebrew Thank You English Merci French Russian Danke German Grazie Italian Gracias Spanish Obrigado Portuguese Arabic Simplified Chinese Traditional Chinese Thai Korean