Editing Java elements

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

Using the outline view

  1. To open the file junit.samples.VectorTest.java in the Java editor, double click on the file in the Package Explorer view.

  2. Notice the syntax highlighting. Examples of parts of java source which are rendered differently are:

    • Regular comments
    • Javadoc comments
    • Keywords
    • Strings.
    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 also indicates whether a Java element is static, abstract, final, etc. The outline view also shows 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. Toggle the Sort button in the Outline view to sort the Java elements in alphabetical order rather than in the order they appear in the Java file.

  6. You can edit source code by viewing the whole Java file, or you can narrow the view to a single Java element. Click the Show Source of Selected Element Only button in the toolbar.

    Next, In the Outline view, select any element and note that only the selected element is displayed in the Java editor.

    Show selected element only tool bar button
  7. 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 is now indicated with a range indicator on the vertical ruler on the left border of the Java editor.

    Range indicator

Adding methods and using code assist

  1. Confirm that the alphabetical Sort button in the toolbar of the Outline view is toggled off.

  2. 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
  3. 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
  4. Complete the new method by typing the following:

        assertTrue(fFull.size() == 3);
    }
  5. Save the file.

    Notice that the error indicators disappear.

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 from Local History.

  4. In the Replace Java Element from Local History dialog, the Local History list shows the various saved states of that element. 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

  1. Open the junit.samples.VectorTest.java file in the Java editor.

  2. Go to the testSizeIsThree() method by selecting it in the Outline view.

  3. 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
  4. 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
  5. With the content assist window still active, type the letter 't' after assert (with no space in between). The list is narrowed and only shows entries starting with 'assert'.

    Select and then hover over various items in the list to view any available Javadoc help for each item.

    Content assist filtered
  6. Select assertTrue(boolean) from the list and press Enter.

    The code for the assertTrue(boolean) method is inserted.

  7. Complete the line so that it reads as follows:

    assertTrue(v.size() == fFull.size());
  8. Save the file.

Using templates

  1. Open the junit.samples.VectorTest.java file in the Java editor.

  2. Start adding a new method by typing the following:

    public void testValues() {
        Integer[] expected= new Integer[3];
        for
  3. With the cursor at the end of for, type Ctrl+Space to enable content assist.

    Content assist for for
  4. Choose the for - iterate over array entry.

    The template is inserted in the editor and i is highlighted. The local array name is guessed automatically.

    Inserted for template
  5. Press Enter to confirm the template.

  6. Complete the for loop to:

    public void testValues() {
        Integer[] expected= new Integer[3];
        for (int i= 0; i < expected.length; i++) {
            expected[i]= i + 1;
        }
        
        Integer[] actual= to
  7. With the cursor at the end of to, type Ctrl+Space to enable content assist. Pick toarray - convert collection to array.

    Inserted for template

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

    Inserted for template
  8. Overwrite the selection by typing Integer. The type of the array constructor changes synchronously.

  9. Type Tab to highlight and select collection and Overwrite the selection by typing fFull.

    Inserted for template
  10. Type Enter to confirm the template.

  11. 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]);
        }
  12. Save the file.

Organizing imports

  1. Open the junit.samples.VectorTest.java file in the Java editor.

  2. Delete the import declarations by selecting them in the Outline view and selecting Delete from the context menu.

    Outline view context menu
  3. From the context menu in the editor, select Organize Imports.

    The required import statements are added to the beginning of your code below the package declaration.

    Organize imports

    Alternatively, Organize Imports can be invoked directly 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).

  4. Save the file.

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:

    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();
    }

    Select the code range by using the Expand Selection to feature:

    1. Navigate to the constructor TestSuite(Class).
    2. Set the cursor just before while.
    3. Press Alt+Shift+Arrow Up.
    4. Press Alt+Shift+Arrow Left twice.
  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, then click Next.

    Extract method wizard page 1
  4. The refactoring preview page displays the changes that will be made.

    Press Finish to extract the method.

    Extract method wizard page 2
  5. 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 TestCase superclass specification and either

    • select from the menu bar Navigate > Open Declaration or
    • press F3.

    Note: This command also works on methods and fields.

    Menu open declaration

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

    Opened declaration
  3. Click the TestCase.java editor tab to make it the active editor. Ensure that the class declaration is still selected, and:

    • select from the menu bar Navigate > Open Type Hierarchy or
    • press F4.
    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