Domanda

I need to intercept the calls to all the method calls to an Interface. I've gone through the Java Dynamic Proxies however that will not help me. I'm not even sure whether this can be achieved, but thought of confirming.

So basically lets say i have an interface as follows:

public interface Foo {        
   public String getValue();
}

I would like to intercept all the calls to getValue() from whichever implementations of Foo. Problem is i do not have control over the different implementations of Foo, because of which i cant use Dynamic Proxies.

Is there a way i can do this?

Thanks.

È stato utile?

Soluzione

AOP might help, but as you've discovered, it all gets much easier if you're in control of the object creation (even if only through a DI framework like Spring or Guice).

Another alternative is compile-time byte-code weaving - that is, finding all implementations and altering them to have your interception code in them at compile time.

A third alternative would be to look at using either an agent or a custom classloader to do weaving as the classes are loaded into the system. This is load-time weaving. But if you're in, say, a web container where you're not fully in charge of the classloaders, this might be tricky.

Altri suggerimenti

The only way to do this would be with a custom classloader that replaces the implementation classes with a proxy.

It might be possible to do this out-of-the-box with Spring AOP's load-time weaving.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top