AspectJ @DeclareParents defaultImpl 代码作为依赖使用时不使用 [英] AspectJ @DeclareParents defaultImpl code is not used when using it as dependency

查看:22
本文介绍了AspectJ @DeclareParents defaultImpl 代码作为依赖使用时不使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与 AspectJ 合作.我在依赖项中分离了 AspectJ 代码.在该依赖项中,一切都按预期工作.但是一旦我将它导入到另一个项目中,只有某些功能不再起作用.使用@DeclareParents 的defaultImpl 时,接口显示在编译代码中,但不显示默认实现.这是我的代码来显示我的意思(每个代码片段都是它自己的文件):

I am working with AspectJ at the moment. I seperated AspectJ code in a dependency. Within that dependency everything works as intended. But as soon as I import it in another project only some functionality does not work anymore. When using the defaultImpl of @DeclareParents, the interface is shown within the compiled code but not the default Implementation. Here is my code to show what I mean (every code snippet is its own File):

AspectJ 代码:

AspectJ code:

public interface IAspect
{
    String hello();
}

public class IAspectDefaultImpl implements IAspect
{
    @Override
    public String hello()
    {
        return "hello";
    }
}

@Aspect
public class AspectJ
{

    @DeclareParents(value = "@SomeAnnotation*", defaultImpl = IAspectDefaultImpl.class)
    private IAspect implementedInterface;
}

不同项目中的目标类:

@SomeAnnotation
public class MyClass
{
    private final int myValue;

    public MyClass(final int wert)
    {
        this.myValue = wert;
    }


    public int getMyValue()
    {
        return myValue;
    }
}

Maven 把我扔了:

Maven throws me:

The type MyClass must implement the inherited abstract method IAspect.hello()

这意味着它部分工作.在查看反编译的 .class 文件时,目标类实际上实现了 IAspect.IAspectDefaultImpl 中定义的方法仍然缺失.

Which implies that it works partially. When looking at the decompiled .class files the targeted Class does in fact implement IAspect. The method defined in IAspectDefaultImpl is still missing tho.

我的 pom 设置类似于 this 示例.

My pom is set up like in this example.

我不确定应该从哪里开始查找错误.感谢任何帮助.

I am not sure where I should start to look for errors. Any help is apreciated.

推荐答案

感谢 MCVE.但是,嘿,您不使用 Git 来提交 7z 或 ZIP 档案,您应该提交源代码.我分叉了您的项目并修复了该问题,重组并简化了您的 POM,并修复了主要问题.

Thanks for the MCVE. But hey, you don't use Git in order to commit 7z or ZIP archives, you ought to commit source code. I forked your project and fixed that, restructured and simplified your POMs and also fixed the main problem.

查看我的拉取请求以及其中的提交以了解更多详细信息.

See my pull request and the commits in it for further details.

关于您的问题,我可以确认,如果您像在方面库中那样使用 @DeclareParents 会发生这种情况.

Concerning your problem, I can confirm that it occurs if you use @DeclareParents the way you do in an aspect library.

实际上,根据 AspectJ 维护者 Andy Clement 的说法,@DeclareParents 在使用它来提供父接口 + 注释样式的实现时存在某些问题.通过 declare parents 的原生 AspectJ 语法不受此影响,但对于注释样式的语法,Andy 提供了一个名为 @DeclareMixin 的替代方法,请参阅 AspectJ 手册.在那里他提到他甚至考虑弃用 @DeclareParentsdefaultImpl 参数,而支持 @DeclareMixin.

Actually, according to AspectJ maintainer Andy Clement there are certain problems with @DeclareParents when using it to provide parent interfaces + implementations in annotation style. The native AspectJ syntax via declare parents is not affected by that, but for annotation-style syntax Andy provided an alternative called @DeclareMixin, see the AspectJ manual. There he mentions that he is even considering to deprecate the defaultImpl argument of @DeclareParents in favour of @DeclareMixin.

所以我的错误修复(或解决方法)

So my bugfix (or workaround) for your problems is to actually replace

@DeclareParents(value = "@de.example.aspect.SomeAnnotation *", defaultImpl = IAspectDefaultImpl.class)
private IAspect implementedInterface;

@DeclareMixin("@de.example.aspect.SomeAnnotation *")
public static IAspect createIAspectImplementation() {
    return new IAspectDefaultImpl();
}

这适用于方面库.

我将与 Andy 讨论为您的问题提交错误票证是否有意义,或者他是否因为有可行且推荐的替代方案而无论如何都不会修复它.

I will discuss with Andy about whether it makes sense to file a bug ticket for your problem or if he won't fix it anyway because there is a viable and recommended alternative.

这篇关于AspectJ @DeclareParents defaultImpl 代码作为依赖使用时不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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