How do I intercept a method invocation with standa

2019-01-05 02:39发布

I want to intercept all method invocations to some class MyClass to be able to react on some setter-invocations.

I tried to use dynamic proxies, but as far as I know, this only works for classes implementing some interface. But MyClass does not have such an interface.

Is there any other way, besides implementing a wrapper class, that delegates all invocations to a member, which is an instance of the MyClass or besided using AOP?

8条回答
甜甜的少女心
2楼-- · 2019-01-05 02:48

Some of the Java gurus might frown upon this but I've had some good success with avoiding primitive types and setters altogether. My class looks like this:

class Employee extends SmartPojo {
    public SmartString name;
    public SmartInt age;
}

You'll notice two things: 1. everything is public. 2. No constructor.

The magic happens in SmartPojo which searches for any field which implements the "Smart" interface and initializes it. Since this is no primitive (and no final class), I can add set() and get() methods for all fields anywhere in my model in a single place. So no setter/getter wastes anymore, it's stunningly simple to add notification (also in a single place), etc.

True, this is no POJO anymore and it's not a Bean in most ways but I've found that these old ideas limit me more than they help. YMMV.

查看更多
虎瘦雄心在
3楼-- · 2019-01-05 02:49

For interception of java method invoketion an AspectJ is good tool to use. Through which can intercept mthod calls, executions etc.. efficiently. I am usin the same tool for intercepting java method calls. We can apply pointcuts ansd aspects to Intercept any java method calls. for mere information read this

查看更多
forever°为你锁心
4楼-- · 2019-01-05 02:51

Java doesn't have any actual language features for method interception (not sure any static language does)

I kinda like Nick's idea of using the debugger interface, that's just mean.

I think the short answer you need is: No there isn't a way of intercepting a method call in Java without actually replacing the class using a proxy or wrapper.

Note: The AOP libraries just make this happen automatically.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-05 02:55
  1. Why cannot your class implement an interface? You could just extract some interface from it containing all the methods that you want to intercept and use the dynamic proxies mechanism easily. It's also a good programming practice to code with interfaces and not classes.

  2. You could use Spring framework with Spring AOP capabilities (which are using dynamic proxies inside) to do it. You will just have to define your class as a Spring bean in the configuration file and clients of your class will have to either get its instance from the Spring application context or as a dependency automatically (by defining the setMyClass(MyClass mc) method for instance). From there you can easily go to defining an aspect that intercepts all the method calls to this class.

查看更多
放荡不羁爱自由
6楼-- · 2019-01-05 02:56

I just developed a small framework for this purpose. You can check it out at: http://code.google.com/p/java-interceptor/ (use svn to check out).

查看更多
倾城 Initia
7楼-- · 2019-01-05 03:00

If you are prepared to do something really ugly, have a look at:

http://docs.oracle.com/javase/7/docs/technotes/guides/jpda/

Basically the debugger interface ought to allow you to attach like a debugger, and hence intercept calls. Bear in mind I think this is a really bad idea, but you asked if it was possible.

查看更多
登录 后发表回答