Feeds:
Posts
Comments

Extract supertype

When I’m working on a group of classes that offer similar functionality, I’ll want to create a parent class or interface to share functionality or allow the children to be used polymorphically. Eclipse has a couple of refactoring tools that make the process of creating the parent easier. Ctrl-Alt-T to open the refactoring menu and then select Extract interface… or Extract superclass… depending on how you want your hierarchy to behave. Then, select the methods and fields that should be promoted to the parent and voila, Eclipse will create that supertype with the methods and fields you specified.

Today’s tip:

Ctrl-Alt-T, then Extract interface/superclass: moves methods and fields into a new parent supertype

A quick way to expand your current selection to include the next most significant element is to use the keystroke Alt-Shift-Up. Try placing your cursor in the middle of a word and hit Alt-Shift-Up. Your selection will expand to include the whole word. Say your word is a parameter in a method signature, hitting Alt-Shift-Up again will expand the selection to include the parameter’s type. Hitting it again will select the entire method. Neat, huh? As you might expect, there’s a corresponding Alt-Shift-Down to reduce the current selection as well.

Today’s tip:

Alt-Shift-Up/Down: expand/contract the current selection

Ok, this is not an Eclipse tip, unless you consider that it can improve the experience of launching the Eclipse application, in which case, yeah, I guess this is an Eclipse tip… false alarm people.

I came across a gem of a program called Launchy. Basically, it allows you to launch programs (and other things) by using keyboard shortcuts instead of hunting through the Start Menu or Windows Explorer. It does this by indexing certain types of files in certain folders, such as .exe files in the Start Menu folder. 

From the website:

Launchy is a free windows utility designed to help you forget about your start menu, the icons on your desktop, and even your file manager.

Launchy indexes the programs in your start menu and can launch your documents, project files, folders, and bookmarks with just a few keystrokes!

You launch Launchy with the shortcut Alt-Space, then you start typing the name of the thing you want to launch, which Launchy will auto-complete. For example, by the time you’ve typed “e”, “c”, “l”, “i”, Launchy will have proposed Eclipse as one of your options (assuming you’ve created a shortcut in the Start Menu for Eclipse) and you can hit enter to launch it. Type “f”, “i”, “r”, “e” and Firefox will come up. Very cool. Want something even cooler? Type “fire” and then hit tab, then type www.eclipse.org and Launchy will open a new tab in Firefox for the Eclipse home page.

I mentioned that Launchy can launch other things, by that I mean it can launch an mp3 or a Word document, or anything that is launchable. You have to configure it a bit by telling it to index your My Music or your My Documents folder but that’s as easy as right-clicking it and selecting the Directories option.

You really just have to try it, you won’t be disappointed. I can already feel myself becoming disencumbered of some of the weight of Windows, and that’s a good thing.

Launchy is a Windows app. If you are lucky enough to be a Mac user, check out the even more impressive Quicksilver and if you are at all interested in user-interface design, the creator of Quicksilver has given a fascinating Google Talk that you must check out.

Interestingly, Launchy and Quicksilver are both open-source applications which have thoughtful and delightful user-interfaces. I can’t find it now but I read a blog a while back about “why open source software sucks” and the number one reason was that is was unusable because the developers who built it did not have the talent or desire to build quality UI’s. Well, here are two counter-examples to that claim (and I would also throw Eclipse into this mix) and I think it’s a good sign for OSS and users of OSS.

Today’s tip:

Alt-Space (if you’ve installed Launchy): launches arbitrary stuff

Fuzzy search

There are 2 essential shortcuts for searching for and opening files: Ctrl-Shift-R and Ctrl-Shift-T. The R version opens any resource that is in your project where the T version opens any type that is referenced by your project (for a java project, that could mean a type that is included in a 3rd party library).

Once you’ve mastered that, each of those search options allows you to apply Eclipse’s fuzzy search logic. For example, if you Ctrl-Shift-R for a resource called “HippopotamusHandler.java” you could start looking for it by spelling out “H”, “i”, “p”, “p”, etc.. and that would match against “Hippopotamus.java”, “HippopotamusCage.java”, “HippopotamusMeal.java”, “Hippocampus.java”, as well as “HippopotamusHandler.java”. However, if you spell out “H”, “H”, Eclipse uses fuzzy logic to find any resources that have “H” and “H” as the first 2 uppercased letters in their name, so your search would match against “HippopotamusHandler.java”, “HippopotamusHandler.java”, etc. thus greatly narrowing your choices and speeding up the search process, saving you literally 5’s of keystrokes. Holy efficiency, Batman!

Today’s tip:

Ctrl-Shift-R: opens a resource

Ctrl-Shift-T: opens a type

