Friday, July 27, 2007

TODO and FIXME notes

This may help if you developing software in team.
We may add comment on our code as notes for later changes/implementation.

public class Monitor {
public void doMonitor() {
// TODO implement this method
// FIXME create specific exception
throw new IllegalArgumentException("Not implemented yet");
}
public void process() {
// FIXME bugs ....
....
}
}

But when the source is already unified with other source code on the same team, ("TODO implement this method") we might ask "who write this notes? the system is on production state and there are still unimplemented code!" or another case is "FIXME find bugs ....", someone find bugs, but the original writer can read and say "thanks, you save my life by finding my bugs" but he/she didn't know who the one find the bugs.

So it might a good idea if we write a name on our comments


public class Monitor {
public void doMonitor() {
// TODO uudashr: implement this method
// FIXME uudashr: create specific exception
throw new IllegalArgumentException("Not implemented yet");
}
public void process() {
// FIXME uudashr: bugs ....
....
}
}

Monday, July 16, 2007

Reflection API

It started 2 years ago, when I have to develop a web based system using PHP.
I'm not a PHP expert, so I asked my friends who works as PHP Developer a question
"What are they usually need for developing system? What is the best framework did they
use?" So 2 frameworks comes, Eocene and Mojavi.

I'm trying to learn about Mojavi, and I have to learn so many things to start my
project. Oh my god. So I stopped and think "Why don't I learn the concepts, and create
the framework by myself, so I don't have to waste time to learn features and
regret if the framework didn't fit to me".
So I started my simple framework using OO on PHP.
The problem is "how can I handle with unexists class on code time, but exist on
run time?" Google comes to help and I found "reflection".
So there is reflection API on PHP as well.
After that I learn reflection on Java.

Here is an example of how to use reflection API.

package uudashr.reflection;

public class Example {
public void hello() {
System.out.println("This is hello method");
}

public String toString() {
return "This is example instance [from toString method]";
}
}

How to handle this simple class using reflection API? watch this.

package uudashr.reflection;

import java.lang.reflect.Method;

public class GettingClass {
public static void main(String[] args) {
testCreateInstance();

testExecutingMethod();
}

public static void testCreateInstance() {
try {
Class exampleClass = Class.forName("uudashr.reflection.Example");
Object obj = exampleClass.newInstance();
System.out.println(obj.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

public static void testExecutingMethod() {
try {
Class exampleClass = Class.forName("uudashr.reflection.Example");

Object obj = exampleClass.newInstance();

Method helloMethod = exampleClass.getMethod("hello", null);
helloMethod.invoke(obj, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}