Advice around method implementing an extended interface
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.)
精彩评论