Whenever I use the Step Into feature (F5) in Eclipse’s debugger, I’m mainly interested in stepping through code in my own classes, not the ones from external libraries or even Java classes.
For example, there’s almost no reason to ever want to step into Spring’s code or proxy classes (other than to learn more about them or maybe debug a potential bug in Spring). And normally I’m not interested in Java util classes (eg. ArrayList). This also goes for Hibernate, Apache Commons, Google and many other external libraries.
Fortunately, Eclipse makes it easy to specify which classes to skip by allowing step filters. This makes it easier to focus on your own code and also keeps your editor area clean since Eclipse won’t be opening classes in separate editors all the time.
Whenever you hover over any piece of code in Eclipse, it pops up a tooltip that displays more information about the item, such as its declaration, variable values or Javadoc information, as in the example below.
Although useful at times, this becomes extremely annoying after a while, especially when you’re using your mouse to browse some code. Popup after popup of unwanted information keeps obscuring your view of the code, leading to some lengthy expletives and big productivity loss. It’s useful information, but not every time all the time, almost like your car’s GPS giving you directions to 10 different places at once while you’re still parked in the driveway.
Luckily there is a way to alleviate the problem and all it takes is changing some preferences in Eclipse. We don’t want to completely disable tooltips (they can be useful), so I’ll show you how to tell Eclipse to bring up the tooltips only when you request them.
Unused variables and methods should alway be unwelcome. Removing them keeps the code cleaner and easier to read. Now, by default Eclipse warns you about unused private variables and methods, but it doesn’t warn you (by default) about unused method arguments.
But there is a compiler setting in Eclipse that can warn you when you don’t use an argument in a method. You can even handle arguments on inherited methods, especially useful when using 3rd party libraries.
Once you’ve configured Eclipse preferences to your heart’s content, you’ll often want to share those preferences across multiple workspaces. Now normally you can go to File > Export > General > Preferences to save your preferences to a properties file which you can then import into the other workspace. This will share settings such as your customised keyboard shortcuts, formatting, repository settings, etc.
But, for some reason, Eclipse doesn’t save perspective/window layouts, such as which views are open and where they are placed in the perspective. So you’ll find yourself spending another half hour configuring the window to the way you like it. After the 3rd workspace you need to create, this becomes frustrating and just wastes time.
Fortunately there are ways to save and restore these settings automatically. The first is to save the perspective into the preferences and the other is to use Eclipse’s Copy Settings feature when opening the other workspace. I prefer the first option, but I’ll mention the second option and when to use the one over the other.
We all know that Java statements are terminated by a semicolon (;), but they’re a bit of a pain to add to the end of a line. One way would be to press End (to move to the end of the line) then press semicolon, but this is tedious. Because this is something that you do often it’s worth learning how to do this faster.
It’s a good thing Eclipse can automatically put the semicolon at the end of the line, no matter where you are in the statement. It’s as easy as setting one preference and there’s a bonus preference for adding braces to the correct position as well. For something so small, it saves a lot of time.
So in the example below, if you imagine that your cursor is placed after the word blue since you were editing the string. Pressing semicolon will cause Eclipse to place the semicolon after the closing bracket at the end. Nice.
When I use Eclipse I try and use shortcuts all the time. They really make me work faster and give me time to focus on getting the code out rather than slowing down to invoke IDE commands with the mouse.
Because people work differently and on different things, it’s important to manage keyboard shortcuts to suit the way you work. So it’s good that Eclipse makes it really easy to change shortcuts and also to view shortcuts for the commands you use a lot.
I’ll discuss how to change keyboard shortcuts, how to avoid conflicts with existing ones, why it’s a good thing to manage shortcuts and then end off with some examples of common shortcuts that you should add to you arsenal. (more…)
Eclipse normally adds import statements for you when you autocomplete a type, press Ctrl+Shift+O or organise imports when you save. However, by default it doesn’t do the same for static imports, something you’ll miss when using classes like JUnit’s Assert and Apache Commons’ StringUtils. This means having to add import statements manually, which takes unnecessary time.
So, in the code below, I want to be able to autocomplete assertEquals (line 7) and have Eclipse add a static import for org.junit.Assert.assertEquals (line 1).
import static org.junit.Assert.assertEquals;
...
public class MyTest {
@Test
public void testSomething() throws Exception {
assertEquals(1, 1);
}
Now, the good news is that there is a way to automatically add static imports when you use autocomplete. It’s done via a preference that tells Eclipse which static classes you want to include for Content Assist (autocomplete). I show you how to do this below.
The bad news is that there doesn’t seem to be a way to tell the Organise Imports command to add static imports. However, a working autocomplete should already cater for 90% of your coding needs, so the bad news isn’t that bad.
I previously mentioned shortcuts to run/debug a class as a Java app/JUnit test. But have you ever tried to rerun the last application you launched in Eclipse using F11 or the toolbar, only to find out it’s running the currently active class or selection in Package Explorer? Well, that’s because Eclipse’s default is to run the selected resource or active editor if it’s launchable (eg. if it has a main method or is a JUnit test).
Luckily, there is a preference you can change to force Eclipse to always run the last application you launched. So now you can avoid the confusion and rest assured that it’ll do what you want it to do.
Eclipse has a wonderful feature that formats according to your coding standards. It handles things like indentation, where curly braces are placed, if blank lines should occur between field declaration and hundreds of other options.
However, to invoke this formatting, you have to tell Eclipse to do this every time you’ve edited the code. You can do this using Ctrl+Shift+F but (1) you have to do that every time and (2) you have to remember to do it in the first place (and we all know how good developers are at remembering things).
Eclipse sports a feature called Save Actions that makes formatting code easy. With this feature enabled, every time you save the file, Eclipse formats the code to your formatting preferences and does some cleanup of the code (eg. remove unused imports). All done automatically and for free. (more…)
I discussed templates and why to use them in another post. Basically, we use them to eliminate the pesky, repetitive code zombies. I also showed some built-in templates and what they do.
But what if you want to add your own template or modify an existing one? Managing your own templates is important because your business domain and how you work determines to a large extent what is the common code you use all the time.
Don’t be tempted to say “It takes too long to create the template. In that time I could’ve typed the code myself.” Are you going to say that the next 50 times you have to type the same code? I always force myself to sacrifice the 2 minutes because I’ll save me 20 minutes in the long run.
How to add/edit a template
In the spirit of show, rather than tell, here’s a video that shows how to create a template to make it easier to add an additional case-statement to a switch-block. We want to add case blocks for IDLE and OFF for the enum EngineState.
A couple of notes on some of the fields we saw in the video:
Name: The shorter the better, because you want to type as little as possible to invoke it. But make it unique enough so you don’t get too many matches.
Context: Eclipse will only show the template in the list of available templates if it belongs to the right context. Eg. when editing JavaDoc’s, none of the Java statements templates will appear, only the Javadoc ones.
Description: Make it nice and… descriptive, stating what the template does. The short name may not always be enough to know this.
Pattern: You can mix normal Java code with template variables. I describe variables below.
Variables
You can use variables to make the template a bit smarter. Eclipse has a raft of built-in variables and although all variables are useful, some are more useful than others.
The example below (from the video) shows 2 variables: ${cursor} which tells Eclipse where to leave the cursor after cycling through all placeholders and ${value} which is a custom variable, ie. not built-in.
case ${value}:
${cursor}
break;
Keep the following in mind when using templates:
You can use the Tab key to move from one variable to another. No mouse or arrow keys required.
Custom variables don’t mean anything to Eclipse but they can be used later in the same template to refer to the same user-provided name. See the new template for an example of this.
You can also use an unnamed variable (${}) to signify a placeholder where the user should enter something but the variable isn’t used anywhere else.
Other variables can tell Eclipse to guess which field to use or to get documentation info (eg. current author).
Variables must be enclosed in ${} – don’t forget this.
Useful Variables
Here’s a rundown of the most useful variables. You can get a full list by pressing Ctrl+Space while editing a template.:
cursor – Tells Eclipse where the cursor will finally end up after you’ve edited all the other variables. Leave the cursor somewhere sensible like on a blank line so you can start typing immediately.
var – A list of variables in scope, the first guess being the last variable declared in the smallest scope. You can pass a type of limit the list to only variables of that type.
iterable, array, collection - A list of either iterables, arrays or collections in scope, starting with the last declared variable in the smallest scope. Nice for loops over an array/collection. iterable is sufficient for most situations as it covers both arrays and collections.
line_selection – Perfect for templates that wrap a block of selected code in a loop or some other construct. The for and while templates use these to allow you select code that will be placed in the loop. To use, select the code, then press Ctrl+Space twice to get Template Proposals (you may need more presses depending on how you’ve set up your proposal options.
import – A specific import you need generated together with the template. For example, a template that generates a log4j statement can also import log4j’s Logger. The import’s ignored if it already exists.
newName – A unique name for a variable based on the type you pass it. Eg. if you pass the type MultipleDocHandler, a variable with the name multipleDocHandler will be generated.
How to import/export a template
To make it easy to share templates, Eclipse allows you export a template so that someone else can import them. An XML file is produced which contains the template(s) you selected. I’ll be sharing some handy home-grown templates as XML files.
Here’s how to import/export a template
Go to Window > Preferences > Java > Editor > Templates
Select the templates you want. NB! The checkboxes don’t indicate what’s selected; they are used to enable/disable a template. A template is selected if the whole row in the table is selected, so use Ctrl+Left Click or method specific to your OS to multi-select templates.
Click Import… and choose the XML file you received. Or Export… and provide a filename.