gpt4 book ai didi

java - 使用 AspectJ 模拟接口(interface)和方法的注解继承

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:23 25 4
gpt4 key购买 nike

经常有人问 AspectJ 这样的问题,所以我想在以后可以轻松链接到的地方回答它。

我有这个标记注释:

package de.scrum_master.app;

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

@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Marker {}

现在我像这样注释接口(interface)和/或方法:

package de.scrum_master.app;

@Marker
public interface MyInterface {
void one();
@Marker void two();
}

这是一个同样实现接口(interface)的小驱动程序应用程序:

package de.scrum_master.app;

public class Application implements MyInterface {
@Override
public void one() {}

@Override
public void two() {}

public static void main(String[] args) {
Application application = new Application();
application.one();
application.two();
}
}

现在当我定义这个方面时,我希望它被触发

  • 对于注释类的每个构造函数执行和
  • 每次执行带注释的方法。
package de.scrum_master.aspect;

import de.scrum_master.app.Marker;

public aspect MarkerAnnotationInterceptor {
after() : execution((@Marker *).new(..)) && !within(MarkerAnnotationInterceptor) {
System.out.println(thisJoinPoint);
}

after() : execution(@Marker * *(..)) && !within(MarkerAnnotationInterceptor) {
System.out.println(thisJoinPoint);
}
}

不幸的是,方面没有打印任何内容,就好像类 Application 和方法 two() 没有任何 @Marker 注释一样。为什么 AspectJ 不拦截它们?

最佳答案

这里的问题不是 AspectJ,而是 JVM。在 Java 中,注解在

  • 接口(interface),
  • 方法或
  • 其他注释

从不

继承
  • 实现类(class),
  • 覆盖方法或
  • 使用注释注释的类。

注释继承仅适用于从类到子类,但前提是父类(super class)中使用的注释类型带有元注释 @Inherited,参见 JDK JavaDoc .

AspectJ 是一种 JVM 语言,因此可以在 JVM 的限制范围内工作。这个问题没有通用的解决方案,但是对于您希望为其模拟注释继承的特定接口(interface)或方法,您可以使用这样的解决方法:

package de.scrum_master.aspect;

import de.scrum_master.app.Marker;
import de.scrum_master.app.MyInterface;

/**
* It is a known JVM limitation that annotations are never inherited from interface
* to implementing class or from method to overriding method, see explanation in
* <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Inherited.html">JDK API</a>.
* <p>
* Here is a little AspectJ trick which does it manually.
*
*/
public aspect MarkerAnnotationInheritor {
// Implementing classes should inherit marker annotation
declare @type: MyInterface+ : @Marker;
// Overriding methods 'two' should inherit marker annotation
declare @method : void MyInterface+.two() : @Marker;
}

请注意:有了这个方面,您可以从接口(interface)和带注释的方法中删除(文字)注释,因为 AspectJ 的 ITD(类型间定义)机制将它们添加回接口(interface)加上所有实现/覆盖类/方法。

现在运行 Application 时的控制台日志显示:

execution(de.scrum_master.app.Application())
execution(void de.scrum_master.app.Application.two())

顺便说一下,您还可以将方面嵌入到界面中,以便将所有内容都集中在一个地方。小心地将 MyInterface.java 重命名为 MyInterface.aj 以帮助 AspectJ 编译器识别它必须在这里做一些工作。

package de.scrum_master.app;

public interface MyInterface {
void one();
void two();

// Cannot omit 'static' here due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=571104
public static aspect MarkerAnnotationInheritor {
// Implementing classes should inherit marker annotation
declare @type: MyInterface+ : @Marker;
// Overriding methods 'two' should inherit marker annotation
declare @method : void MyInterface+.two() : @Marker;
}
}

2021-02-11 更新:有人建议对后一种解决方案进行编辑,说 MarkerAnnotationInheritor 嵌套在接口(interface) MyInterface 中的方面是隐式地 public static,因此方面声明中的修饰符可以省略。原则上这是正确的,因为接口(interface)的成员(方法、嵌套类)在默认情况下始终是公共(public)的,并且非静态内部类定义在接口(interface)内部也没有意义(没有实例可以将其绑定(bind)到)。不过,我喜欢在我的示例代码中明确说明,因为并非所有 Java 开发人员都知道这些细节。

此外,如果我们省略 static,目前版本 1.9.6 中的 AspectJ 编译器会抛出错误。我刚刚创建了 AspectJ issue #571104对于这个问题。

关于java - 使用 AspectJ 模拟接口(interface)和方法的注解继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com