2

I have a pretty big enterprise application, and I'm planning to write a method interceptor that works across all classes in the app. I know I can use Guice for writing such interceptors, however, this approach only works if the target object that contains the method to be intercepted is created by Guice (reference).

Is there a way in which I can create such an interceptor? I do not want to inject the interceptor module since I cannot control object creation through injector.getInstance() site-wide.

Btw, the app is built on Play! Framework.

Community
  • 1
  • 1
Saksham Gupta
  • 223
  • 1
  • 2
  • 8
  • If you can't control instantiation you may have to use ugly(?) things like http://stackoverflow.com/questions/10659610/how-to-instrument-java-methods – zapl Sep 15 '15 at 19:22
  • I'm not knowledgeable enough to provide a full answer on this, but you might find Proxy helpful. http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Proxy.html – Ben M. Sep 15 '15 at 19:24

1 Answers1

1

You need aspectj https://eclipse.org/aspectj/

Taken from the home page of the project:

"Aspectj enables clean modularization of crosscutting concerns, such as error checking and handling, synchronization, context-sensitive behavior, performance optimizations, monitoring and logging, debugging support, and multi-object protocols"

EDIT: You can also look at java agents, introduced in JDK 5:

http://javahowto.blogspot.com.cy/2006/07/javaagent-option.html

http://www.javabeat.net/introduction-to-java-agents/

chrisl08
  • 1,658
  • 1
  • 15
  • 24