Homepage, Blog >
Articles >
Eclipse Usage Tips
Introduction
This article lists tips for using Eclipse. It oviously is
not
exhaustive, but instead limited to things that I find the most useful
and/or use most often.
Nomenclature
- Preferences vs. Properties: Preferences are
global settings
while
properties apply to single entities such as projects or files. Project
properties can override preferences in many cases, such as what
warnings are to be produced etc.
- Macintosh keys: Unless otherwise noted,
substitute the
COMAND key for CTRL in the descriptions below.
Navigate
- Bookmarks: This is a view that you must
explicitly open
(via the Window
menu). Add new bookmarks via the Edit
menu.
- Clickable identifiers: Holding CTRL turns
identifiers in
editors
into hyperlinks that when clicked transport you to their definition.
Display Information
- Explore the Navigate
menu:
- Quick type hierarchy: displays the type
hierarchy view as
a popup window
- Quick outline: same as above for the outline
view.
- Call hierarchy: here you can incrementally
go into a tree
of who is calling a certain method.
- Explore the standard toolbar icons:
- Toggle Mark Occurrences: highlights all
occurrences of
the current identifier. Works for:
- Implemented interfaces: highlights the
implementations
of their methods
- Catch argument types: highlights where
they are thrown
- Return types: highlights where they are
returned
- Show source code/JavaDoc of libraries: Project
Properties
-> Java Build Path allows you to attach the source code (or
JavaDoc) to a library.
Improve Editors and Well-Known Views
- Open Mode: Use single-click for opening
resources (files)
in the Navigator and Package Explorer (Preferences
-> General).
- Package Explorer
- View menu -> Layout: I like the
hierarchical
package layout better than the (default) flat layout.
- View menu -> Filters: I usually hide
all "*.jar"
files to have less clutter.
- Link with editor icon: constantly display
(and highlight)
the
currently active editor. That is, when you have several editor windows
open, you'll always get the context of the currently edited resource.
- Context menu -> Go into: Restrict the
display to a
subtree.
Handy if you have many packages. Works great with the forward/backward
buttons that work like in a web browser.
- Type Hierarchy
- Lock View and Show Members in Hierarchy
(icon in lower
half):
show all methods that override/implement the currently selected one.
- Explore Preferences -> Java ->
Compiler
-> Errors/Warnings:
- Warn about unused private members
- Warn about methods that override, but don't
have the
@Override annotation
- Stack Trace Console: Can be shown in the
standard console
view
and allows one to paste a stack trace to make it hyper-linked (i.e.,
clicking on source code locations jumps to that location).
Source Code Generation
Explore the Source
menu:
- Generate delegate methods
- Surround with try/catch block: Select a few
lines, invoke
this
command and Eclipse infers what exceptions are thrown in these lines.
- Format source code: make source code adhere to
reasonable
standards (that are even configurable); and there is some atrocious
code out there.
- selections restrict formatting to a text
region
- works well for method headers and JavaDoc
comments
- less useful for long method invocations
Content Assist
Invoke content assist via the Edit
menu or via CTRL-SPACE (ALT-SPACE on a Mac). It works in suprisingly
many situations
- JavaDoc comments:
- Complete HTML tags. For example:
auto-completing
"<co" gives
you "<code></code>" where the cursor is
positioned between
the tags and a tab stop has been added after them.
- Generate inline {@link} tags (new in Eclipse
3.2): if the
prefix for content assistance starts with "#" or a capital letter,
you'll be offered a selection of methods or classes to link to.
- "nu" is expanded to "<code>null</code>".
- Variable names: provide handy defaults for
variable names
that
are derived from the class name. For example, after typing
"StringBuffer", content assist offers "stringBuffer" as a variable name.
- Override methods: if you invoke content assist
between
methods, you'll be offered a set of methods to override.
- Code templates: these appear at the end of the
expansion
suggestions
- main: insert the standard static main method
- sysout, syserr: insert System.out.println,
System.err.println
- systrace: print the current method on stdout
- for: insert a for loop
Quick Fix
Invoke via Edit
menu or via
CTRL-1. For keyboard navigation, you can use "jump to next
annotation" (see below) to go to the next error message and then quick
fix the problem.
- Unknown identifier: fix by generating a
definition for a
local variable, a field etc.
- Wrong type: introduce cast when the right hand
side of an
assignment of the type of a parameter does not fit.
- Example:
if (obj instanceof
String) { String
str = obj; } produces an error. Quick-fixing it puts a
cast in front of obj.
- Remove method bodies: in interfaces or for
abstract
methods, method bodies make no sense. You can remove them with a quick
fix.
- Example: Copying a complete method from a
class to an
interface
and removing its body is often quicker than selecting just the header
and appending a semicolon.
- Quick fixes even if there is nothing to fix:
- At if statement: add else case.
- After opening brace: remove surrounding if
block.
- At try statement: add finally block.
- At catch block: remove or turn into throws
declaration.
Keyboard Navigation
- Explore Preferences -> General
-> Keys
-> View. This
table gives you a good overview of the available keyboard
shortcuts.Category "Text Editing" has a few gems:
- Scroll Line Down/Up: Scroll the editor without leaving the
current line.
- Move Lines Down/Up: move either the current
selection or
current line one line up/down. Nice for moving a line out of a block.
- If you look at the context menu, you see that
standard
menus such as Refactoring
have their own shortcuts. It is often easier to memorize these category
shortcuts instead of the shortcuts of every single operation.
- Jump to next annotation: CTRL-. (DOT). What
annotations are
considered can be configured via the icon menu in the standard toolbar.
- Open type/resource (menu Navigate, icon in
standard toolbar):
Jump directly to a Java type or a resource, by typing part of its
(unqualified) name. If you use the shortcut, you can switch between
classes without using the mouse.
- Fix indentation (CTRL-I): of either the
selection or the
current
line. This change is less disruptive than formatting the source code.
- Show tooltip description: Display information
for current
entity (same as hovering with cursor)
- Error messages
- JavaDoc: also displays inherited content,
thus making it
useful for methods that override and have no JavaDoc of their own.
Refactoring
- Introduce parameter: select a constant in a
method and this
refactoring leads to a new parameter being created and the constant
being copied to every existing invocation of that method.
- Useful together with the "Extract Method"
refactoring to
produce more parameters, as the extracted source code fragment is bound
to use fields, constants in addition to local variables (which are
always turned into parameters).
Standard Java
- Final methods: prevent overriding
- Final member variables: force either direct
initialization
or (at the latest) in the constructor.
- @Override annotation: Avoids the common
pitfall where you
think
you override a method, but actually you don't. Using this annotation on
a method that does not override anything results in an error.
- Inline JavaDoc tags: With braces you can nest
JavaDoc tags
- {@param}: Refer to a parameter by name.
Advantage: name
stays up-to-date when using refactoring to rename it.
- {@inheritDoc}: Insert the JavaDoc of an
overridden entity.
- {@literal} (Java 1.5): escape special HTML
characters
(such as < and >).
- {@code} (Java 1.5): escape HTML characters,
format in a
fixed-width font.
Document History
- 2006-05-14: First version of this document
2006-05-14,
Axel
Rauschmayer