I usually take code analysis tools with a grain of salt, and with an extra shot of espresso if it’s early, but one tool that has proven useful many times is FindBugs, which is available as an Eclipse plugin. It’s easy to install because it has an update site – so there’s no excuse, go grab it. To run, right-click your Java project and select the “Find Bugs” option. There are a few options for viewing the results: they appear as markers buggy-tiny-orange.png in open editors, there are specialized views (Window > Show Views > Other… > FindBugs – start with Bug Tree View, the other views open automatically as you use this one) or you can see the results in the standard Problems view.

The FindBugs plugin is helpful when you are doing code reviews and, if you don’t tell anyone you’re using it, they’ll think you’re so clever for finding that “Load of known null value” or “May expose internal representation by incorporating reference to mutable object”… don’t worry, the names aren’t so confusing when you look at the code.

Today’s tip:

FindBugs plugin: a program which uses static analysis to look for bugs in Java code

Ok, by now we all know that REFACTORING IS GOOD. Say it again! Testify! But with so many refactoring choices in this workaday world, how is a developer to know which one to pick? Well, in a given situation Eclipse will guide you towards refactoring bliss by showing you a neat list of all the applicable refactorings. Select a symbol and hit Ctrl-Alt-T and a dropdown menu will appear, then select whichever refactoring that catches your fancy and you’ll be a few short clicks away from completing your refactoring. 

Today’s tip:

Ctrl-Alt-T: show applicable refactorings

In an earlier blog, I wrote about using F3 to navigate to the declaration of a symbol, say a method. This works great until you hit an interface type and need to find the actual implementation. One way to get the implementation is to hit F4, which shows the type hierarchy in a separate view. The drawbacks of this are that although you can find an implementation, you’ll lose your place in the sense that it won’t automatically jump into the method you were interested in. A great alternative to F3-F4 is Ctrl-T.

Try highlighting a method call that is being made on an interface type and hit Ctrl-T. Eclipse will churn away for a second or so and then you’ll be presented with a dropdown list of implementers of the method. This is a very quick, very handy way to navigate directly to implementations without getting stuck at the interfaces.

Today’s tips:

Ctrl-T: quick type hierarchy

F4: type hierarchy in a view

Line dancing

I have never gone line dancing and there’s a good chance that I never will. I’m not trying to make a statement about urban cowboys. Just saying is all.

Eclipse has a way to make lines dance that Billy Ray Cyrus would be envious of. Alt-Up/Down cures your cut & paste blues and automatically indents the line as it moves up and down the editor.

Today’s tip:

Alt-Up/Down: move line up/down

You know how sometimes when you’re out at a bar with your hot friends and even though you’re drunk and it’s really late you can still tell that the person who’s checking you out is not your type? No? Me neither… I’d much more likely be the unlucky one doing the checking out. Well, at least Eclipse gives us the ability to filter out types that are uninteresting to you. Now the tables have turned and the rejectee becomes the rejecter. Take that, hot people at the bars!

Eclipse has many functions that involve searching for classes, auto-completion, organize imports, etc. and it can be frustrating if the class you’re looking for is defined in other libraries that you never want to use because you’ll always have to manually select it. For example, if you try to use (java.util.)List, Eclipse will prompt you to select either java.util.List or java.awt.List. Well, I am not an AWT developer so I’d prefer to ignore those options. We can configure a type filter by going to Window > Preferences > Java > Appearance > Type Filters. I added java.awt.* to my list of filters and now Eclipse will automatically select java.util.List when I attempt to use it.

Today’s tip:

Window > Preferences > Java > Appearance > Type Filters: configure type filters for content assist or quick fix proposals.

Content assist and templates

Let’s face it, we’d all like to do a little less typing and a little more sailing, wouldn’t we? Ctrl-space is the jack-of-all-trades keystroke that will have you typing less and hitting the water in no time. It goes by the intriguing name of “Content Assist” and I must admit that it’s a little hard to describe exactly what it does but basically, if you type it Eclipse will figure out depending on your context (i.e. where the cursor is) a whole bunch of useful ways to assist you by autogenerating code. One particularly interesting use of Content Assist is with templates. Simply type a keyword (that you can configure – more on that later), hit Ctrl-space, and a drop down of choices will appear. Where you see this icon template_obj.gif beside an option, it means that Eclipse can drop some template code into the editor. Here’s an example: let’s add a new unit test method. Open up a test class, find a blank line and type “Test” then Ctrl-space. If you’re using JUnit4, one of your choices will be “Test – test method (JUnit 4)”. Select it and Eclipse will generate a template for a JUnit 4 test method:

@Test
public void testname() throws Exception {

}

Nice! There are many other examples of Content Assist templates, all of which can be configured however you like in Window > Preferences > Java > Editor > Templates. You can add new ones as well.

Today’s tip:

Ctrl-space: Content Assist to select template code

Older Posts »