Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ matching return type as interface with generics

Tags:

aspectj

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?

like image 941
Beto Neto Avatar asked Jun 05 '15 14:06

Beto Neto


1 Answers

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) {
    }
}
like image 199
Beto Neto Avatar answered Sep 30 '22 01:09

Beto Neto