【问题标题】:Advice around method implementing an extended interface关于实现扩展接口的方法的建议
【发布时间】:2011-09-20 12:19:33
【问题描述】:

我正在尝试围绕一个扩展如下界面的方法提出建议:

public interface StructureService {
    void delete(FileEntry entry);
}

public interface FileService extends StructureService {
     void dummy();
}

实现这些的类如下所示:

public class DbStructureService implements StructureService {
    public void delete(FileEntry entry) {
    }
}
public class DbFileService extends DbStructureService implements FileService {
    public void dummy() {
    }
}

我正在尝试匹配删除方法,但仅适用于实现 FileService 的类。

我已经定义了以下方面:

public aspect FileServiceEventDispatcherAspect {
    pointcut isFileService() : within(org.service.FileService+);

    pointcut delete(FileEntry entry) :
        execution(void org.service.StructureService.delete(..))
        && args(entry) && isFileService();

    void around(FileEntry entry) : delete(entry)  {
            proceed(entry);
    }
}

问题是,只要我启用了 isFileService 切入点,它就不会匹配任何类;即使有很多方法应该匹配这个

如果我将within(org.service.FileService+) 中的within(org.service.StructureService+) 替换为within(org.service.StructureService+),它也可以正常工作。

我尝试过使用 this() 等等,但没有成功。我如何在 aspectj 中做到这一点?

编辑: 更新了实现接口的类的外观。我认为这种情况可能很难建议,因为 DbFileService 中没有重写方法

【问题讨论】:

  • 如果这很重要,我正在使用二进制编译时编织
  • 对于编译时源代码编织此代码是正确的。可能问题出在“二进制”中,尽管不应该存在。
  • 是的,我编辑了我的问题。您认为这是 aspectj 不支持的场景吗?子类中没有真正的建议功能

标签: java aspectj instanceof


【解决方案1】:

我想你的意思是 DbFileService 实现了 FileService,但没有实现 StructureService。鉴于此,这段代码应该可以工作:

public aspect FileServiceEventDispatcherAspect {    

pointcut delete(FileService this_, FileEntry entry) :
    execution(void org.service.StructureService.delete(..))
    && args(entry) && this(this_);

void around(FileService this_, FileEntry entry) : delete(this_, entry)  {
        proceed(this_, entry);
}
}

“内部”切入点在这里不合适,因为它是“基于词法结构的切入点”("AspectJ in Action", second edition.)

【讨论】:

  • 谢谢,我感到困惑的原因是eclipse AJDT中似乎有错误。标记显示与此类型有关的所有签名的匹配。但是在编译时它可以工作。
  • AJDT 非常有用,但同时也不是很可靠。它可能会错过实际存在建议的标记,发出有关“建议尚未应用”的警告等。
猜你喜欢
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2012-04-30
相关资源
最近更新 更多