Aspect-Orientated Programming?
I have been looking through a few documents to try give me a better understanding of Aspect-Orientated Programming.
I was wondering if it is possible to write a class using AspectJ which could tell me details about a running Java program such as what methods are called and when.
Basically I am wondering whether it's possible to write a class using aspectJ which can capture information from another running class?
I am not looking to find out how this is done, just whether it is possible.
Thanks.
[531 byte] By [
colly10a] at [2007-10-2 13:41:04]

the idea with AOP is that you write an Aspect (not a class) that is then injected (or woven or whatever you want to call it) into the rest of your classes. So no, don't think of it as some sort of reflection for looking at other classes, think of it as a way to alter your classes in a prescribed way to implement and centralize the logic for cross-cutting concerns.
Hope that helped
Lee
tsitha at 2007-7-13 11:34:25 >

Sorry I don't think I explained it very well.
> don't think of it as some sort of reflection
> for looking at other classes.
I am looking at it more as a replacement for code injection to let me know when a method has been run.
> the idea with AOP is that you write an Aspect (not a
> class) that is then injected (or woven or whatever
> you want to call it) into the rest of your classes.
So suppose I have a standard Java class called A. Is it possible for me to write an aspect that will either inject code into class A at runtime or tell me when a method from class A is run without me actaully having to modify class A manually.
I would basically like to know whether it is possible for me to create an aspect which will allow me to find out when a method is run in any sample Java class that I am given without me actaully needing to manually modify the class?
Thanks for your help.
[snip]
> I would basically like to know whether it is possible
> for me to create an aspect which will allow me to
> find out when a method is run in any sample Java
> class that I am given without me actaully needing to
> manually modify the class?
> Thanks for your help.
I am not an expert, so it's quite possible that I am wrong, but that's never stopped me before. It depends on what you mean by "manually modify". AOP is all about modifying the class, so if you meant "without modifying class A" then I think it's safe to say no. But if you didn't mean that then I don't understand what you're getting at, since whatever AOP "compiler" you use is the thing that does the modification, so it would never be a "manual" process.
tsitha at 2007-7-13 11:34:25 >

> It depends on what you mean by "manually modify". AOP
> is all about modifying the class, so if you meant
> "without modifying class A" then I think it's safe to
> say no. But if you didn't mean that then I don't
> understand what you're getting at, since whatever AOP
> "compiler" you use is the thing that does the
> modification, so it would never be a "manual" process.
I have no problem with the AOP compiler modifing class A, by manually modifying I mean me needing to insert any AOP code into class A, to get the information about when a perticular method is run.
If I personally need to make no modifications to the source code of class A whatsoever because the compiler will deal with it then I would be happy with that.
> > It depends on what you mean by "manually modify".
> AOP
> > is all about modifying the class, so if you meant
> > "without modifying class A" then I think it's safe
> to
> > say no. But if you didn't mean that then I don't
> > understand what you're getting at, since whatever
> AOP
> > "compiler" you use is the thing that does the
> > modification, so it would never be a "manual"
> process.
>
> I have no problem with the AOP compiler modifing
> class A, by manually modifying I mean me needing to
> insert any AOP code into class A,
Right - (one of) the point of AOP is to not have to modify any source code.
> to get the
> information about when a perticular method is run.
>
> If I personally need to make no modifications to the
> source code of class A whatsoever because the
> compiler will deal with it then I would be happy with
> that.
Yes, you would just create a pointcut that encompasses all methods in A, then write what you want done for that join point in your aspect (specifics depend on what AOP implementation you use). This gets injectd into the bytecode for A.
Good Luck
Lee
tsitha at 2007-7-13 11:34:25 >

Aspects can do method interception as a point-cut. Check out Spring Framework's aspects or AspectJ. Both are open source. However, I would caution against 'aggressive' use of aspects. Start slowly. IMO, they are great tools for cross-cutting functionality like audit, security, logging, etc., however, they do make testing and understanding what is going on a bit more complicated.
- Saish
Saisha at 2007-7-13 11:34:25 >

> Yes, you would just create a pointcut that
> encompasses all methods in A, then write what you
> want done for that join point in your aspect
I assume though that I would have to add these point cuts to the source code of class A myself? Thers is no way to get these point cuts into class A without writing into the source?
Certainly. Spring allows point-cuts to be declared in XML configuration files. - Saish
Saisha at 2007-7-13 11:34:25 >

Best of luck.- Saish
Saisha at 2007-7-13 11:34:25 >

> > Yes, you would just create a pointcut that
> > encompasses all methods in A, then write what you
> > want done for that join point in your aspect
>
> I assume though that I would have to add these point
> cuts to the source code of class A myself? Thers is
> no way to get these point cuts into class A without
> writing into the source?
Just to reiterate - things like pointcuts are part of the *aspect*, not the source - (one of ) the point of AOP is to not have to touch the source, all the logic is contained within the *aspect* and injected into the *class* file, not the source file.
tsitha at 2007-7-13 11:34:25 >

> > > Yes, you would just create a pointcut that
> > > encompasses all methods in A, then write what
> you
> > > want done for that join point in your aspect
> >
> > I assume though that I would have to add these
> point
> > cuts to the source code of class A myself? Thers
> is
> > no way to get these point cuts into class A
> without
> > writing into the source?
>
> Just to reiterate - things like pointcuts are part of
> the *aspect*, not the source - (one of ) the point of
> AOP is to not have to touch the source, all the logic
> is contained within the *aspect* and injected into
> the *class* file, not the source file.
Right. You can think of servlet filters as being similar to aspects. You write the filter, but you only run it when you put it into your web.xml. It can be called before the servlet to operate on the request or, in the latest version of the servlet spec, after the servlet to operate on the response.
Your point exactly, Lee. It's not part of the source code for the class being operated on. It's a separate class, configured in a deployment descriptor.
%
Lots of good, hands on, AOP articles can be found here: http://www-128.ibm.com/developerworks/views/java/libraryview.jsp?search_by=aop@workRegards,Arvid