Monthly Archive for July, 2010

Code coverage goal: 80% and no less!

But it is nice to see green overall on your code coverage reports, isn’t it?

nwire for Java

A seemingly interesting Eclipse plugin. Unfortunately it is not free.

Eclipse Tips: Open Implementation

Eclipse Helios has another subtle enhancement to the JDT. It is called as Open Implementation. So what’s that? Before we play around with the feature, let’s create some classes.

interface IProcessor {
	void process();
}
public class Processor1 implements IProcessor {

	@Override
	public void process() {
		// TODO Auto-generated method stub
	}

}
public class Processor2 implements IProcessor {

	@Override
	public void process() {
	}

}
public class Main {

	public static void main(String[] args) {
		new Processor1().process();
	}

	public static void a(IProcessor p) {
		p.process();
	}
}

Before Open Implementation is implemented, we have only Open Declaration with F3 as default keybinding. Try this on Eclipse before Helios. Put your text cursor on method process on statement p.process(); in class Main. Click F3. You’ll be brought to the process method in IProcessor.

But what we need often is not the declaration, we need the implementation of the method. Before Helios, what I do is to go the declaration, click Ctrl+T and pick the implementation I want.

Helios shortens this. Put your text cursor again on the method and click menu Navigate → Open Implementation. Now if you have more than one implementation of the method, you will get choice to pick which implementation to open.

By defining a keybinding on Preferences → General → Keys you can even use the feature easier, but before you do that, see if this shortcut is fast enough for you.

Click Ctrl and hold it. Now move your mouse over the same method. Tadam… you will get choice.

If you pick Open Implementation you’ll get the same choice as before.

So that’s it. Hopefully this tips helps you a lot.

Google Docs: Before and After

How you compare the old Google Docs and the new one?

Here is the old:

Here is the new:

I like the new one much better.

Java Tips: Process Object Based On Its Type Without if-then-else Solution

I want to share my answer for this question on StackOverflow.

Say you want to process several objects with different types. Each type must be processed differently but some concerns are:

  1. You don’t want if-then-else solution which is obviously not great for long-term
  2. Configuration is also bad for the same reason

So how is the solution? This is the solution that is using library from Reflections.

public class A {

}
public class B {

}
import java.lang.reflect.ParameterizedType;

public abstract class Processor<T> {

	private final Class<T> processedClass;

	public Processor() {
		ParameterizedType parameterizedType =
			(ParameterizedType) getClass().getGenericSuperclass();
		processedClass =
			(Class<T>) parameterizedType.getActualTypeArguments()[0];
	}

	public Class<T> getProcessedClass() {
		return processedClass;
	}

	protected abstract void process(T message);

}
public class ProcessorA extends Processor<A> {

	@Override
	protected void process(A message) {
		System.out.println("Processing object A");
	}

}
public class ProcessorB extends Processor<B> {

	@Override
	protected void process(B message) {
		System.out.println("Processing object B");
	}

}
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.reflections.Reflections;

public class Adapter {

	private Map<Class<?>, Processor<Class<?>>> mapping
		= new HashMap<Class<?>, Processor<Class<?>>>();

	public Adapter() throws Exception {
		Reflections r = new Reflections("");
		Set<Class<? extends Processor>> subTypesOf =
			r.getSubTypesOf(Processor.class);

		for (Iterator iterator = subTypesOf.iterator();
                     iterator.hasNext();) {
			Class<? extends Processor> c =
				(Class<? extends Processor>) iterator.next();
			Constructor<? extends Processor> constructor =
				c.getConstructor();
			Processor p = constructor.newInstance();
			mapping.put(p.getProcessedClass(), p);
		}
	}

	public <T> Processor<T> getProcessor(
			Class<? extends T> c) {
		return (Processor<T>) mapping.get(c);
	}
}
public class Main {

	public static void main(String[] args)
			throws Exception {
		Adapter adapter = new Adapter();

		A a = new A();

		adapter.getProcessor(a.getClass()).process(a);

		B b = new B();

		adapter.getProcessor(b.getClass()).process(b);
	}

}

The console after running main method:

14:01:37.640 [main] INFO  org.reflections.Reflections - Reflections took 375 ms to scan 4 urls, producing 222 keys and 919 values
Processing object A
Processing object B

It’s kind of magic, isn’t it?

2000! Yippi!

I can now edit a post on StackOverflow.

Reveal Hidden Menu Bar Options

Nice tip! Keep remember that holding Option key while clicking a menu bar icon will reveal hidden options.

Eclipse ID Fan Page

I think I found a new habitat.

Manage OS X's downloaded file warning system

If you are also annoyed with the warning dialog when opening a downloaded file, this is the solution.