One holiday, please! Comin' right up!
Yeah yeah yeah .. We're going to Rumania!! yeah yeah yeahhhh
Later!
Yeah yeah yeah .. We're going to Rumania!! yeah yeah yeahhhh
Well - if you like me ever wanted something much like the C# delegate or a pointer to a method, in java - this might be for you. Its not quite there, but this is as close as I could come... Written in notepad on a cloudy evening..
import java.lang.reflect.*;
public class Test
{
public void objectCallback(String parameter)
{
System.out.println("objectCallback("+parameter+")");
}
public static void instanceCallback(String parameter)
{
System.out.println("instanceCallback("+parameter+")");
}
public void add(int val1, int val2, Delegate delegate)
{
int result = val1 + val2;
try {
delegate.delegate(new Object[] { String.valueOf(result) });
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Test start");
Test test = new Test();
test.add(3, 3, new Delegate(test, "objectCallback"));
test.add(3, 3, new Delegate(Test.class, "instanceCallback"));
System.out.println("Test end");
}
public static class Delegate
{
private String methodName;
private Class clazz;
private Object object;
public Delegate(Class clazz, String methodName)
{
this.clazz = clazz;
this.methodName = methodName;
}
public Delegate(Object obj, String methodName)
{
this.object = obj;
this.methodName = methodName;
}
public void delegate(Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
if (object != null) {
Class objClass = object.getClass();
Method method = objClass.getMethod(methodName, mapArgs(args));
method.invoke(object, args);
} else if (clazz != null) {
Method method = clazz.getDeclaredMethod(methodName, mapArgs(args));
if (Modifier.isStatic(method.getModifiers()))
method.invoke(null, args);
else
throw new NoSuchMethodException("Method "+method+" is not static on class "+clazz);
}
}
private final static Class[] mapArgs(Object[] args)
{
if (args != null) {
Class[] cArray = new Class[args.length];
for (int i=0;i<args.length;i++) {
if (args[i] != null)
cArray[i] = args[i].getClass();
else
cArray[i] = Object.class;
}
return cArray;
}
return null;
}
}
}
Where to start? well - having just read this I actually first wanted to copy-paste a few paragraphs to get you, the reader, to think about the concept behind an agile approach towards software development. Well I soon found myself out of space on this blog-entry, so I decided not to, but really - you ought be reading the above linked PDF.. Its "Good Stuff"(TM)!
Not so long ago I started using Commons Configuration for most my configuration needs. Even though I dislike some Commons (especially Commons Logging) packages I really do like the Commons Configuration package. Its neat and the XMLConfiguration is indeed useful. Well - anyways, I've been a strong believer in microkernels (or atleast the idea behind the microkernel pattern), but sometimes this leads to each "kernlet" having to configure itself and what about re-configurations of a running system? (perhaps simply by changing the value of a node in an XML config file) What if we had a microkernel capable of reading a "master" config file, describing which "kernlets" should be started and each kernlet was infact a javabean. Then by using the java.beans package and all its goodies, kernlets were able to be notified when-ever a property changed and they were able to veto a property-changed-event if such an event was not wanted at the time being. Hmmm.. throw in some JMX capabilities too and you might just have yourself a sweet kernel...(now dont scream JBO$$ kernel, please!! *pfft*)