Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] pointcut to match "return" statements

You can neither match "return null" nor assignments to local variables, only assignments to member variables.

What you can do is match returning calls or executions and check their results dynamically like this:

    after() returning(Object result) : execution(!void *(..)) {
        if (result == null)
            System.err.println(thisJoinPoint.toString() + " must not return null");
    }

As you can see, I am only matching non-void methods because void methods always implicitly return null. You can further refine the pointcut if you want to restrict it to being applied to certain packages or classes or if you want to match boolean methods returning false, int methods returning 0 etc.
-- 
Alexander Kriegisch


Sean Patrick Floyd schrieb am 06.01.2014 14:53:

> Hi,
> 
> is there any way for me to match "return null" statements statically?
> I would like to create a policy enforcement aspect that forbids null
> returns unless the method is marked as @Nullable.
> 
> I'm aware this check wouldn't be perfect because it could always be tricked
> with a variable:
> 
> String result = null;
> return result;
> 
> But still it would make finding errors much easier. Is there any such
> construct available?



Back to the top