[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-dev] How does AspectJ implement load-time weaving?

Murali, the Java “language” specification guarantees that the VM will always load the superclass of a class before it loads the class itself. So this situation can never arise.

 

See step 7 at http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2 for the details of how this is guaranteed.

 


From: aspectj-dev-bounces@xxxxxxxxxxx [mailto:aspectj-dev-bounces@xxxxxxxxxxx] On Behalf Of murvaja (sent by Nabble.com)
Sent: Thursday, January 05, 2006 9:31 PM
To: aspectj-dev@xxxxxxxxxxx
Subject: [aspectj-dev] How does AspectJ implement load-time weaving?

 

I've been trying to understand how AspectJ manages to match the '+' pointcut construct at runtime for load-time weaving. The example I have in mind is for the Hotspot JVM 1.5, with load-time weaving enabled through the -javaagent option, and goes as follows.

Say I have a class hierarchy like this:

public class A
{
   public void foo(){}
}  

public class B extends A
{
   
}  

public class C extends B
{
    public void foo(){}
}  

And I write a pointcut and advice as follows:

pointcut pc() : execution (* A+.foo() )

before() : pc() {
//weaving code here
...
}

According to the pointcut definition, all subclasses of A will match the pointcut, so the load-time weaving should insert the weaving code at the beginning of both A.foo and C.foo. This will happen at their respective class-load times, that is, A.foo will be weaved when A is loaded and C.foo will be weaved when C is loaded.

My question is, how does the weaver do this if C is loaded before A and B, like in the following code (assume none of A, B or C can be loaded by the system class loader) ?


public static void main (String args[])
{
   ClassLoader loader = new MyClassLoader();
   loader.loadClass("C");
}


At the time C is loaded, the weaver will have C's bytes at hand and will have to decide whether to weave them or not. According to the pointcut definition, the bytecodes should be weaved because C is of type A. However, the weaver cannot know that at this time, because the C bytecode only contains a reference to its direct superclass B (not A), and the bytes for B are not yet available because B is not yet loaded.

Any leads? I've been trying to find documentation on this but to no avail. Thanks in advance,

-Murali


View this message in context: How does AspectJ implement load-time weaving?
Sent from the AspectJ - dev forum at Nabble.com.