Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Excluding Particular Joinpoints

Doh! I mislabeled the first "file".  It is actually
"ExcludeExceptions.aj", just to be clear.

dean

On 11/30/05, Dean Wampler <deanwampler@xxxxxxxxx> wrote:
> You can exclude the method annotation in the PCD itself. Here is a
> complete example:
>
> TestExcludeExceptions.aj: Aspect that declares a nested method
> annotation "DontLog". Note the PCD that excludes methods with this
> annotation.
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Target;
>
> public aspect ExcludeExceptions {
>         @Target(ElementType.METHOD)
>         public static @interface DontLog {}
>
>         pointcut watch(): execution (!@DontLog * *.*(..));
>
>         after() throwing(Throwable th): watch() {
>                 System.err.println("Oops! "+thisJoinPointStaticPart+": "+th);
>         }
> }
>
> TestExcludeExceptions.aj: Test routine. You'll see the advice "Oops!"
> message only after calls to "failUnexpected()", because
> "failExpected()" has the marker annotation "DontLog". (The "okay()"
> method is for "background subtraction" - that's a bone for you other
> physicists out there... ;)
>
> public class TestExcludeExceptions {
>         public void okay() {
>                 System.out.println ("TestExcludeExceptions.okay():");
>         }
>
>         @ExcludeExceptions.DontLog
>         public void failExpected() {
>                 System.out.println ("TestExcludeExceptions.failExpected():");
>                 throw new RuntimeException();
>         }
>
>         public void failUnexpected() {
>                 System.out.println ("TestExcludeExceptions.failUnexpected():");
>                 throw new RuntimeException();
>         }
>
>         public static void main(String[] args) {
>                 TestExcludeExceptions tee = new TestExcludeExceptions();
>                 tee.okay();
>                 try {
>                         tee.failExpected();
>                 } catch (Throwable th) {}
>                 try {
>                         tee.failUnexpected();
>                 } catch (Throwable th) {}
>        }
> }
>
> dean
>
> On 11/30/05, Ron Bodkin <rbodkin@xxxxxxxxxxxxxx> wrote:
> >
> >
> >
> > You can use the signature at the enclosing join point static part to get the
> > right Java reflective object (a Method or Constructor). For example,
> > ((MethodSignature)thisEnclosingJoinPointStaticPart.getSignature()).getMethod().getAnnotation(myAnnotationType);
> >
> >
> >
> > You probably want to have at least 2 cases for method and constructor. To be
> > totally complete there are 6 cases (for advice, initialization,
> > pre-initialization and static initialization). To support static
> > initialization you'd want to have annotations on the class too…
> >
> >
> >
> > http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg00655.html
> >
> >
> >
> >  ________________________________
> >
> >
> > From: aspectj-users-bounces@xxxxxxxxxxx
> > [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of
> > Tobias Dunn-Krahn
> >  Sent: Wednesday, November 30, 2005 4:13 PM
> >
> >  To: aspectj-users@xxxxxxxxxxx
> >  Subject: RE: [aspectj-users] Excluding Particular Joinpoints
> >
> >
> >
> >
> > Hi Ron,
> >
> >
> >
> > I agree that annotating the method is the cleanest proposal so far.  One
> > problem though… I can't figure out how to examine annotations from
> > thisEnclosingJoinpointStaticPart (or the JoinPoint
> > interface in general).  Any hints?
> >
> >
> >
> > Tobias
> >
> >
> >
> >  ________________________________
> >
> >
> > From: aspectj-users-bounces@xxxxxxxxxxx
> > [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Ron
> > Bodkin
> >  Sent: Wednesday, November 30, 2005 2:32 PM
> >  To: aspectj-users@xxxxxxxxxxx
> >  Subject: RE: [aspectj-users] Excluding Particular Joinpoints
> >
> >
> >
> > Hi Tobias,
> >
> >
> >
> > You can use annotations at the method level to address this: you could then
> > look at thisEnclosingJoinPointStaticPart to see if there is
> > an appropriate annotation. If you find that the same exception is handled
> > differently in the same method, I'd suggest extracting a method to deal with
> > this. This would be a lot cleaner than using a naming convention on the
> > methods.
> >
> >
> >
> > As you noted, it isn't possible to use variable annotations both because
> > they are not retained in the binary representation and AspectJ relies on
> > binary representations for weaving. Also AspectJ doesn't in general expose
> > information about local variables (e.g., there is way to match pointcuts or
> > expose join point information based on local variables). By the same token,
> > you can't determine the name of the local variable being used in the catch
> > block, so a naming convention like this wouldn't help with AspectJ. This is
> > a good thing IMHO: it would be too brittle if renaming local variables would
> > change pointcut matching…
> >
> >
> >
> >  ________________________________
> >
> >
> > From: aspectj-users-bounces@xxxxxxxxxxx
> > [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of
> > Tobias Dunn-Krahn
> >  Sent: Wednesday, November 30, 2005 2:03 PM
> >  To: gregor@xxxxxxxxx; aspectj-users@xxxxxxxxxxx
> >  Subject: RE: [aspectj-users] Excluding Particular Joinpoints
> >
> >
> >
> > Gregor and Ron,
> >
> >
> >
> > Thanks for your suggestions.  Unfortunately in my case, I can't determine
> > from just the Throwable whether or not it should be logged.  For example, a
> > FooBarException should be logged in some cases but not others, depending on
> > the surrounding application logic.  Really the only way to determine this is
> > a decision on the part of developer, which is why I was experimenting with
> > annotations.
> >
> >
> >
> > Perhaps I could use thisJoinPoint in combination with a naming convention
> > for the throwable, i.e. any throwable named "expectedThrowable" would not be
> > logged.  It would be nice if I could do something more explicit (like an
> > annotation) but perhaps it's not possible.  Please let me know if I've
> > misunderstood something about your suggestions.
> >
> >
> >
> > Thanks,
> >
> > Tobias
> >
> >
> >
> >  ________________________________
> >
> >
> > From: aspectj-users-bounces@xxxxxxxxxxx
> > [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of
> > Gregor Kiczales
> >  Sent: Wednesday, November 30, 2005 1:26 PM
> >  To: aspectj-users@xxxxxxxxxxx
> >  Subject: RE: [aspectj-users] Excluding Particular Joinpoints
> >
> >
> >
> >  ________________________________
> >
> >
> > From: aspectj-users-bounces@xxxxxxxxxxx
> > [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of
> > Tobias Dunn-Krahn
> >
> >
> >
> > I am using AspectJ in a project to log all handled exceptions.  In general
> > this is what I want, but in a small number of cases such exceptions are
> > "expected" and I do not wish to log them.
> >
> >
> >
> > aspect FooBarExceptionLoggingStrategy {
> >
> >
> >
> >   before(Throwable t): handler(Throwable) && args(t) {
> >
> >     if( shouldLog(t) )
> >
> >        log(t);
> >
> >   }
> >
> >
> >
> >   private boolean shouldLog(Throwable t) {
> >
> >     <<test whatever needs testing here>>
> >
> >   }
> >
> >
> >
> >   private void log(Throwable t) {
> >
> >     ...
> >
> >   }
> >
> > }
> >
> >
> >
> > The point being that you can test the exception in any way you want before
> > logging it.
> >
> > You could also pass thisJoinPoint or thisJoinPointStaticPart. If you want,
> > you could
> >
> > move shouldLog into the actual pointcut, using if(shouldLog(t)).
> >
> >
> >
> >
> >
> >
> >
> >  ________________________________
> >
> >
> > From: aspectj-users-bounces@xxxxxxxxxxx
> > [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of
> > Tobias Dunn-Krahn
> >  Sent: Wednesday, November 30, 2005 11:42 AM
> >  To: aspectj-users@xxxxxxxxxxx
> >  Subject: [aspectj-users] Excluding Particular Joinpoints
> >
> > Hello All,
> >
> >
> >
> > I am using AspectJ in a project to log all handled exceptions.  In general
> > this is what I want, but in a small number of cases such exceptions are
> > "expected" and I do not wish to log them.  My pointcut definition is
> > currently very simple; it matches all exception handlers except those that
> > handle SocketTimeoutExceptions:
> >
> >
> >
> >   pointcut exceptionHandlers(Throwable t) : handler(Throwable+) && (args(t)
> >
> >       && !args(java.net.SocketTimeoutException));
> >
> >
> >
> > However, now I need to exclude matching of particular exception handlers
> > that are not distinguishable by type.  I tried creating a custom annotation
> > with target type LOCAL_VARIABLE and using this annotation to decorate the
> > exception, but unfortunately annotations of this type are not retained for
> > runtime (and are therefore inaccessible).  Does anyone have any suggestions?
> >
> >
> >
> > Thanks in advance,
> >
> > Tobias
> >
> >
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> >
> >
> >
>
>
> --
> Dean Wampler
> http://www.aspectprogramming.com
> http://www.newaspects.com
> http://www.contract4j.org
>


--
Dean Wampler
http://www.aspectprogramming.com
http://www.newaspects.com
http://www.contract4j.org

Back to the top