Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Advising advice?

Hi Ron,

You absolutely can advise advice: I presented some useful examples of doing
this in my articles on the Glassbox Inspector in the AOP@Work series. You
can give any type of advice a name with an annotation. You can also use
reflection to match the name of an @AspectJ advice body. Indeed, on your
earlier thread about the protocol for aspects interacting with mainline
code, you could use an testing aspect to check for other advice applying at
join points without the expected annotation :-)

Here are two examples of advising advice based on names:

For @AspectJ syntax:

import java.lang.annotation.*;
import java.lang.reflect.*;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.*;
import org.aspectj.lang.*;
import org.aspectj.lang.JoinPoint.StaticPart;

@Aspect
public class AtAdviceName {
    //can't use general if in @AspectJ pointcuts...
    //@Before("adviceexecution() &&
if(getAdviceName(thisJoinPointStaticPart).indexOf(\"Service\")!=-1)")
    @Before("adviceexecution() && !within(AtAdviceName)")
    public void adviseServiceAdvice(StaticPart staticPart) {
	if(getAdviceName(staticPart).indexOf("Service")!=-1) {
            System.out.println("advising service: "+staticPart+",
name="+getAdviceName(staticPart));
        }
    }

    public static String getAdviceName(StaticPart staticPart) {
        AdviceSignature adviceSig =
(AdviceSignature)staticPart.getSignature();
        return adviceSig.getAdvice().getName();
    }

}

@Aspect
class AdvisedAspect {
    @Before("execution(* main(..))")
    public void testService() {
        System.out.println("service");
    }

    @Before("execution(* main(..))")
    public void skip() {
        System.out.println("not a service");
    }

    public static void main(String args[]) {
    }
}

For code syntax:
import java.lang.annotation.*;
import java.lang.reflect.*;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.*;

public aspect AdviceNameExample {
    before(AdviceName name) : adviceexecution() && @annotation(name) &&
if(name.value().indexOf("Service")!=-1) {
        AdviceSignature adviceSig =
(AdviceSignature)thisJoinPointStaticPart.getSignature();
        System.out.println("advising service: "+thisJoinPointStaticPart+",
name="+adviceSig.getAdvice().getAnnotation(AdviceName.class).value());
    }

    @AdviceName("TrackService")
    before() : execution(* main(..)) {
        System.out.println("service");
    }

    @AdviceName("Skip")
    before() : execution(* main(..)) {
        System.out.println("not a service");
    }

    public static void main(String args[]) {
    }
}




-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Ronald J Mann
Sent: Tuesday, February 14, 2006 4:53 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Advising advice?

Another foolish question.  Is it possible to advise advice?   In
searching for an answer to this question, I ran across an older version
of the Developer notebook that mentioned the annotation styles use of a
method name made this possible, but it would appear that this passage
has been removed from the current documentation.  It also referenced an
annotation @AdviceName() whose current documentation states "...may in
future be used in adviceexecution() pcd."  I tried something like the
following, but this issues the 'adviceDidNotMatch' warning on the @After
declaration.  So am I to assume that this is not possible at the moment?

@Aspect
public class A {

    @Before( "call(void C.bar( int )) && args( i ) " )
    public void doBar( int i ) {
       System.out.println("What is 6 x 9?");
    }

    @After( "execution(void A.doBar( int ))" )
    public void adviseAdvise() {
        System.out.println("Everybody knows that..." );
    }
}

Thanks!

=Ron=




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




Back to the top