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

问题描述

目前,我正在与AspectJ合作.我在依赖项中分离了AspectJ代码.在该依赖关系中,一切都按预期工作.但是,一旦我将其导入另一个项目中,则仅某些功能不再起作用.当使用@DeclareParents的defaultImpl时,该接口显示在已编译的代码中,而不显示默认的Implementation.这是我的代码,以显示我的意思(每个代码段都是其自己的文件):

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;
}

另一个项目中的目标类:

Target Class in a different project:

@SomeAnnotation
public class MyClass
{
    private final int myValue;

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


    public int getMyValue()
    {
        return myValue;
    }
}

Maven扔了我

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

这暗示它部分起作用.当查看反编译的.class文件时,目标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的设置类似于示例

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提供注释样式的父接口+实现时,@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();
}

这与方面库一起使用.

我将与安迪讨论是否有必要为您的问题提交故障单,或者他是否会因为存在可行且建议的替代方案而无法解决该问题.

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代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:53