Bug 74913

Summary: !cflowbelow(within(A)) causes ExceptionInitializerError
Product: [Tools] AspectJ Reporter: Adrian Powell <apowell>
Component: CompilerAssignee: Adrian Colyer <adrian.colyer>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3    
Version: 1.2   
Target Milestone: 1.2.1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Adrian Powell CLA 2004-09-24 01:00:03 EDT
The following cflowbelow() pointcut causes an ExceptionInitializerError and
dialog box to appear, title "Java Virtual Machine Launcher", body "Could not
find the main class.  Program will exit".  Eclipse 3.0, ajdt 1.1.12 (AspectJ
1.2.0 I think).

---
package test;

public class SomeClass {
	public static void main(String[] args) {
	}
}
--
package test;
public aspect TraceClass {
    pointcut pc() : !cflowbelow(within(TraceClass));
    before () : pc() {
        System.out.println("-> " + thisJoinPointStaticPart);
    }
}
---
Output:

java.lang.ExceptionInInitializerError
	at test.SomeClass.<clinit>(SomeClass.java)
Caused by: org.aspectj.lang.NoAspectBoundException: test_TraceClass
	at test.TraceClass.aspectOf(TraceClass.aj)
	at test.TraceClass.<clinit>(TraceClass.aj)
	... 1 more
Exception in thread "main"
Comment 1 Adrian Colyer CLA 2004-09-24 10:23:22 EDT
Strange as it may seem, this program is working as designed!

Here's what's going on: 

* You have pointcut that matches EVERY join point that is NOT in the control 
flow below a join point in TraceClass. 

* One join point matched by this pointcut is the staticinitialization join point 
for SomeClass (the very first join point in the program execution). 

* In order to put into effect the before advice at this join point, the aspect 
TraceClass needs to be called. 

* This causes the TraceClass aspect to be loaded, giving rise to a 
staticinitialization join point. 

* The staticinitialization join point for TraceClass itself is also matched by 
the pointcut - so you have an aspect trying to advise its own initialization.

* This is the situation that leads to the "NoAspectBoundException" (there is no 
instance of the aspect bound at the point at which the advice should execute).

* Since the NoAspectBoundException is thrown during initialization of the 
SomeClass class, you get the "ExceptionInInitializer" error.

The short summary is that an aspect can't advise its own static initialization.
Comment 2 Adrian Colyer CLA 2004-10-21 04:30:53 EDT
Fix released as part of AspectJ 1.2.1