Eclipse is one powerful IDE. I’ve been using it for a few weeks and in this article, I will be reviewing a few of the features that make it such a wonderful tool.
I’ve been a Vim user for the longest time. Vim has always been a great development tool, but with the complexity of Java development nowadays, I’ve found that IDE’s are no longer candy: they have become a necessity.
I’ve used a number of IDE’s. My first love was Forte. Then, someone explained to me that Forte was based on Netbeans. So I tried Netbeans and I liked it. It was my main IDE for almost two years. But I was still using Vim periodically because some editing was easier in Vim than in Netbeans.
Then, one of my colleagues (Michel, it’s all your fault!) introduced me to IntelliJ IDEA. I was hooked! IntelliJ remains my favourite IDE, even more than Eclipse. But I don’t have the funds to pay for a license, so I looked for the next best thing.
I’d been hearing a lot about Eclipse so I decided to try it. My first use of Eclipse was during the 2.1 era. At that time, it had a bad bug which froze the interface when using cetain versions of GTK+. But late last year, the 3.0 releases began showing up and the bug was gone. I started looking closely at Eclipse and I liked what I saw. I adopted it and since then, I’ve been trying to locate all the little things that can speed development time.
This isn’t an introduction to Eclipse, it doesn’t cover every aspect but it covers enough nifty features to hopefully make you want to give it a spin.
Automate it!
The greatest element of modern IDE’s, I find, is the automation of a lot of repetitive, boring and not very educational tasks. For example, creating new classes (screenshot). It’s always the same: insert the package declaration, put some Javadoc, the class name and maybe a couple of other things.
Most IDE’s have facilities to automate creation of classes. But what if the way it creates them is not the way you need them to be? For example, I have a client who likes his code to look like this:
/*********************** * Class: ClassName * * © 2004, This company, All rights reserved **********************/ package com.foo.package; /** * Class: ClassName * * This class does something. * * Created by: My Name * On: Creation date */ public class ClassName { private c_iAnInteger; ////////////////////////// // getAnInteger() ///////////////////////// /** * Put some text here * * @return */ public void getAnInteger() { return c_iAnInteger; } ////////////////////////// // setAnInteger() ///////////////////////// /** * Put some text here * * @javadoc */ public void setAnInteger(int p_iAnInteger) { c_iAnInteger = p_iAnInteger; } ////////////////////////// // aMethod() ///////////////////////// /** * Put some text here * * @javadoc */ public void aMethod() { ; } }
As you can see, it’s pretty far from the Java Coding Standards. After creating one or two classes like that, I quickly saw that I needed to automate this or I’d lose my mind! That’s where Eclipse’s templates come into play.
Templates for classes
Templates are pieces of code that you tell Eclipse to use when it is generating code for you. You can use special variables that are expanded to insert appropriate code in the generated code. So in the end, Eclipse does everything that doesn’t require any thinking to accomplish, while you have fun writing real code.
This is what a template to generate the above class looks like:
/*********************** * Class: ${type_name} * * © 2004, This company, All rights reserved **********************/ ${package_declaration} ${typecomment} ${type_declaration}
So what does all this mean? Chances are that just by looking at the above code, you have a pretty good idea what it does. Everything that is not a variable will be inserted as is. Variables look like this: ${string}
. In the above snippet, these are the variables used:
${type_name}: This is the name of the class. In our example, it is expanded to ClassName
.
${package_declaration}: The package declaration. Eclipse will generate the package declaration for you automatically, depending on where in your source tree the new file will be created.
${type_comment}: This inserts the comment for the class. Comments templates are defined separately, as we will see later.
${type_declaration}: This is the actual code for the class, with its name, the type modifiers (private
, public
, static
, etc.) and any automatically generated methods.
Once the template for the class is to our liking, we can concentrate on the templates for the various pieces of code.
Templates for methods and comments
The next step is to generate methods. Eclipse allows us to define templates for various types of methods: getters, setters, other methods, constructors and catch blocks. For our purposes, we need to define getters, setters and methods.
This is the template for the getter:
return ${field};
and for the setter
${field} = ${param};
For methods, it’s slightly different:
${method_body}
Simple and elegant. Just how it should be! So now, when we ask Eclipse to generate a getter and a setter for us, it will create the body and the declaration for us. But what about the comment?
There is separate set of templates for controlling automated comments. There is support for seven types of comments: getters, setters, constructors, types, fields, methods and overriding methods. In our example, we need to change the ones for getters, setters and types. Let’s begin with types.
The comments for types are the ones that will appear when you generate a new class. So in our case, we want the comment template to look like this:
/**
* Class: ${enclosing_type}
*
* This class does something.
*
* Created by: Laurent Duperval
* On: ${date}
*/
The interesting element you see is ${enclosing_type}
which is the name of the type for which the comment is being generated. In this case, it will generate ClassName
for us.
Yes, I hard-coded my name. The templates are user-specific, not system-wide. Since I will always be the one to create the code, I’m hard-coding it. It is possible to have Eclipse insert your userid if you wish, however. Just use the ${author}
variable.
Finally, the date is inserted. It will look like this: Mar 30, 2004. I’m not sure if you can change the layout according to your locale. That exercise is left to the reader.
For methods, the template will look like this:
//////////////////////////
// ${enclosing_method}()
//////////////////////////
/**
* Put some text here.
*
* ${tags}
*/
The ${tags}
variable automatically inserts the needed Javadoc tags for us. To complete the Javadoc, we will only need to add the text.
The comments for the getters and the setters are more problematic. Unfortunately, Eclipse doesn’t (yet?) allow us to insert the name of the getter directly in a comment. So using ${enclosing_method}
is not an option. What to do? We have a couple of options: we can either convince our client to change the coding standars, or use editor templates instead of code templates.
The principles of editor templates are very similar to that of code templates, except that there is more interaction with the user. With an editor template, variables are filled in as you type instead of being automatically generated. In our case, it’s not ideal but it’s better than nothing.
Our editor templates are defined in the Editor instead of the code generator. As you can see in the screenshot, there are a number of pre-existing code templates distributed with Eclipse.
The code for our getter template will look like this:
//////////////////////////
// ${name}()
/////////////////////////
/**
* Get the value of ${value}.
*
* @return the value of ${value}.
*/
public ${return_type} ${name}(${arguments}) {
return ${value};
}
Ant the one for the setter will look like this:
//////////////////////////
// ${name}()
/////////////////////////
/**
* Set the value of ${var_name}.
*
* @param ${arguments} the value to set
*/
public void ${name}(${arguments}) {
${var_name} = ${arguments};
}
In order to use these templates, we need to give each of them a name. Since I’m a graduate of the Business School of Branding and Unforgettable Marketing, I decided to call mine
getter
and setter
. Now, to use the new templates, we type their name, followed by Ctrl-Space
. Eclipse will insert the code for us and walk us through every variable defined in the template, so that we can give those variables a proper value. This screenshot shows how that looks. To change from one variable to another, we need to press the Tab
key.Admittedly, it isn’t as convenient as automatic code generation, but it’s better than typing everything by hand.
Now we are able to generate methods, getters and setters with the proper Javadoc. Yes, we’re almost there. The only thing left to do is to take care of those pesky variable names.
Automating variables names
When Eclipse generates a method for a getter or a setter, it capitalizes the first letter of the field and prefixes get
or set
to the getter/setter name. But a method that looks like getC_iAnInteger
is just plain awful. Luckily, Eclipse can be told how to recognize different variable names.
If you have coding conventions that require you to use specific prefixes or suffixes when creating variable names, you can give Eclipse a list of those prefixes and suffixes. Then, when you generate a getter or a setter, Eclipse will strip your prefixes and suffixes and generate nicer-looking names.
You can generate prefixes for fields, static fields, parameters and local variables.
Now when you create getters and setters, pretty much all the boring work is done for you. A field that looks like c_iAnInteger
will generate a decently named getter: getAnInteger()
.
Another bonus is that when you create new variables and press Ctrl-Space
, your dropdown list will offer to create variables for you which contain your prefixes or suffixes. However, if you have many suffixes and many prefixes, this can be a problem more than anything else.
Now get to work
We now have all the pieces we need to make sure that our generated code conforms to a given coding standard. To use them, use the Wizard to create a new Class. Give your class a name, and watch how all that beautiful code appears before your amazed eyes. Then, add a new field and ask Eclipse to generate a setter and a getter for you (or do it manually if you created an editor template). They both should be generated with the proper formatting.
Finally, to create a new method, you need to type some code in the class, select the code you typed and refactor that code into a new method (by pressing Shift-Alt-M). Eclipse will replace the code by a method call. A new method will be created for you, the code you highlighted will be the body of the method, and the comments will look like what we defined in our template.
Templates are just part of what makes Eclipse such a great tool. If you aren’t satisfied with your IDE and you want something better, take Eclipse for a spin. You might just like it enough to adopt it. I know I did.
I used to be one of those l33t vi coders who sniffed at anything with buttons and menus and wizards. However, the latest generations of IDEs have made me a convert. I admit that I sometimes pull up Vim to do minor code changes to a Java class, but that reflex is fading fast.
As for IDEA, yes, I still miss it sometimes. I even find myself a bit annoyed when familiar keystrokes don’t give me the expected results. But if I can forget the name of the girl who gave me my first kiss, I’m sure I can forget all about the IDE I was using last summer.
About the author
Laurent Duperval has been a developer for the past ten years. He is a budding speaker and an active member of the OpenOffice.org mailing lists. He wishes he could contribute more to the OpenOffice.org and the Mozilla projects. He’s a big comics fan.
If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.
Great, informative article, but is any of this specific to 3.0M8, or even 3.0?
Every feature you mentioned could be implemented as a VIM macro. No need for Eclipse.
I haven’t looked into Eclipse yet, but I’d like to thank you for the good article.
i.e. automate by using information in you existing class.
I have a plugin that will digest you project classes into a velocity context so you can use that information to generate other context.
This is great for automating ActionForms, web forms, WebWork actions or other things that a based on using existing information from “Your” beans.
http://www.sutternow.com/CodeStuff/velomix/
sample webwork2 template
http://www.sutternow.com/CodeStuff/velomix/samples/webwork2params.v…
Reformist: This is what happens when you write too late at night. Yes, the version is 3.0M8. But I think some of this is also applicable to 2.1.
dano: yes, I could do it with Vim. But building the macros in Vim and testing them all is much longer. With Eclipse, it just works.
L
And when you read too early in the morning, you don’t pay attention to the title of the article… 🙂
L
Eclipse is turning out to be the only reason many coders bother downloading the JRE at all. Bravo to the team for developing what is without a doubt a kick-ass tool that is not a toy.
Eclipse is becoming the new emacs, an editing platform easily adaptable to new tasks. It would be nice if it was as easy to just edit an old text file though, perhaps too much of a focus on project-oriented work.
I have never used an IDE before I used Eclipse. Since download Eclipse I have tried others just for the heck of it (NetBeans, IntelliJ, Borland and even VS.NET for my C# stuff at work). Eclipse is by far and away my favorite.
The only thing that I hate,and I mean hate, about Eclipse is that it does not have Code Folding or Code Collapsing as other IDE’s do. VS.NET has it, IntelliJ has it…why can’t Eclipse?
Anyone know of a plugin to do this, btw? If so, that would rock, but this should be something that is built in.
Anyway…Eclipse is pretty awesome. I do almost all my dev in it now.
jason
For Java I use http://www.Netbeans.orgthe only thing bothering me is that it only supports Java and jPython. What other languages does Eclipse support if any?
Eclipse supports almost any language. It does this through plugins
http://eclipse-plugins.2y.net/
I do PHP, Python, XML, Mono, Perl, HTML, Java, and some SQL in it.
jason
On the cold folding, I believe I read on another site that it’s coming into the 3.0.x series. I’ll try to find the link.
As to Eclipse, it is one of the killer applications, the must haves, for me. When I first tried it out I was not so excited, thought it was a bit cumbersome. That was simply not knowing the product. A lot of the beauty of Eclipse though is the plugin architecture. If you have a feature or extension you wish you had, odds are someone’s already created it.
These two sites list plugins made by others: http://www.eclipse-plugins.info/eclipse/index.jsp and http://www.eclipse-workbench.com/jsp/plugins.jsp
A discussion on one of the plugin vendor sites, http://www.myeclipseide.com/index.php?name=PNphpBB2&file=viewtopic&… , mentions cold folding being mostly complete but not running in M8 – and most likely turned on in M9.
Here’s another interesting bit on the plugin front, Mozilla is apparently going to release an XUL builder for Eclipse. http://www.mozillazine.org/talkback.html?article=4414
The author barely mentioned the support for refactoring, which was largely copied from IntelliJ IDEA.
I wander how long will it take to replicate this effort using VIM macro, if at all possible.
Harel
Do you mean code folding, or is this a variant of it? If so, that sounds like a really nice feature. Very handy when I need to focus on just part of a project (as implemented in VS.NET anyway).
The CDT provides a complete C/C++ development environment for Eclipse as well. It is what QNX, LynxWorks, WindRiver, TimeSys, RedHat and others are using for the basis of their C/C++ IDE offerings.
http://www.eclipse.org/cdt/
With jde for emacs, you can set up those templates with all the niceties of cusomize-variable. There are also several refactoring tools available for emacs.
My biggest complain bout Eclipse: I can’t have multiple editor windows going. Tabs are fine but I prefer to have multiple windows most of the time. For example, I have like 4 shell+vi windows open usually when I code.
Anyone know of any tricks for multi-editor window?
If there is an application worth buying, it is IntelliJ IDEA. It is an unbelievable piece of work. However, for people who hack with Java in their spare time, Eclipse is God sent. Eclipse is one of those projects that gives me hope for open source projects. It is an examplary model I feel many open source should emulate.
If you have not already tried kdevelop, it will stand out on its own merits.. but if your a vim user, then you should definitely try the kvim texteditor kpart.
Thanks for the link…I am starting to get really excited about that…..
Thanks
crypt0 – You can drag the tabs around and put them beside or below other editors. Basically lets you re-arange your editors within the editor area like you do views within the workbench window.
root – The reason why Eclipse is so good is because it wasn’t started as an open-source project. It is an IBM product that IBM open-sourced. The VisualAge/WebSphere products prior to eclipse where all based on the same code base. It also has a very strong internal management scheme due to the fact that most of the contributors are paid by eclipse member companies to work on it. We (QNX) have 3-4 full time people working on the CDT, for example.
After reading this article, Randy Faust, one of the Eclipse developers, did something I should have done while writing the article: he logged a bug and provided a patch for the getter/setter templates. This patch allows one to insert the name of the method in the generated documentation. The bug is here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=58061
I deliberately chose not to talk about other Eclipse features (such as plugins and refactoring). I’ll keep those for a later article, once I have enough material to cover those aspects.
I’m not an emacs fan, except for Ctrl-E and Ctrl-A. 🙂
Once the XUL plugin is available, I hope to get a chance to try it out and report. Of course, I’d need to master XUL a bit beforehand…
L
I love people that bash something that is open source. Coz they will say something like, “Well **** does not support ****” and then the next release will have it, unless *** was a dumb idea. I really do not see the point of bashing good OSS ???
Code folding and generic templates are in the plan: http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/pla…
You can also use ‘Show source of selected element only’ combined with the Java Browsing perspective to give you a visual age L&F.
As for refactoring and IntelliJ, after using intelliJ I went back to Eclipse because its refactoring was better in the 3.0 stream – try it out.
Tear off windows are now available and you have always been able to open more windows with complete perspectives in them.
Channing
Not every open source project has the backing of IBM, which recently contributed $40m dollars for Eclipse project.
Laurent,
Excellent article! Columns like yours do a great job of pointing out one facet of a topic and giving it the space it needs to be properly understood. Also, introducing templates and macros in the context you mentioned made it easier to see how and when they should be used.
Great job!
Carlos