Jun
24
Filed Under (Technology) by Thejesh GN on 24-06-2008

I have seen developers writing toString method in each of their bean class. The purpose is to print the bean variables for logging or debugging purposes. I thought of reducing their effort by writing a generic toString method to have in their base bean class. It works for simple bean classes. Its no-brainer you can modify to work for others.


public String toString() {
	String lineSeaparator = System.getProperty("line.separator");
	StringBuffer buffer = new StringBuffer(lineSeaparator);
	buffer.append("|---------");
	buffer.append(this.getClass());
	buffer.append("---------|");
	buffer.append(lineSeaparator);
	Method[] methods = this.getClass().getMethods();
	if(methods != null && methods.length >0){
		Method method = null;
		for(int i =0; i< methods.length; i++){
			method = methods[i];
			if(method.getName().startsWith("get")
			&& !method.getName().startsWith("getClass")){
			buffer.append
                                (method.getName().replaceAll("get",""));
			buffer.append(" = ");
			Object[] params=null;
			try {
				buffer.append
                                   (method.invoke(this, params));
			} catch (Exception e) {
				buffer.append("  ");
			}
				buffer.append(lineSeaparator);
			}
		}
	}
	buffer.append("|---------------------------------------------|");
	buffer.append(lineSeaparator);
	return buffer.toString();
}

Any suggestions to improve are welcome.



May
23
Filed Under (Technology) by Thejesh GN on 23-05-2008

Myself and Veetrag caught up with Greg Murray sipping his coffee at Developers Summit. As usual we started talking and in between I realized, we could as well record it. Hence the start is abrupt and volume is low.
Greg Murray is an AJAX architect at Sun. He is known for his contributions to OpenAJAX Alliance and Dojo. He is the architect of Project jMaki. jMaki uses the best parts of Java and the best parts of JavaScript to deliver rich AJAX style widgets through a singe, easy-to-use interface that accesses components from popular widget libraries such as Dojo, Script.aculo.us, Yahoo’s UI Library, Spry, DHTML Goodies, and Google’s Web Toolkit.



Dec
17
Filed Under (Uncategorized) by Thejesh GN on 17-12-2007

Simple shutdown hooks can often be written as anonymous inner classes, as in this example:Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { call_ur_method(); }
});
This idiom is fine as long as you’ll never need to cancel the hook, in which case you’d need to save a reference to the hook when you create it.

More about Java Application shutdown hook design