]> source.dussan.org Git - aspectj.git/commitdiff
fix and tests for checked exception on advice being checked at
authorjhugunin <jhugunin>
Wed, 12 Mar 2003 19:51:43 +0000 (19:51 +0000)
committerjhugunin <jhugunin>
Wed, 12 Mar 2003 19:51:43 +0000 (19:51 +0000)
woven join points

14 files changed:
tests/ajcTests.xml
tests/ajcTestsFailing.xml
tests/jimTests.xml
tests/new/AdviceThrowsCf.java
weaver/src/org/aspectj/weaver/Checker.java
weaver/src/org/aspectj/weaver/Shadow.java
weaver/src/org/aspectj/weaver/ShadowMunger.java
weaver/src/org/aspectj/weaver/TypeX.java
weaver/src/org/aspectj/weaver/bcel/BcelAdvice.java
weaver/testdata/dummyAspect.jar
weaver/testdata/megatrace.jar
weaver/testdata/megatrace0easy.jar
weaver/testdata/megatraceNoweave.jar
weaver/testdata/tracing.jar

index 31f5a7607b019f1deba64038cd8d380f504e4162..b8486e3b59c2880aed96639e759c7bfc60395937 100644 (file)
         <compile files="CflowCycles.java"/>
         <run class="CflowCycles"/>
     </ajc-test>
+    
+    <ajc-test dir="new"
+      title="incompatible advice throws clause are a compile-time error"
+      keywords="from-resolved_10x">
+        <compile files="AdviceThrowsCf.java">
+            <message kind="error" line="13"/>
+            <message kind="error" line="28"/>
+            <message kind="error" line="47"/>
+            <message kind="error" line="48"/>
+            <message kind="error" line="50"/>
+            
+            <message kind="error" line="70"/>
+            <message kind="error" line="74"/>
+            <message kind="error" line="76"/>
+            <message kind="error" line="78"/>
+            
+            <message kind="error" line="85"/>
+        </compile>
+    </ajc-test>
 </suite>
index 5964c4aeec5a266b316bbc692d41ae89f7ca885e..578f0aa7529b915fa7d5a035359ffecab3d7a5ed 100644 (file)
@@ -2,18 +2,6 @@
 <!DOCTYPE suite SYSTEM "../tests/ajcTestSuite.dtd">
 
 <suite>
-
-    <ajc-test dir="new"
-      title="incompatible advice throws clause are a compile-time error"
-      keywords="from-resolved_10x">
-        <compile files="AdviceThrowsCf.java">
-            <message kind="error" line="70"/>
-            <message kind="error" line="74"/>
-            <message kind="error" line="76"/>
-            <message kind="error" line="78"/>
-        </compile>
-    </ajc-test>
-
     <ajc-test dir="new" pr="660" title="illegal name binding in around cflow"
       keywords="from-resolved_104">
         <compile files="ArgsInCflowCf.java">
index e55e54ff7f975009d4f65b1bff775ea52e6faa8c..f4adb0f97b4169c97f1ff3bb7378fea2527ed510 100644 (file)
@@ -1,14 +1,15 @@
 <!DOCTYPE suite SYSTEM "../tests/ajcTestSuite.dtd">
 <suite>
-    <ajc-test dir="new" pr="29934"
-      title="can't apply around advice to the execution of around advice"
-      keywords="from-resolved_10x">
-        <compile files="CflowCycles.java"/>
-        <run class="CflowCycles"/>
-    </ajc-test>
+
+
+
 
     <!--
     
-
+    <ajc-test dir="new" pr="885"
+      title="source locations within expressions">
+        <compile files="SourceLocationWithinExpr.java" options="-1.4"/>
+        <run class="SourceLocationWithinExpr"/>
+    </ajc-test>
        -->
 </suite>
\ No newline at end of file
index f8564d97915cc32b7803a984458c9c0047347ce2..279bd2d55a2d251db7bfdc9afe70f9646c8152f9 100644 (file)
@@ -77,4 +77,17 @@ aspect A {
     }
     before() throws CheckedExc: staticinitialization(C) { //ERR: can't throw
     }
+    
+    void around() throws CheckedExc: canThrowChecked() {
+       proceed();
+    }
+    
+    void around() throws CheckedExc: canThrowUnchecked() {  // ERR: can't throw
+       proceed();
+    }
+    
+    void around() throws UncheckedExc: canThrowUnchecked() || set(int C.x) { 
+       proceed();
+    }
+
 }
index ab4541de371bd3d1b19063b27e067d9e2b3233cb..69285c37f3c2dbfb9a56733b2eaa52acfc117ca3 100644 (file)
@@ -13,6 +13,9 @@
 
 package org.aspectj.weaver;
 
