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

Hi Romain,

I'm afraid the ProceedingJoinPoint was only created to facilitate
annotation style aspects.  It isn't intended to be used in code style
aspects right now.  As you have discovered it isn't wired up and
proceed() will not do anything when called upon it.  The parameter
setup code that gets us ready to invoke advice isn't the same for code
style vs annotation style and so the closure hasn't been provided in
the PJP object for us to invoke.  What you want to do is perfectly
valid, but it isn't going to work at the moment.  If you convert your
aspect/advice to annotation style, it should start working (but hardly
a great workaround, I know!).

On the other topic, I know handling exceptions in advice isn't ideal
right now - there are a few open bugzillas to cover enhancements to
it.  The problem is that whilst there is a relatively simple
workaround (just duplicate the code), the issues don't bubble to the
top of the priority list.

cheers,
Andy

On 27 July 2012 05:16, Romain Muller <romain.muller@xxxxxxxxx> wrote:
> 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
>


Back to the top