Project: AspectJ
Bug 62642 proper handling of ExceptionInIntializer inside <clinit> in presence of after throwing advice
Reported: 2004-05-18 10:23:19 | Status: resolved fixed

When a <clinit> of a class contains after throwing advice that may catch ExceptionInInitializer errors due to an embedded advice aspectOf() that fails, the ExceptionInIntializer error does not get properly reported. For the example below: public class Main { static int x = 13; int y; public Main() { y= 14; } public static void main(String args[]) { Main m = new Main(); m.y = 3; System.out.println("hi"); } } aspect Aspect { before () : within(*) { System.out.println("BEFORE " + thisJoinPointStaticPart.getKind() + " at " + thisJoinPointStaticPart.getSourceLocation()); } after () : within(*) { System.out.println("AFTER " + thisJoinPointStaticPart.getKind() + " at " + thisJoinPointStaticPart.getSourceLocation()); } } .... when compiled with ajc, and then run, the following exception is given, Exception in thread "main" java.lang.NoClassDefFoundError at Main.<clinit>(Main.java:1) when really one wants the following: Exception in thread "main" java.lang.ExceptionInInitializerError at Main. ... Caused by: org.aspectj.lang.NoAspectBoundException: Aspect at Aspect.aspectOf .. ------------- Here is a fix ... I give the decompiled <clinit> for Main.java and indicate with comments, look for ***LJH ****, what can be woven to fix it. Basically, the catch block for after throwing, when in <clinit>, should first check if the caught exception if a ExceptionInInitializerError, and if so just throw it. ----------- static { org.aspectj.runtime.reflect.Factory r1; java.lang.Throwable r2, r31; int $i0; r1 = new Factory("Main.java", Class.forName("Main")); ajc$tjp_0 = r1.makeSJP("field-set", r1.makeFieldSig("8-x-Main-int-"), 2); ajc$tjp_1 = r1.makeSJP("staticinitialization", r1.makeInitializerSig("8--Main-"), 2); ajc$tjp_10 = r1.makeSJP("preinitialization", r1.makeConstructorSig("1--Main----"), 5); ajc$tjp_2 = r1.makeSJP("field-set", r1.makeFieldSig("0-y-Main-int-"), 6); ajc$tjp_3 = r1.makeSJP("constructor-execution", r1.makeConstructorSig("1--Main----"), 6); ajc$tjp_4 = r1.makeSJP("constructor-call", r1.makeConstructorSig("1--Main----"), 10); ajc$tjp_5 = r1.makeSJP("field-set", r1.makeFieldSig("0-y-Main-int-"), 11); ajc$tjp_6 = r1.makeSJP("field-get", r1.makeFieldSig("19-out-java.lang.System-java.io.PrintStream-"), 12); ajc$tjp_7 = r1.makeSJP("method-call", r1.makeMethodSig("1-println-java.io.PrintStream-java.lang.String:-arg0:--void-"), 12); ajc$tjp_8 = r1.makeSJP("method-execution", r1.makeMethodSig("9-main-Main-[Ljava.lang.String;:-args:--void-"), 10); ajc$tjp_9 = r1.makeSJP("initialization", r1.makeConstructorSig("1--Main----"), 6); try { Aspect.aspectOf().ajc$before$Aspect$1$36f01b1c(ajc$tjp_1); $i0 = 13; try { Aspect.aspectOf().ajc$before$Aspect$1$36f01b1c(ajc$tjp_0); x = $i0; } catch (Throwable $r30) { /*** LJH insert here *****/ if ($r30 instanceof ExceptionInIntializerError) //*** throw($r30); //*** /***********/ r31 = $r30; Aspect.aspectOf().ajc$after$Aspect$2$36f01b1c(ajc$tjp_0); throw r31; } Aspect.aspectOf().ajc$after$Aspect$2$36f01b1c(ajc$tjp_0); } catch (Throwable $r36) { /*** LJH insert here *****/ if ($r36 instanceof ExceptionInIntializerError) //*** throw($r36); //*** /***********/ r2 = $r36; Aspect.aspectOf().ajc$after$Aspect$2$36f01b1c(ajc$tjp_1); throw r2; } Aspect.aspectOf().ajc$after$Aspect$2$36f01b1c(ajc$tjp_1); }

Files changed in fix:
tests/bugs/pr62642.java tests/src/org/aspectj/systemtest/ajc121/Ajc121Tests.java weaver/src/org/aspectj/weaver/bcel/BcelShadow.java