+import java.util.Collection;
+import java.util.Collections;
+
 import org.aspectj.bridge.IMessage;
 import org.aspectj.bridge.Message;
 import org.aspectj.weaver.patterns.DeclareErrorOrWarning;
@@ -58,5 +61,7 @@ public class Checker extends ShadowMunger {
        public int compareTo(Object other) {
                return 0;
        }
+       
+       public Collection getThrownExceptions() { return Collections.EMPTY_LIST; }
 
 }
index 9a3f7908205498736a9c192581ecd019400cfe6a..fb3fddb204ff6a101938dcaec4b7784a7d3c5fa6 100644 (file)
@@ -268,8 +268,42 @@ public abstract class Shadow {
                }               
        }
        
+       protected boolean checkMunger(ShadowMunger munger) {
+               for (Iterator i = munger.getThrownExceptions().iterator(); i.hasNext(); ) {
+                       if (!checkCanThrow(munger,  (ResolvedTypeX)i.next() )) return false;
+               }
+               return true;
+       }
+
+       protected boolean checkCanThrow(ShadowMunger munger, ResolvedTypeX resolvedTypeX) {
+               if (getKind() == ExceptionHandler) {
+                       //XXX much too lenient rules here, need to walk up exception handlers
+                       return true;
+               }
+               
+               if (!isDeclaredException(resolvedTypeX, getSignature())) {
+                       getIWorld().showMessage(IMessage.ERROR, "can't throw checked exception \'" + resolvedTypeX +
+                                                       "\' at this join point \'" + this +"\'", // from advice in \'" + munger. + "\'",
+                                                       getSourceLocation(), munger.getSourceLocation());
+               }
+               
+               return true;
+       }
+
+       private boolean isDeclaredException(
+               ResolvedTypeX resolvedTypeX,
+               Member member)
+       {
+               ResolvedTypeX[] excs = getIWorld().resolve(member.getExceptions(getIWorld()));
+               for (int i=0, len=excs.length; i < len; i++) {
+                       if (excs[i].isAssignableFrom(resolvedTypeX)) return true;
+               }
+               return false;
+       }
+       
+       
     public void addMunger(ShadowMunger munger) {
-       this.mungers.add(munger);
+       if (checkMunger(munger)) this.mungers.add(munger);
     }
  
     public final void implement() {
@@ -323,16 +357,4 @@ public abstract class Shadow {
     public String toString() {
         return getKind() + "(" + getSignature() + ")"; // + getSourceLines();
     }
-
-
-
-
-
-
-
-
-       // ---- type access methods
-  
-      
-
 }
index f6788f6daf5b61b567f9eaedeb24d40b02980f0a..a8cf1dd806626ae13185992b5083f1cc4b495fc2 100644 (file)
@@ -13,6 +13,8 @@
 
 package org.aspectj.weaver;
 
+import java.util.Collection;
+
 import org.aspectj.bridge.ISourceLocation;
 import org.aspectj.util.PartialOrder;
 import org.aspectj.weaver.patterns.PerClause;
@@ -88,4 +90,10 @@ public abstract class ShadowMunger implements PartialOrder.PartialComparable, IH
                return pointcut;
        }
 
+
+       /**
+        * @return a Collection of ResolvedTypeX for all checked exceptions that
+        *          might be thrown by this munger
+        */
+       public abstract Collection getThrownExceptions();
 }
