AspectJ:向类添加静态初始化器 [英] AspectJ: add static initializer to class
问题描述
我的某些Java类的静态方法带有特殊注释@Assert
,其中包含健全性检查.
Some of my Java classes have static methods marked with a special annotation, @Assert
, containing sanity checks.
我想在实际执行任何代码之前执行这些检查.最好是从<clinit>
调用这些方法.如何在不修改初始代码的情况下使用AspectJ获得类似的行为?
I'd like to perform these checks before any code gets actually executed. The best would be to call these methods from <clinit>
. How to get similar behavior using AspectJ, and without modifying initial code?
推荐答案
AspectJ提供了一个staticinitialization(TypePattern)
切入点定义,该定义将选择现有的静态初始值设定项并对其进行拦截.但是,似乎无法向没有静态初始化程序的类中添加静态初始化程序.
AspectJ provides a staticinitialization(TypePattern)
pointcut definition that will select existing static initializers and intercept them. Doesn't appear to be able to add static initializers to classes that don't have them, however.
这篇关于AspectJ:向类添加静态初始化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!