Bug 501656

Summary: Problem in abstract @AfterThrowing advice
Product: [Tools] AspectJ Reporter: Alexander Kriegisch <Alexander>
Component: CompilerAssignee: aspectj inbox <aspectj-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: aclement
Version: 1.8.9   
Target Milestone: ---   
Hardware: PC   
OS: Windows 10   
Whiteboard:

Description Alexander Kriegisch CLA 2016-09-18 08:05:39 EDT
What is wrong with this? Am I handling generics wrong or have I hit a bug or limitation in Ajc?


package com.myapp;

public class ApplicationException extends Exception {
    private static final long serialVersionUID = 1L;

    public ApplicationException(String message) {
        super(message);
    }
}


// ---------------------------------------------------


package com.myapp.aspect;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

import com.myapp.ApplicationException;

@Aspect
public abstract class ApplicationExceptionHandler<EX extends ApplicationException> {
    @AfterThrowing(
        pointcut = "execution(* com.myapp.*.facade.*.*(..))",
        throwing = "exception"
    )
    public abstract void handleFacadeException(EX exception);
}


// ---------------------------------------------------

Compile error for ApplicationExceptionHandler.aj:

the last parameter of this advice must be named 'exception' and be of a subtype of Throwable
Comment 1 Alexander Kriegisch CLA 2016-09-18 08:07:06 EDT
I am confused because the last parameter of this advice
  - *is* named 'exception' and
  - *is* of a subtype of Throwable
Comment 2 Andrew Clement CLA 2016-11-10 17:14:13 EST
I don't imagine the code that checks that hasn't a clue about generics, but taking a deeper look now.
Comment 3 Andrew Clement CLA 2016-11-10 17:27:55 EST
Actually it looks like it is because there is no local variable table attached to the abstract method so it comes up with a default name of 'arg' which doesn't meet expectations.
Comment 4 Andrew Clement CLA 2016-11-10 18:52:38 EST
Well I didn't know that, or I've forgotten it. Add the argNames="a,b,c" value to the annotation when there cannot be a local variable table (which occurs for abstract methods and non default interface methods).

@AfterThrowing(
        pointcut = "execution(* com.myapp.*.facade.*.*(..))",
        throwing = "exception",
        argNames="exception"
    )

I think this was written before -parameters, which would be another option but needs some code writing in AspectJ to support it.

I don't like the duplication, but at least it works.