Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Novice Newsgroups: comp.lang.java.programmer Subject: Re: Basic AspectJ Question Date: Sun, 26 Feb 2012 01:43:31 +0000 (UTC) Organization: Your Company Lines: 93 Message-ID: References: <4f4968eb$0$295$14726298@news.sunsite.dk> NNTP-Posting-Host: Dmxgbnvd+eoRDUV2lwYf7Q.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: Xnews/5.04.25 X-Antivirus-Status: Clean X-Notice: Filtered by postfilter v. 0.8.2 X-Antivirus: avast! (VPS 120225-1, 2012-02-25), Outbound message Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12346 Arne Vajhøj wrote in news:4f4968eb$0$295$14726298@news.sunsite.dk: > On 2/25/2012 3:07 PM, Novice wrote: >> I'm finding AspectJ very hard to get into, mostly because the manual >> (AspectJ Language Guide) seems very weak to me. >> >> For example, a construct they use all the time in examples is >> "target()". Clearly, this is an important thing. But I'm darned if I >> can find anything in the manual that actually says what "target()" is >> or does! They just seem to assume that it's obvious. But I've got a >> pretty good imagination and I can imagine lots of things it might be, >> each of which has different implications and significance.... >> >> Can anyone enlighten me on what "target()" does? Even just an >> indication of where it is explained in the manual would be fine. A >> link to a better manual or tutorial on AspectJ, if there is such a >> thing, would also be greatly appreciated. > > Have you looked at the example at: > > doc/faq.html#q:interfacesastypepatterns > > ? > >> And if anyone is aware of a manual or tutorial that explains how to >> do X in Aspect J where there is a long list of X's, that would be >> REALLY helpful. For instance, I'd like to write an "entering" log >> entry for each method (and constructor) as I execute it and I want >> that log entry to include the name of the class and method so I need >> a pointcut and advice that is able to determine the class name and >> method name that is being executed so that I can put it in the >> logging statement. I'm picturing something like: >> >> pointcut entering() : execution ( * * (..)); >> >> before() : entering() { >> this.logger.entering(className, methodName); >> } >> >> I know that some context information is available in each method but >> I'm not sure how to get the class and method names so that I can put >> them in the advice. >> >> That's why I'd love to find a "How to do X" list with lots of >> different X's in it. With a bit of luck, what I'm trying to do would >> be on the list and I get to what I want to do a little more quickly >> than churning through the Language Guide... > > Reading the docs and google a bit is sufficient for most AspectJ > questions. > > Regarding the specific question the the JoinPoint instance > has the necessary info. > > Example from my shelf: > > import java.lang.reflect.*; > > import org.aspectj.lang.*; > import org.aspectj.lang.annotation.*; > > @Aspect > public class Trace { > @Pointcut("call(public * IntMath.*(..)) || call(public * > DoubleMath.*(..))") > public void mathtrace() { }; > @Before("mathtrace()") > public void enter(JoinPoint thisJoinPoint) { > System.out.println("Enter " + thisJoinPoint.getSignature()); > } > @AfterReturning("mathtrace()") > public void leave(JoinPoint thisJoinPoint) { > System.out.println("Exit " + thisJoinPoint.getSignature()); > } > } > > (this example only trace methods in two specific classes, but ...) > > Check: > > http://www.eclipse.org/aspectj/doc/next/runtime-api/org/aspectj/lang/Jo > inPoint.html to see what you can get out. > Have I told you lately that you rock, Arne? ;-) Thanks a lot! I was working through the manual hoping that they would say this somewhere along the line but it was taking a long time and I wasn't seeing anything like what I wanted. Your example was VERY helpful. -- Novice