Newbie - need very basic examples - AspectJ annotation style

View: New views
3 Messages — Rating Filter:   Alert me  

Newbie - need very basic examples - AspectJ annotation style

by CheapLisa () :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My IDE only supports annotation style with AspectJ and I am in need of examples for annotation style.  I bought a book (no examples with annotation style) and have read a lot of documentation but can not get this one simple thing to work.

1) Example of an aspect that runs after any Throwable and logs the exception (cause, message) + stack trace (any throwable anywhere Runtime exception or checked exception.

2) Example same as the above (run after any Throwable) but limited to any Throwable in a specific package like "com.mycompany.*"

I found examples that are non-annotation style, tried to translate them to annotation style but they did not work.  This is such a common thing I believe someone will have an example they can attach, email or paste into this response.

I recently inherited a big blob of code and believe there are all sorts of things going on under the hood that I can't see.  This would help me log any exception anywhere and see what may be going awry.

I believe if I had examples of the @Pointcut and @After I could fill in the body of the method to do what I need.  I'm just not sure how to catch every throwable and then write another one that will catch every throwable given a package name.

thanks!!!


Lisa

Re: Newbie - need very basic examples - AspectJ annotation style

by Ross Cohen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It's true that the online documentation is not terribly organized, and that
examples of many common cases are missing.    But the information
*is* there -- you just have to dig for it.    A lot of what you're looking
for will be in the developers notebook (which has most of the annotation
style
documentation):
http://www.eclipse.org/aspectj/doc/released/adk15notebook/index.html

Ross


CheapLisa wrote:

>My IDE only supports annotation style with AspectJ and I am in need of
>examples for annotation style.  I bought a book (no examples with annotation
>style) and have read a lot of documentation but can not get this one simple
>thing to work.
>
>1) Example of an aspect that runs after any Throwable and logs the exception
>(cause, message) + stack trace (any throwable anywhere Runtime exception or
>checked exception.
>
>2) Example same as the above (run after any Throwable) but limited to any
>Throwable in a specific package like "com.mycompany.*"
>
>I found examples that are non-annotation style, tried to translate them to
>annotation style but they did not work.  This is such a common thing I
>believe someone will have an example they can attach, email or paste into
>this response.
>
>I recently inherited a big blob of code and believe there are all sorts of
>things going on under the hood that I can't see.  This would help me log any
>exception anywhere and see what may be going awry.
>
>I believe if I had examples of the @Pointcut and @After I could fill in the
>body of the method to do what I need.  I'm just not sure how to catch every
>throwable and then write another one that will catch every throwable given a
>package name.
>
>thanks!!!
>
>
>Lisa
>  
>
_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Newbie - need very basic examples - AspectJ annotation style

by Andy Clement :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As Ross says, the developers notebook is still the best place to find that info.  Here is a demo program:

import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;

@Aspect
class TrackExceptions {

  // Detect after throwing advice to catch methods that throw exceptions:
        @Pointcut("execution(* *(..))  && within(Demo)") 
        void interestingMethods() {} 
        
        @AfterThrowing(pointcut = "interestingMethods()", throwing = "t") 
        public void logException(JoinPoint thisJoinPoint, Throwable t) { 
 System.err.println("Exception thrown at "+thisJoinPoint.getSourceLocation()+".  Exception was "+t);
        } 

  // 
        @Before("handler(Throwable+) && args(t) && within(Demo)")
        public void watchHandler(JoinPoint thisJoinPoint, Throwable t) { 
 System.err.println("Entered exception handler at "+thisJoinPoint.getSourceLocation()+".  Exception was "+t);
        } 
  
}
public class Demo {
  public static void main(String[]argv) {
    System.err.println("Calling method with handler");
    new Demo().run2();

    System.err.println("Calling method that throws exception");
    new Demo().run();
  }

  public void run() {
    throw new RuntimeException();
  }

  public void run2() {
    try {
      throw new RuntimeException();
    } catch (RuntimeException re) {
    }
  }
}

Andy.


2008/8/28 Ross Cohen <ross.cohen@...>
It's true that the online documentation is not terribly organized, and that
examples of many common cases are missing.    But the information
*is* there -- you just have to dig for it.    A lot of what you're looking
for will be in the developers notebook (which has most of the annotation style
documentation):
http://www.eclipse.org/aspectj/doc/released/adk15notebook/index.html

Ross



CheapLisa wrote:

My IDE only supports annotation style with AspectJ and I am in need of
examples for annotation style.  I bought a book (no examples with annotation
style) and have read a lot of documentation but can not get this one simple
thing to work.

1) Example of an aspect that runs after any Throwable and logs the exception
(cause, message) + stack trace (any throwable anywhere Runtime exception or
checked exception.

2) Example same as the above (run after any Throwable) but limited to any
Throwable in a specific package like "com.mycompany.*"

I found examples that are non-annotation style, tried to translate them to
annotation style but they did not work.  This is such a common thing I
believe someone will have an example they can attach, email or paste into
this response.

I recently inherited a big blob of code and believe there are all sorts of
things going on under the hood that I can't see.  This would help me log any
exception anywhere and see what may be going awry.

I believe if I had examples of the @Pointcut and @After I could fill in the
body of the method to do what I need.  I'm just not sure how to catch every
throwable and then write another one that will catch every throwable given a
package name.

thanks!!!


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


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