Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] After Throwing advice called several times wh en only expecting on ce.

Yes, advice in an abstract aspect on an abstract pointcut
will run for each concrete subaspect of the abstract aspect.

One way to avoid having subaspect pointcuts pick out the
same join point is to have an aspect that regulates them.
See below for one approach; it's not pretty because you
have to enumerate the subaspect binary combinations.
(You could also put the code in the abstract aspect, but
then it knows about (and changes with) all its subaspects.)

Wes

---- SubAdviceRegulator.java

public class SubAdviceRegulator {
    public static void main(String[] args) {
        new C().run();
    }
}
class C {
    void run() { System.out.println("run()"); }
}

abstract aspect A {
   abstract pointcut pc();
    before() : pc() {
        System.out.println("pc: " + thisJoinPointStaticPart);
    }
}

aspect A1 extends A { pointcut pc(): execution(void run()); }
aspect A2 extends A { pointcut pc(): execution(void run()); }
aspect A3 extends A { pointcut pc(): execution(void run()); }

aspect RegulateUsageOfA {
    // if binaryCombinations() is staticly-determinable
    // (WARNING: broken in 1.1.1 - see bug 45184)
    //declare error : binaryCombinations() : "pc overlap";

    // if not staticly-determinable
    pointcut binaryCombinations() : (A1.pc() && (A2.pc() || A3.pc()))
        || (A2.pc() && A3.pc());

    before() : binaryCombinations() {
        throw new Error("overlap at " + thisJoinPointStaticPart);
        // might instead set a flag to skip duplicate advice
    }

    // this aspect depends on knowing all A subaspects, so check that
    declare error : subtypesOfA() && !knownSubtypesOfA() :
       "update RegulateUsageOfA for each subtype of A";

    pointcut subtypesOfA() : staticinitialization(A+ && !A);
    pointcut knownSubtypesOfA() : staticinitialization(A1 || A2 || A3);
}
----


Rustad, Aaron wrote:
OK, I actually think I may know why this was happening. My advice was in a
abstract Aspect, and all my other Aspects were applying the advice to the
same code.
Should this be the way it works? I would think that if you override the
advice in each aspect, then yes, it would apply each time.

AR.

-----Original Message-----
From: Rustad, Aaron Sent: October 19, 2003 9:03 AM
To: 'aspectj-users@xxxxxxxxxxx'
Subject: [aspectj-users] After Throwing advice called several times when
only expecting on ce.



Hello everyone. I am new to the whole AOP scene and I am hoping one of you
can help solve this problem for me.

I have some code that picks out an after throwing joinpoint, but when it
executes and I look at the logging statements, I see that it seems to
execute 4 times. The aspect looks like this:

	after() throwing(CreateEnvelopeException e):
		within(com.blah.edi..*) &&
		call(* com.blah.edi..*(..)) {
			
		Logger logger = getLogger(thisJoinPoint.getThis());
		if (logger.isInfoEnabled()) {
	
logger.info(thisJoinPointStaticPart.getSourceLocation());
			logger.info(e.getMessage(), e);
		}
	}

The joinpoint looks like this:
	throw new CreateEnvelopeException("Envelope version '" + version +
"' is not supported.");

I am using Version 1.1.1 and JDK 1.4.2.

Thanks for your help!
Aaron.
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top