Hector
Hector

Reputation: 5694

Android AspectJ @Around with method args not working

In my current Android application I am investigating the use of @AspectJ

I am attempting to "capture" all executions to methods whose signature resembles:-

public void onMethodClicked(com.example.CustomType customType) {}

I have the following POINTCUTS

1) Ignore my Aspect class:

@Pointcut("!within(com.example.aspect)")
public void notAspect() { }

2) Select all "Clicked" methods with customType argument

@Pointcut("execution(* com.example..*.*Clicked(com.example.CustomType)) && args(custom)";)
public void customClicked(CustomType custom) { }

3) My @Around:-

@Around("notAspect() && customClicked()")
public Object selectedClicked(final ProceedingJoinPoint joinPoint, CustomType custom) throws Throwable {
    Log.d(TAG, "Found a clicked method " + custom);
    Object result = joinPoint.proceed();

    return result;
}

When I build my Android Application I get these messages

no match for this type name: CustomType [Xlint:invalidAbsoluteTypeName]

bad parameter to pointcut reference
formal unbound in pointcut 

no match for this type name: com.example.aspect [Xlint:invalidAbsoluteTypeName]

the parameter custom is not bound in [all branches of] pointcut
use of ProceedingJoinPoint is allowed only on around advice (arg 1 in (before(extraFlags: 2): (((!within(com.example.aspect+) && execution(* com.example..*.*Clicked(com.example.CustomType)) && args(custom)) && persingleton(com.example.aspect.TraceAspect))->void com.example.aspect.TraceAspect.selectedClicked(org.aspectj.lang.JoinPoint, com.example.CustomType)))

What have I done wrong?

UPDATE

I have fixed one of the error/warning messages by correcting the !within() as follows:-

1) Ignore my Aspect class:

@Pointcut("!within(com.example.aspect.TraceAspect)")
public void notAspect() { }

Upvotes: 1

Views: 3144

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24211

I'm not sure about your problem but you may try changing the POINTCUT like this.

@Pointcut("!within(com.example.aspect.TraceAspect)")
public void notAspect() { }

@Pointcut("execution(* com.example..*.*Clicked(com.example.CustomType)))
public void customClicked() { }

Look I've removed the args(custom) part here which go inside the @Around annotation. And yes, of course I've removed the function parameter argument of customClicked function and the semi-colon by the end of the statement.

Now write your selectedClicked function like this by passing the arguments from here.

@Around("notAspect() && customClicked() && args(custom)")
public Object selectedClicked(final ProceedingJoinPoint joinPoint, CustomType custom) throws Throwable {

    Log.d(TAG, "Found a clicked method " + custom);
    Object result = joinPoint.proceed();

    return result;
}

It should work with no failure.

Upvotes: 3

Related Questions