如何使用 AspectJ 向自定义注释类添加字段 [英] How to add a field to a custom-annotated class using AspectJ

查看:48
本文介绍了如何使用 AspectJ 向自定义注释类添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用aspectj向某个特定类添加字段

To add a field to some specific class with aspectj we do

package com.test;

public class MyClass {
    private String myField;
}

public aspect MyAspect
{
    private String MyClass.myHiddenField;
}

我们如何向用一些自定义注解注解的类添加字段?

How do we add a field to a class that is annotated with some custom annotation?

示例用法:如果使用 @CustomLoggable 注释类,则添加 Logger 字段和一些方法.

example usage : if class is annotated with @CustomLoggable add a Logger field and some methods.

如果方法有 @ReadLocked 注释,那么类将有一个 ReentrantReadWriteLock 字段和适当的逻辑注入等

if method has the @ReadLocked annotation then class will have a ReentrantReadWriteLock field and the appropriate logic injected, etc.

推荐答案

实际上你不能对注解类型进行类型间声明 (ITD),即你需要知道具体的类名才能声明静态或非静态成员或直接方法.

Actually you cannot make inter-type declarations (ITD) on annotation types, i.e. you need to know concrete class names in order to declare static or non-static members or methods directly.

通常的解决方法是:

  • 使用您需要的所有方法创建一个接口.
  • 为每个接口方法提供实现.
  • 使每个带注释的类型通过 ITD 实现接口.

现在,如果您还想向所有带注释的类型添加一个静态成员(例如记录器),同样如果您不知道确切的类名,您需要使用一种解决方法:

Now if you also want to add a static member such as a logger to all annotated types, again if you do not know the exact class names you need to use a workaround:

  • 创建一个包含所需成员的方面.在本例中,我们称之为 LoggerHolder.
  • 确保为每个目标类创建一个方面实例,而不是默认的单一方面实例.这是通过 pertypewithin 完成的.
  • 为了避免运行时异常,您不能直接通过 Logger logger = ... 初始化成员,而需要延迟执行,直到目标类型的静态初始化阶段完成.
  • 您还需要在方面中提供像 LoggerHolder.getLogger() 这样的访问器方法,并在必要时调用它.
  • 为了向最终用户隐藏所有丑陋的方面,我建议向上面提到的 ITD 接口添加另一个访问器方法 LoggableAspect.getLogger()(为方便起见,使用相同的方法名称)提供一个方法实现,通过 LoggerHolder.aspectOf(this.getClass()).getLogger() 从方面实例中提取成员引用.
  • Create an aspect holding the desired member(s). Let's call it LoggerHolder in this example.
  • Make sure that one aspect instance per target class is created instead of the default singleton aspect instance. This is done via pertypewithin.
  • In order to avoid runtime exceptions you must not initialise the members directly via Logger logger = ... but need to do it lazily, waiting until after the target type's static initialisation phase is finished.
  • You also need to provide an accessor method like LoggerHolder.getLogger() in the aspect and call it whenever necessary.
  • In order to hide all the ugly aspect stuff from the end user I recommend to add yet another accessor method LoggableAspect.getLogger() (same method name for convenience) to the ITD interface mentioned above and provide a method implementation extracting the member reference from the aspect instance via LoggerHolder.aspectOf(this.getClass()).getLogger().

注意:我在这里同时使用两个概念,将它们混合在一个应用程序中,因为您要求将静态成员和非静态方法添加到带注释的类中:

Attention: I am using two concepts at once here, mixing them in one application because you asked for both static members and non-static methods added to annotated classes:

  • 通过 ITD 添加到您的核心代码中的 Helper 接口 + 实现
  • Holder 方面声明成员并通过 pertypewithin 与目标类相关联,以模拟静态成员
  • Helper interface + implementation added to your core code via ITD
  • Holder aspect declaring member(s) and associated with target classes via pertypewithin in order to emulate static members

现在是一些示例代码:

注释:

package de.scrum_master.app;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface CustomLoggable {}

两个类,一个带有注释,一个不带有注释:

package de.scrum_master.app;

public class OrdinaryClass {
    public void doSomething() {
        System.out.println("Logging some action directly to console");
    }
}

package de.scrum_master.app;

import java.util.logging.Level;

@CustomLoggable
public class AnnotatedClass {
    public void doSomething() {
        getLogger().log(Level.INFO, "Logging some action via ITD logger");
        getLogger().log(Level.INFO, someOtherMethod(11));
    }
}

