AspectJ matching return type as interface with generics

匆匆过客 提交于 2019-12-07 02:48:49

问题


I'm trying to create an AspectJ Aspect to intercept the returning methods that has a generic interface.

This is my AspectJ

@AspectJ
public class MyAspect {

    @AfterReturning("execution(java.util.Collection+<mypackage.MyAbstractEntity+> mypackage.mvc.controllers..*.*(..))", returning = "list")
    public doStuff(JoinPoint j, Collection<MyAbstractEntity> list) {
    }
}

and this is my class that I want to weave into:

package mypackage.mvc.controller;

public class MyController {
    // MyEntity extends MyAbstractEntity
    public List<MyEntity> findAll() {
    }
}

What am I doing wrong?


回答1:


Solved!

Put the "plus" after the generics definition ("plus" means "classes that extends it"):

java.util.Collection<mypackage.MyAbstractEntity+>+

And contract the "list" as "? extends":

public doStuff(JoinPoint j, Collection<? extends MyAbstractEntity> list) {

The code will look like this:

@AspectJ
public class MyAspect {

    @AfterReturning("execution(java.util.Collection<mypackage.MyAbstractEntity+>+ mypackage.mvc.controllers..*.*(..))", returning = "list")
    public doStuff(JoinPoint j, Collection<MyAbstractEntity> list) {
    }
}


来源:https://stackoverflow.com/questions/30669630/aspectj-matching-return-type-as-interface-with-generics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!