spectj可以向java.lang.String添加方法吗
我读过aspectj的一些文章,我知道它可以增强类,这很有吸引力。我有一个非常愚蠢的问题,找不到明确的答案:
Canspectj addmethods to java.lang.String?
或类似的问题:如果我无法获取某些类的源代码,我可以通过aspectj增强它们吗?
I've read some articles of aspectj, I know it can enhance classes, which is attractive. I've a very stupid question that I can't find a clear answer:
Can aspectj add methods to java.lang.String?
Or similar question: If I can't get the sources of some classes, can I enhance them by aspectj?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只是在寻找其他东西,偶然发现了这个问题。我想指出的是,Kowser 的答案尽管被接受了,但还是有些不正确。该问题提到对 LTW 没有限制,因此 CTW 仍在竞争中。可以编织 JDK/JRE 类文件,然后在后续步骤中将建议的类添加到引导类路径中。我以前已经做过(甚至完全按照要求,即在字符串中声明一个新方法),并且它工作没有问题。
I was just looking for something else and stumbled across this question. I want to point out that Kowser's answer, even though it was accepted, is somewhat incorrect. The question mentions no restriction to LTW, so CTW is still in the race. It is possible to weave the JDK/JRE class files and then prepend the advised classes to the boot class path in a later step. I have done it before (even exactly what was asked, i.e. declaring a new method within String), and it works without problems.
不,你不能这样做,我的朋友。
我认为这就是您正在寻找的 类型未向用户公开
有关更多详细信息,请查看 AspectJ 开发环境指南#特殊情况
正如上面所说
No, you can't do this my friend.
I think this is what you are looking for Type not exposed to user
For further details please take a look at The AspectJ Development Environment Guide #Special Cases
As it says
我绝不是aspectj方面的专家,但我相信你可以做到这两件事。
要向类添加方法,甚至使其实现新接口并通过方面提供实现,我认为您需要 类型间声明。
AspectJ 通过字节码操作来完成工作,因此您不需要访问正在更改的类的源代码。
I'm by no means an expert on aspectj, but I believe you can do both of these things.
To add methods to a class, or even make it implement a new interface and supply the implementation through an aspect, I think you want inter-type declarations.
AspectJ does its work by byte-code manipulation, so you do not need access to the source of the classes you're altering.