如您所见,第二个类使用了两个未在类中直接声明的方法:getLogger()someOtherMethod(int).它们都将通过下面的 ITD 进一步声明,前者提供对伪静态成员的访问,后者只是您希望在每个带注释的类上声明的另一种方法.

As you can see, the second class uses two methods which have not been declared directly within the class: getLogger() and someOtherMethod(int). Both of them will be declared via ITD further below, the former providing access to the pseudo-static member and the latter being just another method you want declared on each annotated class.

持有伪静态成员实例的方面:

package de.scrum_master.aspect;

import java.util.logging.Logger;
import de.scrum_master.app.CustomLoggable;

public aspect LoggerHolder
    pertypewithin(@CustomLoggable *)
{
    private Logger logger;

    after() : staticinitialization(*) {
        logger = Logger.getLogger(getWithinTypeName());
    }

    public Logger getLogger() {
        return logger;
    }
}

正如我之前所说,请注意pertypewithinstaticinitialization 的用法.另一个方便的方法是使用方面的 getWithinTypeName() 方法来获取命名记录器的目标类名.

As I said earlier, please note the usage of pertypewithin and staticinitialization. Another convenient thing is to use the aspect's getWithinTypeName() method in order to get the target class name for naming the logger.

方面声明接口 + 实现并将其应用于所有目标类型:

package de.scrum_master.aspect;

import java.util.logging.Logger;
import de.scrum_master.app.CustomLoggable;

public aspect LoggableAspect {
    public static interface Loggable {
        Logger getLogger();
        String someOtherMethod(int number);
    }

    declare parents : (@CustomLoggable *) implements Loggable;

    public Logger Loggable.getLogger() {
        return LoggerHolder.aspectOf(this.getClass()).getLogger();
    }

    public String Loggable.someOtherMethod(int number) {
        return ((Integer) number).toString();
    }
}

为简单起见,我只是将接口声明为方面内的静态嵌套类型.您也可以单独声明接口,但在这里您可以在其上下文中看到它,这对我来说更可取.

For simplicity, I just declared the interface as a static nested type within the aspect. You can also declare the interface separately, but here you see it in its context which for me is preferable.

这里的关键是使每个目标类实现接口的declare parents 语句.最后的两个方法实现展示了如何提供普通"方法实现以及如何通过 aspectOf 从持有者方面访问记录器.

The key thing here is the declare parents statement making each target class implement the interface. The two method implementations at the end show how to provide "normal" method implementations as well as how to access the logger from the holder aspect via aspectOf.

带入口点的驱动程序类:

最后,但并非最不重要的是,我们想运行代码,看看它是否符合我们的要求.

Last, but not least, we want to run the code and see if it does what we want.

package de.scrum_master.app;

public class Application {
    public static void main(String[] args) {
        new OrdinaryClass().doSomething();
        new AnnotatedClass().doSomething();
    }
}

控制台输出:

Logging some action directly to console
Mrz 15, 2015 11:46:12 AM de.scrum_master.app.AnnotatedClass doSomething
Information: Logging some action via ITD logger
Mrz 15, 2015 11:46:12 AM de.scrum_master.app.AnnotatedClass doSomething
Information: 11

瞧!Logging 工作正常,Logger 有一个不错的名字 de.scrum_master.app.AnnotatedClass 并且调用这两个接口方法按预期工作.

Voilà! Logging works, the Logger has a nice name de.scrum_master.app.AnnotatedClass and calling the two interface methods works as expected.

替代方法:

由于支持 AspectJ 1.8.2 注释处理,另见这篇博文.IE.您可以使用 APT 来为每个带注释的类型生成一个方面,并直接引入静态成员和其他方法,而无需任何技巧,例如每个类型的实例化、持有者方面实例和接口中的访问器方法成员.这是以额外的构建步骤为代价的,但我认为这将是解决问题的一种非常简洁直接的方法.如果您在理解示例时遇到任何困难并需要更多帮助,请告诉我.

Since AspectJ 1.8.2 annotation processing is supported, see also this blog post. I.e. you could use APT in order to generate one aspect per annotated type and introduce static members and additional methods directly without any tricks such as per-type instantiation, accessor methods members within holder aspect instances and interfaces. This comes at the cost of an additional build step, but I think it would be a very neat and straightforward way to solve your problem. Let me know if you have any difficulty understanding the examples and need more help.

这篇关于如何使用 AspectJ 向自定义注释类添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