Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Keeping advice code DRY when using ambiguous bindings

I forgot the "return" only in email. It is there in the code I tested... Good catch though :)

_________________________
Sent over RFC-1149 compliant transport - please excuse occasionnal packet loss

Le 28 juil. 2012 à 09:07, Gopinathan Balaji <gopinathanbalaji@xxxxxxxxx> a écrit :

Hi,

Does, maybe, a
"return pjp.proceed()" 
in the place of
"pjp.proceed()"
help?

-B.


From: Romain Muller <romain.muller@xxxxxxxxx>
To: aspectj-users@xxxxxxxxxxx
Sent: Friday, July 27, 2012 5:46 PM
Subject: [aspectj-users] Keeping advice code DRY when using ambiguous bindings

Hi,

Given the following high-level pointcuts:
* AnyMethod() : execution(* *(..));
* AnyThrowing() : execution(* *(..) throws (!RuntimeException+));
* AnnotatedClass(SomeAnnotation annotation) : within(@SomeAnnotation *) && @within(annotation);
* AnnotatedMethod(SomeAnnotation annotation) : execution(@SomeAnnotation * *(..)) && @annotation(annotation);

I want to around-advise some logic on the following join points:
1. AnnotatedClass(annotation) && AnyMethod() && !AnyThrowing() && !AnnotatedMethod(*);
2. AnnotatedClass(annotation) && AnyThrowing() && !AnnotatedMethod(*);
3. AnnotatedMethod(annotation) && !AnyThrowing();
4. AnnotatedMethod(annotation) && AnyThrowing();

The advise needs the information on the "most specific" @SomeAnnotation, and in case the advised method throws checked exceptions, I need to re-throw them unchanged.

Now, for all of these advises, I need to apply the exact same logic around "proceed();", but I currently have to write it four times. I could reduce it to two if I didn't care about re-throwing checked exceptions unchanged (or would be OK to use Unsafe#throwException(Throwable)).

The advises pretty much look like the following (comments to call out the pointcut specifics):
Object around(final SomeAnnotation annotation)
throws Exception // if it's a checked-throwing pointcut
: SomePointCut(annotation) {
try {
return SomeClass.doSomeStuff(new Callable<Object>(){
@Override public final Object call() throws Exception {
proceed();
}
});
} catch(final RuntimeException re) {
throw re;
} catch(final Exception e) {// if it's **not** a checked-throwing pointcut
throw new AssertionError("Shouldn't have caught checked exception"); // if it's **not** a checked-throwing pointcut
} // if it's **not** a checked-throwing pointcut
}

So, SomeClass.doSomeStuff expects a callable, and will ".call()" it and let thrown exceptions go out unchanged. I'm very annoyed having to create the "new Callable" in every one of the four advises, so I've tried to have a static method that gets a ProceedingJoinPoint in, to which I'd pass "(ProceedingJoinPoint)thisJoinPoint", like so:

private static final Object doSomeStuffAroundProceed(final ProceedingJoinPoint pjp) {
return SomeClass.doSomeStuff(new Callable<Object>(){
@Override public final Object call() throws Exception {
pjp.proceed();
}
});
}

But it turns out that when I do this, "pjp.proceed()" does nothing. Is there anything i'm doing wrong.

Other than that, is there a way I could write a single advice that'd advise the four cases at once?

Thanks in advance,
[
 Romain Muller Software Development Engineer | romain.muller@xxxxxxxxx ]


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Back to the top