So, I realize the answer to this is probably "it's hard", but:
I've got a weird idea, and was wondering if it's possible in Java to create a method like:
<T> T wrapInterface (Class<T> interfaceClass, T wrappedObject) {
if (mClass.isInterface()) {
//create a new implementation of interfaceClass that, in each method,
//does some action before delegating to wrappedObject
return thatImplementation;
}
}
So basically, if my interface Foo defined a method foo(), I'd want this method to create a new class that looked about like this, created an instance of that class with wrappedObject as the constructor parameter, and then returned it:
public class GeneratedClass implements Foo {
private Foo wrapped;
public GeneratedClass (Foo wrapped) {
this.wrapped = wrapped;
}
@Override
public void foo () {
System.out.println("Calling Foo.foo() on wrapped object " +
wrapped.toString());
wrapped.foo();
}
}
The application I'm considering is more complicated than just logging the call, but logging is sufficient for the idea. I'd like to do this with a large number of interface types, which is why I wouldn't just write all the GeneratedClasses by hand.
Bonus points for a solution that doesn't require extra-linguistic features (bringing in AspectJ or something along those lines), and double-bonus-points if this is possible with just the standard JDK libraries.
(I don't need a precise, compilable answer; just a pointer to the right sets of tools/libraries/etc that would let me do this.)
Thanks!