Moving and copying Java elements

In this section, you will use refactoring to move a resource between Java packages.  Refactoring actions change the structure of your code without changing its semantic behavior.

  1. In the Package Explorer view, select the MyTestCase.java file from your test package and drag it into the junit.framework package.  (Dragging and dropping the file is similar to selecting the file and choosing Refactor > Move from the context menu.) 
  2. You will be prompted as to whether you want to update references to the file you are moving.  Typically, you will want to do this to avoid compile errors.  You can press the Preview button to see the list of changes that will be made as a result of the move.
  3. Press Finish.  The file is moved, and its package declaration changes to reflect the new location.

    Editor with updated package declaration

  4. Select the MyTestCase.java file and drag it into the root directory of the JUnit project. Once again, you will be prompted to decide how to handle updates to the references to the file.  You may Preview  a list of these changes or you may press OK to complete the change without previewing.  After you have pressed OK (or Finish from the preview window), a default package is created to contain the class, and the package declaration is removed to reflect the file's location in the default package.

    Editor without package declaration

Using the context menu is an alternative to the drag and drop.  When using the menu, you must specify a target package in the Move dialog, in addition to the update references options you've already seen.

  1. Select the MyTestCase.java file and from its context menu, select Refactor > Move.
  2. In the Move dialog, expand the hierarchy to browse the possible new locations for the resource. Select the junit.framework package, then click OK. The class is moved, and its package declaration is updated to the new location.

    Move destination dialog

Related concepts

Java views
Refactoring support

Related tasks

Refactoring
Copying and moving Java elements
Moving folders, packages and files

Related reference

Refactoring actions
Refactoring wizard
Refactoring preferences

Copyright IBM Corporation 2000, 2002. All Rights Reserved.