Hello,
Currently i am working on a dynamic proxy, i can get it to work for most objects however when i am trying to use my dynamic proxy to record the performance of wait and notify calls.
However my invoke method in my proxy never appears to be called. I have tested the proxy with a RMI stub and it works, however i can't seem to get any of the methods of java.lang.object to be invoked...
Please help, i have tried googling to find a solution but i can't find anything...
Code bellow:
My proxy:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.io.*;
public class Logger implements InvocationHandler {
private Object delegate;
public Logger(Object o) {
delegate = o;
}
public static Object newInstance(Object obj) {
return java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
new Logger(obj));
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("Test here");
BufferedWriter loggingWriter = new BufferedWriter( new FileWriter("log.txt"));
loggingWriter.write("Calling method " + method + " at " +System.currentTimeMillis());
loggingWriter.newLine();
try {
return method.invoke(delegate, args);
}
catch (InvocationTargetException e) {
throw e.getTargetException();
}
finally {
System.out.println("Test here");
loggingWriter.write("Called method " +method + " at " +System.currentTimeMillis());
loggingWriter.newLine();
loggingWriter.close();
}
}
}
synchronized(delegate) {
return method.invoke(delegate, args);
}
my
test program is basically as follows:
private static RemoteInterface wrapper;
//blah blah
wrapper =
(RemoteInterface) Proxy.newProxyInstance(
this.getClass().getClassLoader(),
this.getClass().getInterfaces(),
new Logger(this));
//blah blah
//then within a syncronized call:
wrapper.wait();
The program still works 100% however it bypasses my invoke method in my proxy somehow, i can't figure it out for the life of me. Any help would be greatly apperciated.