Basic AspectJ Question

N

Novice

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.

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...
 
A

Arne Vajhøj

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/JoinPoint.html
to see what you can get out.

Arne
 
N

Novice

Have you looked at the example at:

doc/faq.html#q:interfacesastypepatterns

?


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.
 
A

Arved Sandstrom

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.

I assume you meant the Programming Guide. If you're looking at that in
examples, it should be Right-In-Your-Face (TM) evident that target() is
a pointcut. After that you refer to Appendix B Language Semantics, and
find target(). The section on State-based Pointcuts explains this(0 and
target() pointcuts quite well.

You should get from that explanation that if you wanted your pointcut to
apply when execution is about to transfer to a Java object of type
org.novice.MyClass, say, that target(org.novice.MyClass) would be
useful. This particular primitive pointcut is particularly useful when
combined with others.
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.

Pointcut parameters. Each join point has 3 items of state: current
object, target object, and arguments. What you do with pointcut
parameters is to "publish" context from the selected join points. In
your example you have

pointcut entering() : ...

so because of the empty parentheses you are publishing nothing.

This is where the pointcut primitives this(), target() and args() also
come in real handy, if you use them with Java identifiers rather than types.

In your example you'd like the advice to be

before(String className, String methodname) :
entering(className, methodName) {
log(className, methodName);
}

and you can do that by writing the pointcut as

pointcut entering(String className, String methodName) : execution( * *
(..)) && args(className, methodName);

This will work OK with the '..' wildcard for the parameters, but you'll
want to be sure that the method join points that get picked actually do
have those 2 arguments.

As a complete aside, using the this(), target() and args() pointcuts
like this sort of reminds me of Prolog unification. :)
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...
Getting Started With in the Programming Guide should help, as should the
FAQ. I think you're getting a bit ahead of yourself, maybe trying to get
too much coded up with aspects before doing some solid, necessary
reading and experimenting. Seriously though, the existing AspectJ docs
are simply loaded with good examples.

AHS
 
N

Novice

I assume you meant the Programming Guide.

I've got two manuals in my Eclipse installation and they're called
"AspectJ Development User Guide" and "AspectJ Language Guide" in the high
level help index. (I'm running Eclipse 3.7.1 and installed the latest
version of AJDT a couple of days ago.) So that's why I'm calling it the
Language Guide. But I see that there are some lesser books _within_ the
Language Guide, including a Programming Guide, so we are really talking
about the same thing, more or less.
If you're looking at that in
examples, it should be Right-In-Your-Face (TM) evident
:)

that target() is a pointcut. After that you refer to Appendix B
Language Semantics,
and find target(). The section on State-based Pointcuts explains
this(0 and target() pointcuts quite well.
Yes, I see target() discussed in the Pointcuts section of Language
Semantics.

I didn't phrase my original question/remark as well as I should have.
Yes, the first page where I encountered target() ("Introduction to
AspectJ") explicitly says it's a pointcut but, to make a long story
short, I didn't quite see what it meant or how to use it. I kept reading
forward, hoping for clarification, and just kept encountering more and
more things that raised my eyebrows. I see know that I should have just
headed for Language Semantics.
You should get from that explanation that if you wanted your pointcut
to apply when execution is about to transfer to a Java object of type
org.novice.MyClass, say, that target(org.novice.MyClass) would be
useful. This particular primitive pointcut is particularly useful when
combined with others.
As I expected, target() has a perfectly reasonable meaning/purpose. I was
just getting frustrated trying to find out where that meaning/purpose was
explained. The examples, as frequent as they are, weren't explained in
much detail so they had a sense of "Hey, kids, here's something cool you
can do!" without giving me much insight into how the code actually
worked.
Pointcut parameters. Each join point has 3 items of state: current
object, target object, and arguments. What you do with pointcut
parameters is to "publish" context from the selected join points. In
your example you have

pointcut entering() : ...

so because of the empty parentheses you are publishing nothing.
I didn't mean to present that as if it seemed like a complete solution. I
just meant it as a skeleton that I knew had to be fleshed out. ;-)
This is where the pointcut primitives this(), target() and args() also
come in real handy, if you use them with Java identifiers rather than
types.

In your example you'd like the advice to be

before(String className, String methodname) :
entering(className, methodName) {
log(className, methodName);
}
I hadn't come across anything yet that said what you could/should put in
the brackets after the "before". That was frustrating me too!
and you can do that by writing the pointcut as

pointcut entering(String className, String methodName) : execution( *
* (..)) && args(className, methodName);

This will work OK with the '..' wildcard for the parameters, but
you'll want to be sure that the method join points that get picked
actually do have those 2 arguments.
Well, I'm just playing around with the idea of doing logging from
aspects. Many of my current methods have this as the first two lines:

String methodName = "foo()";
this.logger.entering(this.CLASS_NAME, methodName);

[where className is a class variable defined as final String CLASS_NAME =
getClass().getName();]

I was trying to figure out how I'd write that in an aspect so that I
could remove the "entering" statement from each method.
As a complete aside, using the this(), target() and args() pointcuts
like this sort of reminds me of Prolog unification. :)
Sorry, that one went right over my head. I know Prolog is a programming
language but I've never used it and have no idea what Prolog unification
is ;-)
Getting Started With in the Programming Guide should help, as should
the FAQ. I think you're getting a bit ahead of yourself, maybe trying
to get too much coded up with aspects before doing some solid,
necessary reading and experimenting. Seriously though, the existing
AspectJ docs are simply loaded with good examples.
I do tend to be impatient when learning something new. I'm in something
of a hurry to improve my current logging and I'm hoping to get that done
very quickly and then come back and explore AspectJ in more depth as time
permits. I was looking at the manual hoping to encounter a way of doing
logging and developing a little prototype that would get the essence of
the code I need.

Thanks for your assistance with this Arved. I'll look to Language
Semantics to help me over unclear bits of the manual and, of course, post
questions about things that aren't clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top