确定哪些方面挂接到给定的类中
是否可以确定哪些方面挂接到给定的类并获得对其实例的访问权限?
像这样的东西:
Foo foo = new Foo();
List<Object> aspects = getAllAspectsOf(foo);
Is it possible to determine which aspects hook into a given class and to gain access to their instances?
Something like:
Foo foo = new Foo();
List<Object> aspects = getAllAspectsOf(foo);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您很可能无法使用现有的 Aspect API 进行任何此类监视,因为 Aspect 包装器拦截调用的特定目的是不绑定到任何一个特定组件,因此不会有任何自然的方法检测拦截方面。
也就是说 - 可能有一种方法可以将方面包装在某种策略类中,但是,维护这一点的工作量将非常大。
http://www.eclipse.org/aspectj/doc/released /faq.php#q:benefits
方面通常被描述为“位于其他代码之上”或“与其他代码交织在一起”,即在运行时或构建时。
http://asm.ow2.org/users.html
因此,您的代码通常不会考虑到方面实现的范例,“意识到”任何此类面向方面的功能。
但是,如果您想要可跟踪的切面,您可以使用标准 java 和依赖项注入来实现一些“类似”切面的功能 - 也就是说,通过在运行时加载注入的模块来实现一些横切功能,一个方面会实现......但是,我怀疑,如果您真的在做严肃的面向方面的代码,那么这种方法将达不到您面向方面的要求。
First, you most likely would not be able to do any such monitoring using existing Aspect APIs, because Aspect wrappers that intercept calls with the specific purpose of NOT being tied to any one particular component, so there would not be any natural way to detect intercepting aspects.
That said - there might be a way you could wrap aspects inside of some sort of strategy class, however, the amount of work to maintain this would be quite significant.
http://www.eclipse.org/aspectj/doc/released/faq.php#q:benefits
Aspects are often described as "being on top of" or "woven in with" your other code, i.e. at runtime or build time.
http://asm.ow2.org/users.html
Thus, your code would not typically be "aware" of any such aspect oriented features, given this paradigm for the implementation of aspects.
However, if you wanted traceable aspects , you could implement some Aspect "like" features using standard java and dependency injection - that is, by loading injected modules at runtime which implement some of the cross-cutting functionality that an aspect would implement ... but, i suspect, if you are really doing serious aspect oriented code, such an approach would fall short of your aspect oriented requirements.
我不认为你可以,但这取决于技术。据我所知,aspectj 或 cglib 无法跟踪和访问围绕类编写的建议。您可以创建您的建议,以便它将对自身的引用添加到可以由建议的类访问的某个线程本地结构。然而,如果将目标类与方面结合起来,那么它似乎违背了方面作为横切关注点的解决方案的目的。
I don't think that you can, although, it would be technology dependent. As far as I know, there is no way with aspectj or cglib to track and access the advice woven around a class. You could create your advice such that it will add a reference to itself to some thread local structure that can be accessed by the advised class. However, it would seem to defeat the purpose of aspects as a solution to cross cutting concerns if you couple your target classes to them.
我想要获得一些有限的信息,需要更改编织器,以便将其工作的一些日志保存到增强类中,以便您稍后可以阅读它。但不要期望太多 - AFAIK 方面可能不存在,因为根本没有运行时类型。我想知道 - 有没有框架可以做到这一点?
I guess to get some limited information it would need to change the weaver so it saves some log of its work into the augmented class, so you can later read it. But do not expect much - AFAIK aspects may not exist as no run-time types at all. I wonder - is there any framework doing that?