如何劫持/拦截来自第三方jar(甚至JDK)的类的方法? [英] How to hijacked/intercept method of a class which from third party(EclipseLink) jar or even JDK?

查看:153
本文介绍了如何劫持/拦截来自第三方jar(甚至JDK)的类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试劫持/拦截EclipseLink中类的方法.我曾尝试过Spring AOP和AspectJ,但失败了.我想在类org.eclipse.persistence.internal.localization.i18n.TraceLocalizationResource调用方法getString(..);我的实现时引发异常时做些什么:

I try to hijack/intercept a method of a class in EclipseLink. I had tried Spring AOP and AspectJ and I failed. I want to do something when class org.eclipse.persistence.internal.localization.i18n.TraceLocalizationResource throw exception while calling method getString(..); My implementation as below:

@AfterThrowing(pointcut = "execution(* org.eclipse.persistence.internal.localization.i18n.TraceLocalizationResource.getString(..))",throwing="error")
    public void logAfterThrowing(JoinPoint joinPoint,Throwable error) {
....
}

然后调用了calss TraceLocalizationResource(我对其进行调试).但是没有调用劫持方法.

And calssTraceLocalizationResource was called (I debug into it). But the hijack method wasn't called.

我的问题:

  1. 我做错了吗?
  2. 我们要劫持或拦截的目标类必须是自定义类型?
  3. 如果我想劫持java.lang.String之类的JDK中的类怎么办?

推荐答案

  1. 基本上,语法看起来还不错,但我没有尝试复制 您的代码段中提供了完整的方面.此外,我只使用 原生的AspectJ语法,并且不太熟悉 基于注释的语法,因此如果出现问题,我可能不会 已经看过了.

  1. Basically the syntax looks okay, but I have not tried to replicate a full aspect from your code snippet here. Furthermore, I only use native AspectJ syntax and am not quite familiar with annotation-based syntax, so if there is a problem there I might not have seen it.

否,但是,如果要编织主应用程序使用的库(JAR或解压缩到文件系统中),则必须确保该库位于编织者的路径中.请参考AspectJ文档中的语法. 编辑:我今天早些时候在另一个答案中发布了一些有用的链接.

No, but if you want to weave a library (JAR or unpacked into the file system) used by your main application, you must make sure that it is in the in-path for the weaver. Please consult AspectJ documentation for the syntax. I posted some helpful links in another answer earlier today.

默认情况下,包org.aspectjjavajavax被排除在编织之外.对于后两个,有用于启用编织的命令行开关.但是LTW最好是棘手的,最糟糕的是a **(几乎是不可能的),因为那里存在鸡蛋问题:AspectJ编织器运行时类需要JDK,并且JDK引导发生在任何Java之前像织布工的代理人.因此,最好的选择是对JDK类进行编译时编织,然后在应用程序中稍后使用编织的rt.jar.即使在编译时进行编织,如果您的建议本身使用JDK编织类,您也可能会遇到问题.您要确保避免在那里进行无限递归.但是有可能.

By default, packages org.aspectj, java and javax are excluded from weaving. For the latter two, there are command line switches to enable weaving. But LTW will be tricky at best and a pain in the a** (next to impossible) at worst because you have a hen-and-egg problem there: The AspectJ weaver runtime classes need the JDK, and JDK bootstrapping happens before any Java agent like the weaver is attached. So your best bet is compile-time weaving for JDK classes and then using the woven rt.jar later in your application. Even with compile-time weaving you might run into issues if your advice use JDK woven classes themselves. You want to make sure to avoid infinite recursions there. But it is possible.

更新:对于使用AspectJ已有一段时间的人来说,另一条提示有时似乎太简单了:您可以使用execution切入点代替编织目标类,而不必使用execution切入点只需使用call切入点,该切入点(顾名思义)会在客户端类中的任何地方拦截对目标的调用.效率可能会降低一些,但是除非您真的想解决编织JDK的麻烦,否则最好的选择还是最简单的选择.

Update: Another hint which sometimes seems too simple to mention for someone who has used AspectJ for a while: Instead of an execution pointcut which makes it necessary to weave the target class, you may just use a call pointcut which intercepts (as the name implies) calls to the target everywhere in your client classes. It might be a little less efficient, but maybe your best and easiest bet unless you really want to take the trouble of weaving the JDK.

这篇关于如何劫持/拦截来自第三方jar(甚至JDK)的类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