index f5bab0db3dc55efb51e2c3b56221acc76e3125a6..313e2b81a612ad5f6ba99d584743400952c5801d 100644 (file)
@@ -541,6 +541,8 @@ public class TypeX {
     public static final TypeX   CLONEABLE    = forSignature("Ljava/lang/Cloneable;");
     public static final TypeX   SERIALIZABLE = forSignature("Ljava/io/Serializable;");
     public static final TypeX   THROWABLE    = forSignature("Ljava/lang/Throwable;");
+    public static final TypeX   RUNTIME_EXCEPTION    = forSignature("Ljava/lang/RuntimeException;");
+    public static final TypeX   ERROR    = forSignature("Ljava/lang/Error;");
     
     // ---- helpers
     
index 8aa4c37a2e85130060b59b2cdbe860bd1aabe3e2..a62bbd927412a2d6eb1d8fc6dd6c275a9f387b01 100644 (file)
 
 package org.aspectj.weaver.bcel;
 
-import org.apache.bcel.generic.InstructionFactory;
-import org.apache.bcel.generic.InstructionHandle;
-import org.apache.bcel.generic.InstructionList;
-import org.aspectj.weaver.Advice;
-import org.aspectj.weaver.AdviceKind;
-import org.aspectj.weaver.AjAttribute;
-import org.aspectj.weaver.BCException;
-import org.aspectj.weaver.ISourceContext;
-import org.aspectj.weaver.Member;
-import org.aspectj.weaver.ResolvedTypeX;
-import org.aspectj.weaver.Shadow;
-import org.aspectj.weaver.TypeX;
-import org.aspectj.weaver.WeaverStateKind;
-import org.aspectj.weaver.World;
+import java.util.*;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.bcel.generic.*;
+import org.aspectj.weaver.*;
 import org.aspectj.weaver.ast.Literal;
 import org.aspectj.weaver.ast.Test;
-import org.aspectj.weaver.patterns.ExactTypePattern;
-import org.aspectj.weaver.patterns.ExposedState;
-import org.aspectj.weaver.patterns.Pointcut;
+import org.aspectj.weaver.patterns.*;
 
 /**
  * Advice implemented for bcel.
@@ -53,13 +43,14 @@ public class BcelAdvice extends Advice {
                this.concreteAspect = concreteAspect;
        }
 
-       // !!! only used for testing
+       // !!! must only be used for testing
        public BcelAdvice(AdviceKind kind, Pointcut pointcut, Member signature,
                int extraArgumentFlags,
         int start, int end, ISourceContext sourceContext, ResolvedTypeX concreteAspect)
     {
                this(new AjAttribute.AdviceAttribute(kind, pointcut, extraArgumentFlags, start, end, sourceContext), 
                        pointcut, signature, concreteAspect);
+               thrownExceptions = Collections.EMPTY_LIST;  //!!! interaction with unit tests
        }
 
     // ---- implementations of ShadowMunger's methods
@@ -147,6 +138,46 @@ public class BcelAdvice extends Advice {
     }
 
     // ---- implementations
+       
+       private Collection collectCheckedExceptions(TypeX[] excs) {
+               if (excs == null || excs.length == 0) return Collections.EMPTY_LIST;
+               
+               Collection ret = new ArrayList();
+               World world = concreteAspect.getWorld();
+               ResolvedTypeX runtimeException = world.resolve(TypeX.RUNTIME_EXCEPTION);
+               ResolvedTypeX error = world.resolve(TypeX.ERROR);
+               
+               for (int i=0, len=excs.length; i < len; i++) {
+                       ResolvedTypeX t = world.resolve(excs[i]);
+                       if (!(runtimeException.isAssignableFrom(t) || error.isAssignableFrom(t))) {
+                               ret.add(t);
+                       }
+               }
+                       
+               return ret;
+       }
+
+       private Collection thrownExceptions = null;
+       public Collection getThrownExceptions() {
+               if (thrownExceptions == null) {
+                       //??? can we really lump in Around here, how does this interact with Throwable
+                       if (concreteAspect != null && concreteAspect.getWorld() != null && // null tests for test harness
+                                 (getKind().isAfter() || getKind() == AdviceKind.Before || getKind() == AdviceKind.Around))
+                       {
+                               World world = concreteAspect.getWorld();
+                               ResolvedMember m = world.resolve(signature);
+                               if (m == null) {
+                                       thrownExceptions = Collections.EMPTY_LIST;
+                               } else {
+                                       thrownExceptions = collectCheckedExceptions(m.getExceptions());
+                               }
+                       } else {
+                               thrownExceptions = Collections.EMPTY_LIST;
+                       }
+               }
+               return thrownExceptions;
+       }
+
 
     // only call me after prepare has been called
     public boolean hasDynamicTests() {
index fd37338f440f2102bff86a3c2938a2df5923d815..a1972891f64a49221d1b0af9d13b00f812a3a7d8 100644 (file)
Binary files a/weaver/testdata/dummyAspect.jar and b/weaver/testdata/dummyAspect.jar differ
index 13fde4cc1e561e88afa12ce3849d8b0227fda05c..a6c410d7b56b603535d4c61b75880ef4acca26b6 100644 (file)
Binary files a/weaver/testdata/megatrace.jar and b/weaver/testdata/megatrace.jar differ
index 47549443c3eb74c66056f4b0841c8a6f4b4d8bd4..5d0f4a90e8d8a8de383e359176e4a56b4375cea3 100644 (file)
Binary files a/weaver/testdata/megatrace0easy.jar and b/weaver/testdata/megatrace0easy.jar differ
index cb2a819f29019886a55621cdfead1f20a6bc896e..9cca726ce62486195b8b575c97c637522a0f6490 100644 (file)
Binary files a/weaver/testdata/megatraceNoweave.jar and b/weaver/testdata/megatraceNoweave.jar differ
index 6e178f867caa4c24b4b93daf4a4d559711ef8110..f288f3602b544951f5f94a461dd184ada1c1c629 100644 (file)
Binary files a/weaver/testdata/tracing.jar and b/weaver/testdata/tracing.jar differ