简体   繁体   English

如何调用一个方法作为对 Java 中另一个方法调用的响应?

[英]How to call a method as a response to another method call in Java?

Suppose you have a Library L.jar which contains class C.class and a method m .假设您有一个库L.jar ,其中包含 class C.class和 AB m方法。

Now, you have a project which imports this library and uses the m method.现在,您有一个导入该库并使用m方法的项目。

In the project there is class X with method mX .在项目中有 class X方法mX

What I want to do is to somehow monitor all invocations of the method m and invoke the method mX whenever method m gets invoked.我想要做的是以某种方式监视方法m m调用方法mX

If I can somehow read the parameters which get passed to method m when invoking it, that would be awesome.如果我可以在调用它时以某种方式读取传递给方法m的参数,那就太棒了。

Is this possible in Java and if so, how to do it?这在 Java 中是否可行,如果可以,该怎么做?

There are two approaches.有两种方法。 You can either use AspectJ to intercept your program calls.您可以使用AspectJ来拦截您的程序调用。 OR You should be wrapping L.jar method m into a class of yours and expose that class to end user.或者您应该将L.jar方法m包装到您的 class 中,并将该 class 暴露给最终用户。 end user should be using this class to invoke m .最终用户应该使用这个 class 来调用m now you will have full control on who is calling m with which parameters.现在您将完全控制谁使用哪些参数调用m The downside is that user can always call m directly and bypass your code.缺点是用户总是可以直接调用m并绕过您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM