java - 可以使用 ByteBuddy 而不是被调用的方法来检测方法调用吗?
问题描述
我想替换一些保护对java.lang.System
某些用户代码的调用的 AspectJ 代码。
java.lang.System
不能/不应该被检测。
使用 AspectJ 的解决方案是像以下示例一样检测调用代码。应该保护的代码将被检测,而允许的代码则不被检测。
@Around("call(public long java.lang.System.currentTimeMillis()) && within(io.someuserdomain..*) && !within(io.someotherdomain..*))
def aroundSystemcurrentTimeMillis(wrapped: ProceedingJoinPoint): Long = {
throw new IllegalStateException("must not call System.currentTimeMillis in usercode")
}
有没有办法使用 ByteBuddy 做同样的事情?到目前为止,我只找到了有关如何检测被调用者而不是调用者的示例。
解决方案
您目前可以通过注册 a 来替换方法或字段访问,MemberSubstitution
但与 AspectJ 相比,这些功能仍然有限。例如,不可能像您的示例代码那样抛出异常。但是,您可以委托给包含引发异常的代码的方法:
MemberSubstitution.relaxed()
.method(named("currentTimeMillis"))
.replaceWith(MyClass.class.getMethod("throwException"))
.in(any());
上述替换将用对以下成员的调用替换任何方法调用:
public class MyClass {
public static long throwException() {
throw new IllegalStateException();
}
}
替换将应用于应用访问者的任何方法。您可以注册一个AgentBuilder.Default
来构建一个 Java 代理,或者查看 Byte Buddy 的构建插件。
推荐阅读
- c# - 我的简单 Lockbits Image 类中有一个错误
- git - Git 推送到 Heroku 永远不会从写入和总计对象消息的步骤传递
- angular - NGXS Select Observable上的RxJS超时?
- python - 使用 pybind11 在 C++ 之间传递 Python 大整数(>256 位)
- r - 在R中的模式之间粘贴字符串
- python - 即使尝试除了代码块,错误也会不断出现
- data-structures - 绳索和自平衡二叉树混合?(即具有快速第 n 个元素查找的排序集)
- termux - 不要从 github 打开项目
- javascript - 来自更高层次的 jQuery 内容?
- leaflet - 使用自定义模块在 Drupal 上的组外标记上使用控制层时出现问题