Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] Question about aspectj weaver

Ivica Aracic wrote:
I have a question about the aspectj weaver:
What does the weaver take as input?
How does it know where to insert the advices?

This question deserves a paper to answer it. For now, you'll have to settle for the following short answer.

Each piece of advice in an aspect is associated with a pointcut. This pointcut is stored in an attribute on the methods corresponding to each piece of advice. Before weaving, all of these pieces of advice are gathered into one large list.

Each .class file is woven independently. A .class file is woven by the following steps:

1. Collect all of the joinpoint shadows in the .class file. For every dynamic joinpoint in the AspectJ language model, there is a corresponding static shadow of that joinpoint in the bytecode. For example, every method call joinpoint has an INVOKE bytecode as its static shadow. Some joinpoints (such as initialization) have much more complicated static shadows.

2. Each piece of advice is matched to each static shadow. There are three results possible from this match.

a. Never matches -- in which case nothing is done to the shadow
b. Always matches -- in which case the advice is woven into this joinpoint shadow c. Sometimes matches -- in which case the advice is woven into the shadow along with the minimal dynamic tests to determine if any particular joinpoint in the actual running program matches the advice. The simplest example of sometimes matches is when the pointcut uses if(test()).

3. If any advice matched any static shadows in the .class file, then the transformed .class file is written out, otherwise it is left unchanged.

See org.aspectj.weaver.bcel.BcelClassWeaver and BcelShadow for the two primary classes involved in this process.

Note: This explanation ignores the implementations of inter-type declarations completely. It also ignores performance optimizations such as fast-match that speed up the weaving process.


Back to the top