Eclipse has a nice feature called Quick Fix where if you type in something that doesn’t compile, it thinks of things you could do to fix it. For instance, if you type in the name of a class it can’t find, there’s a little X with a light bulb (which I translate as “Something’s wrong but I have an idea how to fix it”). I can then click on the X-with-the-light-bulb (or better yet, press Ctrl+1) and choose a quick fix from the list.
I had known for several weeks about being able to click on the X/light bulb icon, but having to stop typing and go click over there was enough impedance that I generally would just do the work myself. Just in the last day or so though, I found out about the Ctrl+1 shortcut…
Let me demonstrate how this helped me today.
I had renamed my MessageSender class to MessageSenderImpl, and then performed Extract Interface to recreate MessageSender as an interface. After I helped Subversion understand, I was then ready to create a new implementor of the interface: a UserSvcAwareMessageSenderImpl.
- I started UserSvcAwareMessageSenderImpl’s sendMessage() implementation with the code from the plain MessageSenderImpl:
- I changed the name of the class it was instantiating to UserSvcAwareMessageCreator — which doesn’t exist yet. I got the red underscore:
- I press Ctrl+1 to pop up the Quick Fix list:
- “Create class ‘UserSvcAwareMessageCreator'” is already highlighted, so I just hit Enter. This pops up the Create New Class dialog, where the defaults are fine, so I hit Enter again.
- Eclipse has put me in the new UserSvcAwareMessageCreator class. I click on the TODO comment and press Ctrl+F1, which comes up with the single suggestion “Remove task tag”:
- I hit Enter and the comment is gone (Click, Ctrl+1, Enter is more efficient than my usual Click, Home (to get to beginning of line), Shift+DownArrow, Delete).
- I intend to pass session.createTextMessage a String that’s passed in to UserSvcAwareMessageCreator’s constructor, but I haven’t created the field nor the constructor yet. I type the code anyway:
- I press Ctrl+F1…
- …and down-arrow to “Create field ‘messageText'”:
- I place the cursor on the new messageText field, press Shift+Alt+S (could instead have pulled down the Source menu but I’m partial to keyboard shortcuts), and choose Generate Constructor using Fields…
- And there we are, ready to fill in the rest of the createMessage() method:
Using the Quick Fix feature, I got all this set up in way less time than it took to type about it! :)