Eclipse - notsojug/jug-material GitHub Wiki

This section will explore tips an tricks regarding the ever-famous Java IDE: Eclipse!

Table of contents

Shortcuts

The auto-complete

Eclipse lets you complete what you thought about with a Ctrl-space and some characters.

ctrl-space

This image shows you how the shortcut helped turning so into some by triggering the auto-complete. After we pressed Enter, the first suggestion is entered. You can also choose the suggestion using the arrow keys.

Auto complete also triggers itself after you press . near an object, exploring the method you can call on that object. That's why after getting some. the user does not need to press Ctrl-space to trigger auto-complete again.

Shortcut help

Where: Everywhere

Shortcut: Ctrl-Shift-L

shortcut help

If you don't know the shortcuts, learn this! Then type some characters to search for specific shortcuts

Contextual fix

Where: On errors and warnings, with cursor in the affected line or variable.

Shortcut: Ctrl-1

ctrl-1

Fixes common errors by selecting the suggested entry and pressing <Enter>

Quick assist: assign to field, assign to variable, extract method, rename in file

Where: On lines, variables, method calls, selected code

Shortcut: Ctrl-2 (wait) {F,L,M,R}

Example: Type new Thing(), then Ctrl-2 + L, the, at the cursor, type myThing. The result should be Thing myThing = new Thing()

Animation: ctrl-2

Rename a variable/class/method/file

Where: Almost everywhere, with cursor on the item

Shortcut: Alt-shift-R

rename

Create a test method

Where: In a (test) class

Shortcut: test<ctrl-space><enter>

test

Run tests in normal mode

Where: In test classes

Shortcut: Alt-shift-X T

Running tests should not be painful, this shortcut runs the current test class. If your cursor is over a method name, it runs just the current test method.

Run tests in debug mode

Where: In test classes

Shortcut: Alt-shift-D T

Same as above, but in debug mode.

Run main methods in normal mode

Where: In classes with main method

Shortcut: Alt-shift-X J

Running classes with a main method is of course easy with this shortcut.

Run main in debug mode

Where: In classes with main method

Shortcut: Alt-shift-D J

Same as above, but in debug mode.

Run coverage

Where: In test classes

Shortcut: Alt-shift-E T

If you're into test coverage, this shortcut runs the coverage test for the current test class or method.

Tip: you can see what path are covered by subsequently running single test methods and seeing how coverage changes in the tested classes.

Open a type (class)

Where: Everywhere

Shortcut: Ctrl-shift-T

This opens a dialog to open types and classes. This dialog has no fuzzy search, but accepts * as a wildcard.

Open type

Open a file of the workspace

Where: Everywhere

Shortcut: Ctrl-shift-R

Like the above, but for all file types.

Fix import and rearrange them

Where: Java classes

Shortcut: Ctrl-shift-O

This command will reorder your import, remove the unused imports, and turn the whole-package imports into class imports.

Open method of a class

Where: Java classes

Shortcut: Ctrl-O

goto-method

Indent file

Where: All known file types

Shortcut: Ctrl-shift-F

This shortcut indents the current file. If you use Java and you have configured the checkstyle plugin, this command will oblige the rules of the plugin.

Refactor menu: provides contextual refactor functions

Where: Variables, methods, method's calls, classes

Shortcut: Alt-Shift-T

This menu offer a way to quickly do some refactoring action like renaming, moving methods and classes, etc.

refactor

Source menu: provides contextual source actions

Where: Classes, mainly

Shortcut: Alt-Shift-S

This menu lets you use the IDE to do some routine source action, like generating getters and setters from fields, using a delegate class, etc...

source

Extract variable

Where: Selecting objects or return values

Shortcut: Alt-Shift-L

This is the same as Ctrl-2 L, but in a single shortcut.

Incremental find (search as you type)

Where: Every text file

Shortcut: Ctrl-J <start typing few characters>

This works faster than the usual search dialog: it is a search-as-you-type facility that helps you find the right spot. If you need to jump to the next location, just press again Ctrl-J.

Toggle breakpoint

Where: Every code file

Shortcut Ctrl-Shift-B

Toggles a breakpoint for the current line

Inspect a value

Where: During debug sessions: in code and in Display dialog

Shortcut: Ctrl-shift-I

This shortcut helps you inspect the value of variables or expressions during debug sessions

inspect

Execute some code

Where: During debug sessions: in code and in display dialog

Shortcut: Ctrl-U

Just select some piece code from the running code, or just write something in the display dialog, hit the shortcut, and execute the selected lines!

