title: Using generic types as return type for a pointcut
body: <p>I'm using Spring Boot to set up a REST API. I'll be making a bunch of <code>@RestController</code>s and want to set a pointcut on those methods that return a subtype of a specific abstract class I call <code>Model</code>. These controllers look something like this:</p>
  
  <pre><code>@RestController
  public class UserController {
      @RequestMapping(...)
      public Person getAllPeople() {
          ...
      }
  }
  </code></pre>
  
  <p>Where my <code>Person</code> class would look something like this:</p>
  
  <pre><code>public class Person extends Model {
      ...
  }
  </code></pre>
  
  <p>So would it be possible to write advice that looks something like this:</p>
  
  <pre><code>@Aspect
  @Component
  public class ModelAspect {
      @AfterReturning(
          value = "execution(&lt;T extends mypackages.Model&gt; T mypackages.api.*.*(..))",
          returning = "model")
      public void doSomethingWithModel(Model model) {
          ...
      }
  }
  </code></pre>
  
  <p>Of course that won't work because the advice is not valid syntactically. In the reference documentation, I have only found information about generic parameters, not return types (<a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-ataspectj-advice-params" rel="nofollow noreferrer">Spring AOP reference</a>). What I have now is this, but I think something like the example above would be a lot more efficient:</p>
  
  <pre><code>@Aspect
  @Component
  public class ModelAspect {
      @AfterReturning(
          value = "execution(* mypackages.api.*.*(..))",
          returning = "model")
      public void doSomething(Object model) {
          if (model instanceof Model)
              doSomethingWithModel((Model) model);
      }
  }
  </code></pre>
  
  <p>My next question would be, is the same possible for those methods that return a <code>Collection</code> of suptypes of <code>Model</code>? Because the reference states that parameter types cannot be generic <code>Collection</code>s.</p>