Let's say I have a regular simple Java class, such as:
public class Foo {
private int data;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
...and I now wish to, retroactively (i.e. without changing Foo's implementation), do some magic so that I can monitor the "data" field for changes. I'd be perfectly happy if I could just monitor setData() invocations.
I know I could do something close to this, using a java.lang.reflect.Proxy instance and an InvocationHandler. However, the handler is only involved if the data property is changed through the proxy (naturally). And also from what I understand, the proxy needs to implement an interface to become usable. So the following example works fine:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface IFoo {
public void setData(int data);
}
class Foo implements IFoo {
private int data;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
class MyInvocationHandler implements InvocationHandler {
private final Object monitoredInstance;
public MyInvocationHandler(Object monitoredInstance) {
this.monitoredInstance = monitoredInstance;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Invoked!");
return method.invoke(monitoredInstance, args);
}
}
public class ProxyTest {
public static void main(String[] args) throws Exception {
Foo foo = new Foo();
IFoo fooProxy = (IFoo) Proxy.newProxyInstance(
Foo.class.getClassLoader(),
new Class[]{IFoo.class},
new MyInvocationHandler(foo));
fooProxy.setData(42);
System.out.println("foo data = " + foo.getData());
}
}
But my problem with the above is the need for the statically defined IFoo interface. As mentioned above, I'm looking for a solution where I don't have to modify the implementation of Foo at all.
(I have figured out how to dynamically create, compile, and load interfaces like IFoo using the Java Compiler API. I could walk through the method names of Foo.class, find all "setters", and create an interface having those. Still, after figuring all that out, I'm not really sure it would help me at all. :-))
I would like all this monitoring business to be completely transparent to the user of Foo instances. My wish is that I could end up with something really slim, like:
Foo foo = new Foo();
ObjectMonitor om = new ObjectMonitor();
om.monitor(foo);
foo.setData(42); // The ObjectMonitor should notice this.
Is there a way to do this?