Useful Eclipse Tips

From EngGuide

Contents

Introduction

Eclipse is a great development environment for editing Java source. A programmer can become more productive by using shortcuts and features of the IDE to accomplish a task. These are some useful tips to help programmers navigate quickly through large projects, refactor code, and create patches.

Navigation Shortcuts

Navigating a workspace can be a tedious task when the file that needs to be opened is known but time is wasted searching through Package Explorer. There are quite a few shortcuts to cut down on time and make searching a workspace very easy.

  • F3 (Open Declaration) - Goes to where the object, method, class, etc. was declared. This is very useful to see what a method exactly does or to open up a class.
  • F4 (Open Type Hierarchy) - Displays the type hierarchy in a view. This is useful for seeing the inheritance hierarchy of a class. It also shows the interfaces implemented. Double-clicking on a class in the hierarchy will open it up in an editor.
  • Ctrl+T (Quick Type Hierarchy) - This is similar to the Open Type Hierarchy feature. Instead of showing the hierarchy in a view, it displays it in a lightweight dialog. Pressing Ctrl+T again will cycle over sub-types and super-types. Clicking on a type will open it in an editor. This is extremely useful when interfaces are heavily used since using Open Declaration will go to the interface. Quick Type Hierarchy will show classes implementing the interface and clicking on a concrete class will show the method's implementation.
  • Ctrl+Shift+T (Open Type) - A quick way to open a class in your workspace. It shows a dialog and filters as a class is specified. This is much quicker than searching in the Package Explorer and easier if the class is nested.
  • Ctrl+Shift+G (References in Workspace) - Searches the workspace for references of whatever the cursor is on. It can be an easy way to see where methods are being called. Results are shown in the Search view.
  • Ctrl+Alt+H (Open Call Hierarchy) - Shows calls to the current method. If no method is at the cursor point, it will use the method the cursor is in. A similar result is given when using References in Workspace.
  • Ctrl+Shift+R (Open Resource) - Shows a dialog for opening a resource. It filters resources as the filename is typed. This can be quicker than browsing in the Package Explorer.

Refactoring

Refactoring code is a common task when working on a project. Parts of code may need to be changed to accomodate a new feature or to clean up the design for better reuse. Eclipse comes equipped with many basic features and some advanced tools to make refactoring code a lot easier and quicker. A full list of refactoring options is available in the Refactor menu. Here are some more common refactoring tasks:

  • Alt+Shift+R (Rename) - A very useful feature when variables and such are named poorly. Put the cursor on the variable, class, method, etc. and rename it. Eclipse will go through all occurances in the workspace and smartly replace them with the new name.
  • Alt+Shift+V (Move) - Handy if you wrote a method, etc. in the wrong place. Eclipse will move it to the specified class. It can be used on classes to move them to another package. References will be updated as necessary.
  • Alt+Shift+L (Extract Local Variable) - It happens often that a method returns an object and it is used directly instead of assigning it to a variable. This expression can be highlighted and extracted to a local variable. Eclipse will update any references in the local scope as required.
  • Alt+Shift+Up (Highlight Expression) - This is very useful when used with Extract Local Variable. Highlight Expression can highlight the part required to extract into a local variable. Keep pressing Up until all of the expression that should be turned into a local variable is highlighted.

Some other advanced features that are not used frequently:

  • Extract Interface - Any constants or abstract methods will be shown in a dialog. They can be selected for a new interface. The current class will be refactored to implement the interface and method stubs created.
  • Push Down/Push Up - Methods can be pushed up and down a hierarchy. References will automatically be updated.
  • Convert Anonymous Class to Nested - Anonymous classes are handy but sometimes they need to be reused. Eclipse can extract the class and make it nested (aka. inner class).
  • Introduce Parameter Object - There are a few Introduce... refactoring options. This takes a method and creates a parameter class that can be passed to the method. It is useful for methods that require a lot of values in the constructor, some that may be null/optional.

Patches

Creating patches is a common task in software development when using version control. They can be sent to others or posted as an attachment to a bug to propose a solution. Eclipse has its own patch creation tool and merging system. It can also be a way to save work in progress.

Creating a patch

For a workspace that has projects checked out from a source repository, a patch can be created from the Package Explorer. It can also be done from the Team Synchronizing perspective if the project is from a CVS repository.

  1. Select the files, packages, projects, etc. that should be included in the patch.
  2. Right-click on the selection, go to Team.
  3. Select Create Patch...
  4. A dialog will open asking for the location to create the patch (ie. clipboard, file system, or workspace).
  5. Click on Finish to create the patch.

Applying a patch

When another programmer posts a patch, it would be useful to merge the changes for testing. Or maybe the patch is a save of work in progress. Patches are applied from the Package Explorer.

  1. Be sure that the projects that the patch affects are open.
  2. Right-click anywhere in the Package Explorer, go to Team.
  3. Select Apply Patch...
  4. A dialog will open asking for the location of the patch.
  5. Click Next to check for any merge conflicts.
  6. Use the Guess button if there are any and merge any unresolved conflicts manually.
  7. Click on Finish to go back to the patched workspace.

Other Useful Shortcuts

These are some other useful shortcuts that assist with typing. This is useful for lazy programmers, which we all are.

  • Ctrl+Shift+O (Organize Imports) - When dealing with large projects, dealing with imports can be a mess. This shortcut will automatically add/remove imports that as necessary.
  • Ctrl+1 (Quick Fix) - As if there is such a thing :P. Really, there is. Eclipse can suggest changes that will eliminate a compile error. Put the select the error in the editor and see what Eclipse suggests as a fix. It works well with a variety of problems such as surrounding a block with a try/catch, adding an import, or casting an expression.
  • Ctrl+Space (Content Assist) - This is basically autocomplete. Using this, it is possible to program with one hand. It pops up a lightweight dialog with options to complete method calls or type declarations, insert templates, etc.