Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: [ajdt-dev] Advice, raising different exceptions depending on 'pointcut'

Hi Marco -

This might be a time when enumeration is better than generalization
since generalizing involves reflection and wrapping.

E.g., (warning, untested code)

  pointcut setOwnerCalls(Owner o) : args(o) 
    && call(* com.myapp.ejbs..setOwner(Owner));

  // XException
  before(Owner o) throws XException : setOwnerCalls(o)
    && call(* *(..) throws XException) {
    if (invalidOwner(o)) throw new XException("invalid owner " + o);
  }

  // YException
  before(Owner o) throws YException : setOwnerCalls(o)
    && call(* *(..) throws YException) {
    if (invalidOwner(o)) throw new YException("invalid owner " + o);
  }

  ...

That way you avoid the cost of reflection and complexity of unwrapping
and you get compile-time exception checking.

If you have to work with unanticipated exceptions, you could use
enumeration for known exceptions and the generalized solution for
the unanticipated ones.  That might mean writing pointcuts like

   pointcut callsDeclaringXException: call(* *(..) throws XException);

   // enumerated...
   before(Owner o) throws XException: setOwnerCalls(o) && 
     callsDeclaringXException() { ... }

   ...

   // general...
   before(Owner o): setOwnerCalls(o) 
    && !callsDeclaringXException() 
    && !callsDeclaringYException()  { ... }

btw, for language questions like this you might use aspectj-users@xxxxxxxxxxx

Wes    

------------Original Message------------
From: "Marco Mistroni" <mmistroni@xxxxxxxxx>
To: "AspectJ Development Tools developer discussions" <ajdt-dev@xxxxxxxxxxx>
Date: Wed, Mar-29-2006 5:11 AM
Subject: [ajdt-dev] Advice, raising different exceptions depending on 'pointcut'
Hello all,
  i have  following usecase..
I need to intercept all calls to   the method setOwner for all ejbs in package com.myapp.ejbs.
If the owner does not satisfy a certain condition, i need to raise an exception. 
the challenge here is that the exception type to be raised must be the same exception that
the intercepted pointcut is catching.
Now, i posted some time ago here about raising exception from aspects, and after that i have 
created a ConcernRuntimeException to be raised if my advice raises some exceptions.
THis exception wraps the original exception so that the calling object will be able to handle the exception properly.

But in this case, my pointcut will intercept methods of different EJBs, which raise potentially 
different exceptions..
I have some constraints for NOT to change so that all ejbs intercepted methods raise exactly the same
exception.

I was wondering if, instead, i use static information from the pointcut to get the Exception raised by 
the intercepted method.. 
i saw something named  CatchClauseSignature... i was wondering if it could be useful.....

here's sample of intercepted methods

EJBX {

  public void setData(XData data) throws XException { 
          ....
   }
}

EJBY {

  public void setData(YData data) throws YException {
          ....
   }
}


i was wondering, thus, if i use the CatchClauseSignature, i could
then put following code

 Exception raisedExc = thisJoinPoint.getSignature().getCatchClauseSignature().getParameterType();

throw new ConcernRuntimeException(raisedExc) 

so i can raise a ConcernRuntimeException that could wrap XExceptiopn and YException...

wil this work?

thanx in advance and regards for you comments

 marco










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



Back to the top