Eclipse IDE provides many features to help a developer in development activities. Help to write code, integrate with servers, deployment, quality check and what not. In this article, I am consolidating those eclipse features which help us in improving productivity. Here I have used Eclipse Galileo and for unit test generation it is JUnit 4.4.

1. Getter and Setter Generation

Open a java file in java editor, right click at the place where you want getters and setters implemented. The follow the menu Source > Generate Getters and Setters… This will generate getters and setters. You can select the variables for which getters and setters are to be generated. Also, access modifiers and comment generation can be changed for these methods. Any comment generation can have a template associated with it.

Eclipse Features

2. Content Assist

Simplest code generator provided by eclipse is the code assist. Eclipse is improving it with every release.  Either you can have the content assist working automatically or you can manually enable it. Automatic working happens when you type dot (.) and wait for a fraction. To get the assist manually, press Ctrl + Spacebar.

3. Creating Standard Methods (hashCode(), equals() and toString())

Many classes require these methods overridden from Object class. Mostly data classes expect the implementation to ensure different operations.  Wherever you want to generate implementation of these methods, right click Source > Generate hashCode() and equals()… This will open dialog box like this and required methods can be generated.

Similarly generate toString() option will generate toString() implementation. Here you can also select whether you want to go for non performing string concatenation or StringBuilder/StringBuffer.

4. Overriding Methods from Base Class

Skeleton of methods to be overridden from base class can be generated by right click Source > Override/Implement Methods… This will open following dialog box where you can select required methods to be overridden/implemented.

5. Main Method Skeleton Generation

This has to be done when you are generating new class in eclipse.  In this box below, select checkbox for the main method generation and main method will be inserted in the generated class.

6. Variable Help

When you hit Ctrl + Spacebar, it brings out content assist, if it is at a place where a variable is to be inserted then the suggestions do include variables available in that scope along with methods.

7. Quick Fix to Checkstyle Warnings

If you have checkstyle enabled and there are many warnings shown in the editor. By pressing Ctrl + 1 or using right click Quick Fix, you can opt to allow eclipse to fix the warning.

8. Organizing Imports

If you go for manual fixing then this boring task does take more time. It improves readability hence it is required and you can achieve it by right click in the class, and selecting Source > Organize Imports…

9. Javadoc Skeleton Generation

Wherever you want java doc to be generated type /** and press enter. It will generate java doc relevant to the cursor place. If it is method then it will show parameters, return value and exceptions in the generated javadoc block.

10 JUnit Generation

Another big chunk or code is generated using this feature. Right click on the class for which the test case is to be generated and select menu New > JUnit Test Case. Following dialog box will be opened. After entering details and pressing next button, you can select methods for which you want the JUnit code to be generated.

EclipseProductivityDemo.java

/**
 * 
 */

/**
 * @author 
 *
 */
public class EclipseProductivityDemo {
	private String myVariable = "";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((myVariable == null) ? 0 : myVariable.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof EclipseProductivityDemo)) {
			return false;
		}
		EclipseProductivityDemo other = (EclipseProductivityDemo) obj;
		if (myVariable == null) {
			if (other.myVariable != null) {
				return false;
			}
		} else if (!myVariable.equals(other.myVariable)) {
			return false;
		}
		return true;
	}

	/**
	 * @return the myVariable
	 */
	protected String getMyVariable() {
		return myVariable;
	}

	/**
	 * @param myVariable the myVariable to set
	 */
	protected void setMyVariable(String myVariable) {
		this.myVariable = myVariable;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("EclipseProductivityDemo [myVariable=");
		builder.append(myVariable);
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#finalize()
	 */
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

}

EclipseProductivityDemoTest.java

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * 
 */

/**
 * @author 
 *
 */
public class EclipseProductivityDemoTest {

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link EclipseProductivityDemo#main(java.lang.String[])}.
	 */
	@Test
	public final void testMain() {
		fail("Not yet implemented"); // TODO
	}

	/**
	 * Test method for {@link EclipseProductivityDemo#getMyVariable()}.
	 */
	@Test
	public final void testGetMyVariable() {
		fail("Not yet implemented"); // TODO
	}

	/**
	 * Test method for {@link EclipseProductivityDemo#setMyVariable(java.lang.String)}.
	 */
	@Test
	public final void testSetMyVariable() {
		fail("Not yet implemented"); // TODO
	}

}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS