How to exclude anonymous internal method from pointcut?

I have an AspectJ tracking procedure configured to enter and exit log mode using the following points:

public aspect Trace { pointcut anyMethodExecuted(): execution (* biz.ianw.lanchecker.*.*(..)) && !within(Trace) && !within( is(AnonymousType) ); pointcut anyConstructorExecuted(): execution (biz.ianw.lanchecker.*.new(..)) && !within(Trace); 

In my sendEmail class, I have a method that calls the setDebugOut method to redirect debug output to a LogOutputStream:

 final private static Logger log = LoggerFactory.getLogger(MailMail.class); ... LogOutputStream losStdOut = new LogOutputStream() { @Override protected void processLine(String line, int level) { log.debug(line); } }; public void sendPlainHtmlMessage(...) { Session session = javaMailSender.getSession(); PrintStream printStreamLOS = new PrintStream(losStdOut); session.setDebugOut(printStreamLOS); ... 

This works great, except that the pointcut of the Trace class intercepts a call to the anonymous inner class, creating as output:

 20:14:18.908 TRACE [biz.ianw.lanchecker.Trace] - Enters method: Logger biz.ianw.lanchecker.MailMail.access$0() 20:14:18.909 TRACE [biz.ianw.lanchecker.Trace] - Exits method: Logger biz.ianw.lanchecker.MailMail.access$0(). 20:14:18.909 TRACE [biz.ianw.lanchecker.Trace] - with return value: Logger[biz.ianw.lanchecker.MailMail] 20:14:18.909 DEBUG [biz.ianw.lanchecker.MailMail] - DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] 

I added a pretty wide

 && !within( is(AnonymousType) ) 

the condition for the pointcut, as shown above, but it had no effect. Actually, I have a real difficulty in finding (anonymous type), somewhere documented somewhere.

How can I write a pointcut that excludes this anonymous internal method, preferably without affecting others?

+6
source share
1 answer

This answer was kindly provided by Andrew Clement (see http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg14906.html , ff), posted here with his permission:

The $ 0 access method has been added to MailMail since the journal is private in MailMail - it allows log.debug (string) to access the journal from an anonymous class (supposedly called MailMail $ 1).

Recognizing this, we see that $ 0 access is not in an anonymous class, it is an accessor generated in the MailMail class, so your extra pointcut snippet does not work.

A couple of options:

Exclude it specifically:

 pointcut anyMethodExecuted(): execution (* biz.ianw.lanchecker.*.*(..)) && !within(Trace) && !execution(* MailMail.access$0(..)); 

Exclude all synthetic accessors (considered synthetic because it is "generated by the compiler to support what you are doing":

 pointcut anyMethodExecuted(): execution (* biz.ianw.lanchecker.*.*(..)) && !within(Trace) && !execution(synthetic * access$*(..)); 

Or you could exclude all synthetics:

 pointcut anyMethodExecuted(): execution (!synthetic * biz.ianw.lanchecker.*.*(..)) && !within(Trace); 
+3
source

All Articles