execute

Force the current method to return

Where: During debug session

Shortcut: Alt-Shift-F

You can force the currently debugged method to return with this shortcuts. It works on void method simply by pressing the shortcut. It also works on methods that return objects, and you can also provide a different object to be returned: just create that object in the display dialog, select it, and press the shortcut!

Quickly access to views and open files

Where: Everywhere

Shortcut: Ctrl-3

You can jump to a special searchbox from everywhere by pressing this shortcut. The search box lets you navigate between dialogs, files, and perspectives, just type some letters.

ctrl-3

Go to line

Where: In text files

Shortcut: Ctrl-L

This opens a dialog window to let you jump to a given code line.

Surround with: if, do, try, try-catch, try-finally, loop, lock, runner, ...

Where: Selecting lines

Shortcut: Alt-shift-Z

Provides a menu to surround portions of code with common code snippets, such as try-catches, or loops.

alt-shift-z

Inline

Where: Inline the method in the calling site(s)

Shortcut: Alt-shift-I

You can inline a called method with a shortcut, either from the call site(s) or from the method itself. If possible, you can also choose to remove the original method after the refactoring.

inline

Import statically

Where: Statically import a static method or a constant, if possible

Shortcut: Ctrl-shift-M

The IDE knows that you can statically import classes' methods. A shortcut helps you switch from class import to static (method) import.

E.g. when using EasyMock, you can stumble upon the occasional EasyMock.createMock(...), if you hit the shortcut with the cursor over the createMock, the IDE enables the static import for the method, allowing you to just use createMock(...).

Go to previous/next edited place

Where: In code

Shortcut: Alt-{left, right arrow}

If you edit multiple files or you are just exploring code, you will need to go back-and-forth between them. These shortcuts will help you avoid using the mouse to navigate the code.

Show supertypes / type hierarchy

Where: from code

Shortcut: F4

Select a type and press the shortcut to see its hierarchy.

hierarchy

Go to declaration

Where: From places where a method is used

Shortcut: F3

It teleports you to the declaration of a method.

Go to super-implementation

This is not bounded to a shortcut by default, but you can bind it to a shortcut of your choice.

This is useful in java 8, where some methods are defined in an interface, rather than in a class.

Focus on popup dialogs or see contextual help

Where: Over things

Shortcut: F2

Open call hierrarchy

Where: Over method call

Shortcut: Ctrl-alt-H

Search for occurrences

Where: Over strings (not only literal strings)

Shortcut: Ctrl-alt-G

Delete a line (or delete selected lines)

Where: Over lines or selected lines

Shortcut: Ctrl-D

Recommended plugins

JBoss studio

JBoss studio provides application server integration in the IDE, CDI support (producers discovery, scoped beans, beans.xml parsing and modeling, suggestion) and more.

It really makes your Java EE development easier.

Code recommenders (default from Eclipse Mars)

Code recommenders provide fuzzy method name search in auto complete mode. For example, if you have a method named getVar(), you would get the suggestion only after typing getv, while with recommenders you can just type var and only getVar and setVar will come. If you turn the option on, you also get automatic code snippet suggestion and most frequently used method suggestion.

EGit (default from Eclipse Mars)

This plugin provides git support in Eclipse.

Useful dialogs

Display (debug session)

During debug session, while pausing, the Display dialog gives you a sort of scratchpad within the current context. You can reference variables, classes, and even execute methods. You can even force a return on the current method, if you wish.

Tasks

This dialog shows a list the TODOs and other configurable comments along your code. You can use it to see where you left your work on previous sessions.

Useful but rarely used functions

Favorite classes

In this list you can put your most favorite classes. This will come very useful to avoid clumsy code writing in case of static import.

For example, if you frequently write tests with EasyMock, you stumble upon the class EasyMock.createMock() or EasyMock.expect(), which does not work unless you pre-import all the class' static methods. With favorite classes, you just type createMock and a Ctrl-space will import just the required method statically for you.

Pro tip: use it for java 8 Collectors class, for assertj's Assertions, EasyMock, javaslang.API, and javaslang.Predicates for maximum productivity boost.

Templates

You can create context-sensitive snippets and have the IDE type them for you.

E.g. Find yourself writing the TODOs in a particular way? Save it as a template! All your classes have a special method you always write? Give it a name, and the next time you just have to write the few letters to trigger the template, and a Ctrl-space will generate the code for you.

⚠️ **GitHub.com Fallback** ⚠️