Editing Java elements

In this section, you will edit Java elements in the workbench.

Using the outline view

  1. Expand the package junit.samples and select the file VectorTest.java.  You can open VectorTest.java in the Java editor by double clicking on it.
  2. Notice the syntax highlighting.  Different kinds of elements in the java source are rendered in unique colors.  Examples of java source elements that are rendered differently are:

    Syntax highlighting

  3. Look at the Outline view. It displays an outline of the Java file including the package declaration, import declarations, fields, types and methods. The Outline view uses icons to annotate Java elements.  For example, icons indicate whether a Java element is static, abstract, or final.  Different icons show you whether a method is overridden from a base class (overridden) or when it implements a method from an interface (implements).

    Outline view

  4. Toggle the Hide Fields, Hide Static Members, and Hide Non-Public Members buttons in the Outline view toolbar to filter the view's display.

    Outline view tool bar

  5. You can edit source code by viewing the whole Java file, or you can narrow the view to a single Java element. The toolbar includes a button, Show Source of Selected Element Only, that will cause only the source code of the selected outline element to be displayed in the Java editor.  In the example below, only the setUp() method is displayed.

    Show selected element only tool bar button

  6. Press the Show Source of Selected Element Only button again to see the whole Java file again. In the Outline view, select different elements and note that they are once again displayed in a whole file view in the editor. The Outline view selection now contains a range indicator on the vertical ruler on the left border of the Java editor that indicates the range of the selected element.

    Range indicator

Adding methods and using code assist

  1. Start adding a method by typing the following at the end of the VectorTest.java file (but before the closing brackets of the type) in the Java editor:
      public void testSizeIsThree() {  
    As soon as you type the method name in the editor area, the new method appears at the bottom of the Outline view.

    New method in outline view

  2. Click the Save button. The compilation unit is compiled automatically and errors appear in the Package Explorer view, in the Tasks view and on the vertical ruler. In the Package Explorer view, the errors are propagated up to the project of the compilation unit containing the error.

    Error propagation in package explorer

  3. Complete the new method by typing the following: 
    assertTrue(fFull.size() == 3); }
  4. Save the file. Notice that the error indicators disappear since the missing bracket has been added.

Deleting and replacing a method from the local history

In this section, you will use the local history feature to switch to a previously saved version of an individual Java element.

  1. Delete the method you just created by selecting it in the Outline view and clicking Delete from the context menu.
  2. Add a new version of the method by typing the following at the end of the VectorTest.java file in the Java editor: 
    public void testSizeIsThree() { 
        fFull.add(0, new Integer(0)); 
        fFull.remove(new Integer(3)); 
        assertTrue(fFull.size() == 3); }
     
    Save the file.
  3. In the Outline view, select the method testSizeIsThree(), and from its context menu, select Replace With > Element from Local History.
  4. In the Replace Java Element from Local History dialog, the Local History list shows the various saved states of the method. The Java Source Compare pane shows details of the differences between the selected history resource and the existing workbench resource.

    Replace from local history

  5. In the Local History pane, select the version that you deleted, then click the Replace button. In the Java editor, the method is replaced with the selected history version.
  6. Save the file.

Using content assist

In this section you will use content assist to help finish writing a new method.  Open junit.samples.VectorTest.java file in the Java editor if you do not already have it open and select the testSizeIsThree() method in the Outline view.

  1. Add the following lines to the end of the method: 
    Vector v = new Vector(); 
    for (int i=0; i<3; i++) 
        v.addElement(new Object()); 
        assert
  2. With your cursor at the end of the word assert, press Ctrl+Space to activate content assist. The content assist window with a list of proposals will appear. Scroll the list to see the available choices.

    Content assist

  3. With the content assist window still active, type the letter 't' in the source code after assert (with no space in between). The list is narrowed and only shows entries starting with 'assertt'. Select and then hover over various items in the list to view any available Javadoc help for each item.

    Content assist filtered

  4. Select assertTrue(boolean) from the list and press Enter. The code for the assertTrue(boolean) method is inserted.
  5. Complete the line so that it reads as follows: 
    assertTrue(v.size() == fFull.size());
  6. Save the file.

Using source code templates

