There are tons of excellent plugins for profiling code in eclipse. But that’s probably a bit heavyweight if all you’re after is a crude idea of how many lines of code you’ve written. Obviously you could just turn on line numbers and count the bottom line number in each class on your favourite calculate app. But thats going to get tedious for 20, 100, 1000 files, but ah ha! There is an easier way.

The search facility of Eclipse is extremely powerful, and as well as being good for searching for a snippet of text or a specific declaration, its facility for regular expressions opens up a world of possibilities. But how do you count line numbers you say? Well, allow me to tell you…

Ensure the project you wish to count line numbers for is selected in the Package Explorer. Then, go to the search menu and hit the File… menu item. The search dialog should open on the File Search tab.

Eclipse's search functionality makes all-sorts possible

Eclipse's search functionality makes all-sorts possible

On the right, check the box to use regular expressions and enter our search term as the new line character “\n”. We need to make sure we’re only searching source files, so for file name pattern enter “*.java” if it’s Java you’re working with, or whatever file extension your language choice uses. Finally, make sure Enclosing projects is the selected scope at the bottom. If you wanted to count line numbers over multiple projects you could use workspace or a working set. And we’re done. Hit the Search button and allow Eclipse to do its thing.

Bosh! The number of matches returned is your line count. Of course, this includes comments and blank lines. But if you were picky about it you should be able to write a more fancy regular expression to get what you want.

Creative use of the search feature makes all sorts of other repetitive tasks easy. For example, I used the replace facility yesterday to quickly insert a GPL licence message at the top of every java file, just by searching and replacing on package names. A powerful tool to keep in mind!

Whenever you prepare a release of a Java project, it’s a good idea to create some Javadoc documentation to accompany it. This is especially true for an open source project. Generating Javadoc with the Eclipse IDE really couldn’t be much easier.

  1. Select Project -> Generate Javadoc… from the menubar.

    Eclipse's Javadoc dialog

    Eclipse's Javadoc dialog

  2. The first time you use the Javadoc tool, you may need to set the Javadoc command. Hit the configure button and navigate to the bin directory of your JDK, you’re looking for javadoc.exe. On windows this is likely to be something like: C:\Program Files\Java\jdk1.6.x_xx\bin\javadoc.exe, on linux it may be more like /usr/bin/javadoc.
  3. Check the checkbox for the project you want Javadoc to be generated for.
  4. You probably only want Javadoc for members with Public or Protected visibility.
  5. Hit Finish, and that’s it. Your Javadoc will be generated in a doc directory in your project root.
  6. Open doc/index.html in your browser to view.

Remember that the quality of your Javadoc is dependent on the quality of your comments. For information about writing good doc comments see the Sun style guide.

© 2012 TC33.org Suffusion theme by Sayontan Sinha