开发者

Advice around method implementing an extended interface

开发者 https://www.devze.com 2023-04-05 22:49 出处:网络
I am trying to do an advice around a method that extends an interface that looks like this: public interface StructureService {

I am trying to do an advice around a method that extends an interface that looks like this:

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

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

The classes that implement these looks like the following:

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

I am trying to match the delete method, but only for classes implementing FileService.

I have defined the following aspect:

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

The problem is that as long as I have the isFileService pointcut enabled this will match no classes; even though there are plenty of methos that should match this

If I replace the within within(org.service.FileService+) to within(org.service.StructureService+) it also works fine.

I have tried experimenting with this() and so on but no success. How do I do this in aspectj?

EDIT: Updated how the classes look that implements the interfaces. I think this scenario might be hard to advice since there is no overridden method i开发者_JAVA百科n DbFileService


I suppose you mean DbFileService implements FileService but not StructureService. Given that, this code should work:

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

The "within" pointcut isn't suitable here because it is "lexical-structure based pointcut" ("AspectJ in Action", second edition.)

0

精彩评论

暂无评论...
验证码 换一张
取 消