In this section you will use content assist to fill in a template for a common loop structure.  Open junit.samples.VectorTest.java file in the Java editor if you do not already have it open.

  1. Start adding a new method by typing the following: 
    public void testValues() { 
        Integer[] expected= new Integer[3]; 
        for
  2. With the cursor at the end of for, type Ctrl+Space to enable content assist.  You will see a list of common templates for "for" loops.  When you hover over a template, you'll see the code for the template in its help.  Note that the local array name is guessed automatically.

    Content assist for for

  3. Choose the for - iterate over array entry and press Enter to confirm the template.  The template will be inserted in your source code.

    Inserted for template

  4. Complete the for loop as follows:
    for (int i= 0; i < expected.length; i++) { 
        expected[i]= i + 1; } 
    Integer[] actual= to
    (Ignore any warnings in the vertical ruler for now.)
  5. With the cursor at the end of to, type Ctrl+Space to enable content assist. Pick toarray - convert collection to array and press Enter to confirm the selection (or double-click the selection).

    Inserted for template

    The template is inserted in the editor and type is highlighted and selected.

    Inserted for template

  6. Overwrite the selection by typing Integer. The type of array constructor changes when you change the selection.
  7. Press Tab to move the selection to collection and overwrite it by typing fFull.

    Inserted for template

  8. Add the following lines of code to complete the method: 
    assertEquals(expected.length, actual.length); 
    for (int i= 0; i < actual.length; i++) 
        assertEquals(expected[i], actual[i]); 
    }
  9. Save the file.  (Ignore any warnings in the vertical ruler for now).

Organizing imports

In this section you will use organize the import declarations in your source code.  Open junit.samples.VectorTest.java file in the Java editor if you do not already have it open.

  1. Delete the import declarations by selecting them in the Outline view and selecting Delete from the context menu.  You will see numerous compiler warnings in the vertical ruler since the types used in the method are no longer imported.

    Outline view context menu

  2. From the context menu in the editor, select Source >Organize Imports. The required import statements are added to the beginning of your code below the package declaration.

    Organize imports

    You can also choose Organize Imports from the context menu of the import declarations in the Outline view. Note: You can specify the order of the import declarations in Preferences (Window > Preferences > Java > Organize Imports).
  3. Save the file.
  4. Note that the warnings associated with import statements have disappeared.

Extract a method

In this section, you will improve the code of the constructor of junit.framework.TestSuite. To make the intent of the code more clear, you will extract the code that collects test cases from base classes into a new method called collectTestMethods.

  1. In the junit.framework.TestSuite.java file, select the following range of code inside the TestSuite(Class) constructor:
    Class superClass= theClass; 
    Vector names= new Vector(); 
    while (Test.class.isAssignableFrom(superClass)) { 
        Method[] methods= superClass.getDeclaredMethods(); 
        for (int i= 0; i < methods.length; i++) { 
            addTestMethod(methods[i], names, constructor); 
        } 
        superClass= superClass.getSuperclass(); 
    }
     
  2. From the selection's context menu in the editor, select Refactor > Extract Method....

    Context menu extract method

  3. In the Method Name field, type collectInheritedTests
  4. To preview the changes, press Preview>>.

     

    Extract method wizard page 1

  5. The preview page displays the changes that will be made. Press OK to extract the method.

    Extract method wizard page 2

  6. Go to the extracted method by selecting it in the Outline view.

    Extracted method

Using open declaration and open on type hierarchy

  1. Open the junit.samples.money.MoneyTest.java file in the Java editor.
  2. On the first line of the MoneyTest class declaration, select the superclass TestCase and either Note: This command also works on methods and fields.

    Menu open declaration

    The TestCase class opens in the editor area and is also represented in the Outline view.

    Opened declaration

  3. With the TestCase.java editor open and the class declaration selected:

    Menu open type hierarchy

  4. The Hierarchy view opens with the TestCase class displayed.

    Type hierarchy view

    Note: You can also open editors on types and methods in the Hierarchy view.

Related concepts

Java views
Java editor
Refactoring support
Templates

Related tasks

Using the Java editor
Showing and hiding elements
Showing single elements or whole Java files
Sorting elements in Java views
Using the local history
Using content assist
Using templates
Managing import statements
Refactoring
Refactoring with preview
Using the Hierarchy view
Opening a type hierarchy on a Java element
Opening a type hierarchy on the current text selection
Opening an editor for a selected element

Related reference

Java Outline View
Java Content Assist
Templates Preferences
Organize Imports Preferences
Java Editor Preferences
Extract Method Errors
Type Hierarchy View

Copyright IBM Corporation 2000, 2002. All Rights Reserved.