AspectJ - 实现给定接口的类的静态类型间声明 [英] AspectJ - Static inter-type declaration on classes implementing a given interface
问题描述
我想知道是否有可能(以及如何...)进行静态类型间声明,该声明适用于实现给定接口的所有类.
I would like to know if it is possible (and how if it is...) to make a static inter-type declaration that works on all classes that implements a given interface.
在我的用例中,我有一个空接口:
In my use case, I have an empty interface:
public interface Delegate {}
和两个实现它的类:
public class DelegateA implements Delegate {...}
public class DelegateB implements Delegate {...}
我想要一个方面在 DelegateA 和 DelegateB 上声明一个静态成员......以及将实现我的接口的所有未来类!
And I want an aspect to declare a static member on DelegateA and DelegateB... and all the future classes that will implements my interface !
我该怎么做?如果只是可能...
How should I do it ? If it's only possible...
推荐答案
您确定要这样做吗?无论如何,这是我为您的问题提供的最佳解决方案:
Are you sure that you want to do that? In any case, here is my best solution for your problem:
public aspect MyAspect pertypewithin(Delegate+){
public int oneFieldPerClass;
}
这将为 Delegate 的每个子类型实例化一个方面.您可以通过以下方式访问该字段:
This will instantiate an aspect for each subtype of Delegate. You can access the field in the following ways:
MyAspect.aspectOf(DelegateA.class).oneFieldPerClass = 1;
MyAspect.aspectOf(DelegateB.class).oneFieldPerClass = 2;
Delegate d = new DelegateA();
MyAspect.aspectOf(d.getClass()).onFieldPerClass = 3;
以下主题也可能有帮助:
The following thread might be helpful as well:
AspectJ - 创建全局 Logger 字段使用类型间声明
HTH、DSP
这篇关于AspectJ - 实现给定接口的类的静态类型间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!