Tag Archives: Eclipse

Eclipse Workspace Template

Following on from my Installing Eclipse post, I put together an empty workspace using my recommendations, as well as a couple from the Ivan Moore and Mike Hill session Programming in the Small.

Unzip the template into your workspaces directory and rename it to whatever you want your new workspace to be called. When you start Eclipse, browse to the new workspace directory and have all your code templates set up already.

Download the Eclipse Workspace Template

Please feel free to make suggestions for further improvement.

Installing Eclipse

I’ve just bought myself a netbook, and following the example set by John Smart with his article on installing Eclipse, I’ll document what I do with a clean install of Eclipse.

First Things First

I download the Eclipse IDE for Java Developers. The download is less than half the size of the Java EE edition, and I can always add the extra plugins later (if needed)

Plug it in

I install the mercurial plugin. There is a pattern that I use for pushing mercurial changes to other SCMs (eg. subversion) that I will describe in another post. I intend to use Ivy for my dependency management, so I install IvyDE. I then install the code quality plugins that John mentions, as well as the Metrics plugin. (the following links are the urls for the update sites)

I also install JUnitMax. This is a new plugin from Kent Beck that runs your unit tests after every save. It’s currently on paid beta, and I highly recommend it. Subscribe here, it’s only $2/month

Templates

I update the following templates in Java -> Code Style -> Code Templates

Method Body

// ${todo} Auto-generated method stub
${body_statement}

This evaluates to an empty (apart from the comment) method for void types, or return null;. I’ve already discussed my thoughts on returning null, and I would rather my code failed if it hits an unimplemented method rather than continue in a potentially unsafe manner. So, I change this to:

throw new UnsupportedOperationException("TODO: Implement this method");

Catch block body

// ${todo} Auto-generated catch block
${exception_var}.printStackTrace();

I’ve also discussed my thoughts on checked exceptions. I prefer to not silently hide the exception with a stack trace, I also don’t want to make my callers deal with checked exceptions, so I change this template to:

throw new RuntimeException("TODO: Handle this exception better", ${exception_var});

Test Templates

I also create a new test template in Java -> Editor -> Templates. I copy the Test method (JUnit 4) and add some Behaviour Driven Design style guiding comments. Because I’m lazy, I name it T 🙂

@${testType:newType(org.junit.Test)}
public void should${DoSomething}() throws Exception {
  // Given 
  ${cursor}
  // When
  // Then
}

Favourite Imports

I add the following classes to Java -> Editor -> Content Assist -> Favorites so that I can get type completion on my favourite static imports

  • org.hamcrest.CoreMatchers.*
  • org.hamcrest.Matchers.*
  • org.junit.Assert.*
  • org.mockito.Mockito.*

Code formatting

These are the changes I make to the default Eclipse settings for my personal code.
In Java -> Code Style -> Formatter

Indentation -> General Settings -> Tab Policy => Spaces only 
Indentation -> General Settings -> Indentation size => 2
Indentation -> Indent -> Statements within 'switch' body => on
Control Statements -> General -> Insert new line before 'else' in an 'if' statement => on
Control Statements -> General -> Insert new line before 'catch' in a 'try' statement => on
Control Statements -> General -> Insert new line before 'finally' in a 'try' statement => on
Control Statements -> 'if else' -> Keep 'return' or 'throw' clause on one line => on
Line wrapping -> Line width and indentation levels -> Maximum line width => 132

I also make the following changes to the compiler warning settings (Java -> Compiler -> Errors / Warnings)

Potential Programming Problems -> Serializable class without serialVersionUID => Ignore
Unnecessary Code -> Unused Import => Error