KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > crosscut > CatchCut


1
2 package ch.ethz.prose.crosscut;
3
4 // used packages
5
import java.util.HashMap JavaDoc;
6
7 import ch.ethz.jvmai.ExceptionCatchJoinPoint;
8 import ch.ethz.jvmai.FieldAccessJoinPoint;
9 import ch.ethz.jvmai.FieldModificationJoinPoint;
10 import ch.ethz.jvmai.JoinPoint;
11 import ch.ethz.jvmai.MethodEntryJoinPoint;
12 import ch.ethz.jvmai.MethodExitJoinPoint;
13 import ch.ethz.prose.engine.ExceptionCatchRequest;
14
15 /**
16  * Class CatchCut represents a crosscut which cuts across <em>all</em>
17  * points belonging to <em>all</em> classes in the local VM where exceptions
18  * are catch.
19  *
20  * There are two ways of using this crosscut:
21  * <ol>
22  * <li> override the <em>convenience</em> methods <code>exceptionThrowAdvice</code>
23  * and <code>exceptionCatchAdvice</code> if you do not want to deal with the
24  * reflective part of join-points.
25  * <li> override the <code>joinPointAdvice</code> methods and define void-bodies
26  * for <code>exceptionThrowAdvice</code> and <code>exceptionCatchAdvice</code>.
27  * This second case is recomended only if you need the last bit of speed out
28  * of the system.
29  * </ol>
30  *
31  * @version $Revision: 1.1.1.1 $
32  * @author Philippe Schoch
33  */

34 public abstract class CatchCut extends AbstractCrosscut implements java.io.Serializable JavaDoc {
35
36     private transient CatchHandleSignaturePattern advicePattern;
37
38     // Caching variable for Class Object Throwable
39
private static Class JavaDoc throwableClass;
40
41
42
43     /**
44      * Constructor
45      */

46     protected CatchCut()
47     {
48     initState();
49     }
50
51     public void insertionAction(boolean isBeforeInsertion)
52     {
53     if (isBeforeInsertion)
54         initState();
55     }
56
57     private void initState()
58     {
59       if (advicePattern == null)
60     {
61       advicePattern= new CatchHandleSignaturePattern(this);
62     }
63     }
64     
65
66     /** This method must be defined by subclasses. It will be called
67      * each time one of the selected exceptions is catch.
68      */

69     public void CATCH_ARGS(){}
70
71
72     /** Advice action of Exceptions*/
73     protected void joinPointAction(ExceptionCatchJoinPoint ecjp)
74     throws IllegalAccessException JavaDoc
75     {
76
77     try
78         {
79         switch(advicePattern.signatureCathegory&(SignaturePattern.WILDCARD_1|SignaturePattern.CONCRETE_1|SignaturePattern.SIGNATURE__EMPTY))
80             {
81             case SignaturePattern.SIGNATURE__EMPTY:
82             CATCH_ARGS();
83             break;
84             case SignaturePattern.WILDCARD_1:
85             ANY x = new ANY();
86             x.setObject(ecjp.getException());
87             advicePattern.methodObj.invoke(this,new Object JavaDoc[]{x});
88             break;
89             case SignaturePattern.CONCRETE_1:
90             advicePattern.methodObj.invoke(this,new Object JavaDoc[]{ecjp.getException()});
91             break;
92             default:
93             throw new Error JavaDoc("CatchCut.joinPointAction: illegal signature pattern");
94             }
95         }
96     catch (java.lang.reflect.InvocationTargetException JavaDoc e)
97         {
98         throw new Error JavaDoc("CatchCut.joinPointAction: wrong arguments; static check failure:" + e);
99         }
100
101
102     }
103
104
105
106   /**
107    * Only a class of supertype 'Throwable' is a potential Exception Catch Crosscut Class
108    */

109   protected boolean isPotentialCrosscutClass(Class JavaDoc c)
110     {
111       return Throwable JavaDoc.class.isAssignableFrom(c);
112     }
113
114
115   /**
116    * Create a new CrosscutRequest consisting of ExceptionCatchRequests
117    * for each catch exception in each method declared in class <code>cls</code>.
118    * Only one ExceptionCatchRequest per exception class is
119    * generated for a specific CatchCut.
120    */

121   protected CrosscutRequest doCreateRequest(Class JavaDoc cls)
122     {
123       CrosscutRequest result = new CrosscutRequest();
124       if( (advicePattern.signatureCathegory&(SignaturePattern.CONCRETE_1)) != 0
125       &&
126       (!advicePattern.signature[0].isAssignableFrom(cls)))
127       return result;
128
129       result.add(requestFactory.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,cls));
130
131       return result;
132     }
133
134   /**
135    *
136    */

137   public String JavaDoc toString()
138     {
139       return "Crosscut 'CatchCut' [class '" + getClass().getName() +
140             "'] specialized with [" + getSpecializer() + "]";
141     }
142
143 }
144
Popular Tags