guice AOP是如何实现的?

How is guice AOP implemented?

我在此处的文档中阅读了有关 guice AOP 的内容:- https://github.com/google/guice/wiki/AOP 来自文档:-

Behind the scenes, method interception is implemented by generating bytecode at runtime. Guice dynamically creates a subclass that applies interceptors by overriding methods. If you are on a platform that doesn't support bytecode generation (such as Android), you should use Guice without AOP support.

文档中扩展子类是什么意思。这是方法拦截器子类吗?用反射拦截方法行不行?我问这个问题的原因是因为在文档中,这一行后面跟着这个 :-

Due to this, we have the following limitations on the guice AOP

a) AOP cannot be applied to a private method.

我不清楚为什么 AOP 不能应用于私有方法。

Unclear to me why AOP cannot be applied to a private method.

因为子类不继承私有方法类,即无法拦截私有方法然后委托给它,因为子类甚至无法调用该方法。这是正常的 Java 限制,与 AOP 无关。

顺便说一句,如果你想要一个成熟的、强大的 AOP 工具,可以与任何 JVM 语言一起工作,并且能够拦截私有方法,而无需求助于动态代理(sub类 在运行时创建,如 Guice 或Spring AOP), 只需使用 AspectJ.

What does the documentation means by extending the subclass.

JRE 知道 dynamic proxies 的概念,以便能够拦截方法调用并(可选)委托给原始调用 before/after 在拦截器方法中做其他事情。这仅适用于接口,但 CGLIB 将此概念扩展到非接口 类 的子 类,这就是为什么在基于代理的 AOP 框架中,例如 Spring AOP(也许还有 Guice,不是 100% 确定)使用代理来实现 AOP。 AspectJ 的工作方式不同,它不使用或不需要任何动态代理或其他类型的 sub类.