views:

32

answers:

3

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?

+1  A: 

I suggest you take a look at AspectJ as that can do this as before advice.

cletus
+3  A: 

If by "implementation" you mean "source code", maybe aspects can help you. With AOP you can add things like this to your class methods, and some AOP implementations can alter the bytecode of your class, thus ensuring that all usage of your class is detected.

If the bytecode itself can not be modified, you may be out of luck.

Péter Török
Yes, I meant source code. I appreciate the AOP advice, although it feels like killing a mosquito with an elephant gun. But maybe there is no simpler way?
perp
@perp Not that I know of. You want to do magic, and that rarely is simple - even if it looks like it is ;-)
Péter Török
A: 

Program Transformation Systems allow you to apply arbitrary changes to source code from "outside".

You leave your original code alone. You run a transformation engine to produce code containing your desired instrumentation, and you compile and run that instrumented version. You can do this on every build.

A transformation that would work in this case using the DMS Software Reengineering Toolkit would look like:

rule inspect_SetData_updates(p:parameters,b:method_body):method_declaration->methodeclaration
=  "public void SetData() { \b }"
-> "public void SetData(int data) { custom_action(data);  { \b } }"

(The quotes here are not string quotes; they are metaquotes that mark boundaries between the transformation language and the target language text.) This transformation is just a generalization of what a "before" aspect would do in AspectJ.

A more general way of writing this would be:

rule inspect_data_updates(e:expression):statement->statement
= "this.data=\e;"
-> "{ this.data=\e;
      custom_action(this.data);
    }" if (...);

What you need in the ... is a qualifier to select which expressions in your giant program involving the name "data".

Ira Baxter