KickJava   Java API By Example, From Geeks To Geeks.

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


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
// $Id: ThrowCut.java,v 1.2 2003/07/29 17:04:14 anicoara Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose.crosscut;
28
29 // used packages
30
import java.util.HashMap JavaDoc;
31
32 import ch.ethz.jvmai.ExceptionJoinPoint;
33 import ch.ethz.jvmai.FieldAccessJoinPoint;
34 import ch.ethz.jvmai.FieldModificationJoinPoint;
35 import ch.ethz.jvmai.JoinPoint;
36 import ch.ethz.jvmai.MethodEntryJoinPoint;
37 import ch.ethz.jvmai.MethodExitJoinPoint;
38 import ch.ethz.prose.engine.ExceptionThrowRequest;
39
40 /**
41 * Class ThrowCut is a crosscut which cuts exception throws
42  *
43  *
44  *
45  * The advice method is <code>THROW_ARGS</code>. The
46  * only argument of THROW_ARGS is the thrown exception (or none).
47  * The join-point
48  * where the advice method is executted depend on:
49  * <ol>
50  * <li> the signature of <code>THROW_ARGS</code>
51  * <li> the value returned by <code>pointCutter</code>.
52  * </ol>
53  *
54  * Within an advice method, the <code>thisJoinPoint()</code> method
55  * can be called to obtain a reference to the currently executing
56  * join-point.
57  *
58  *
59  * @version $Revision: 1.2 $
60  * @author Andrei Popovici
61  */

62 public abstract class ThrowCut extends AbstractCrosscut implements java.io.Serializable JavaDoc{
63
64     private transient ThrowHandleSignaturePattern advicePattern;
65
66     // Caching variable for Class Object Throwable
67
private static Class JavaDoc throwableClass;
68
69
70     /**
71      * Constructor
72      */

73     protected ThrowCut()
74     {
75     initState();
76     }
77     
78     public void insertionAction(boolean isBeforeInsertion)
79     {
80     if(isBeforeInsertion)
81         initState();
82     }
83     
84     private void initState()
85     {
86     if (advicePattern == null)
87     {
88             advicePattern = new ThrowHandleSignaturePattern(this);
89     }
90     }
91
92
93     /** This method must be defined by subclasses. It will be called
94      * each time one of the selected exceptions is thrown.
95      */

96     public void THROW_ARGS(){}
97
98
99     /** Advice action of Exceptions*/
100     protected void joinPointAction(ExceptionJoinPoint etjp)
101     throws IllegalAccessException JavaDoc
102     {
103
104     try
105         {
106         switch(advicePattern.signatureCathegory&(SignaturePattern.WILDCARD_1|SignaturePattern.CONCRETE_1|SignaturePattern.SIGNATURE__EMPTY))
107             {
108             case SignaturePattern.SIGNATURE__EMPTY:
109             THROW_ARGS();
110             break;
111             case SignaturePattern.WILDCARD_1:
112             ANY x = new ANY();
113             x.setObject(etjp.getException());
114             advicePattern.methodObj.invoke(this,new Object JavaDoc[]{x});
115             break;
116             case SignaturePattern.CONCRETE_1:
117             advicePattern.methodObj.invoke(this,new Object JavaDoc[]{etjp.getException()});
118             break;
119             default:
120             throw new Error JavaDoc("ThrowCut.joinPointAction: illegal signature pattern");
121             }
122         }
123     catch (java.lang.reflect.InvocationTargetException JavaDoc e)
124         {
125         throw new Error JavaDoc("ThrowCut.joinPointAction: wrong arguments; static check failure:" + e);
126         }
127
128
129     }
130
131
132
133   /**
134    * Only a class of supertype 'Throwable' is a potential Exception Crosscut Class
135    */

136   protected boolean isPotentialCrosscutClass(Class JavaDoc c)
137     {
138       return Throwable JavaDoc.class.isAssignableFrom(c);
139     }
140
141
142   /**
143    * Create a new CrosscutRequest consisting of ExceptionThrowRequests
144    * for each thrown exception in each method declared in class <code>cls</code>.
145    * Only one ExceptionThrowRequest per exception class is
146    * generated for a specific ThrowCut.
147    */

148   protected CrosscutRequest doCreateRequest(Class JavaDoc cls)
149     {
150       CrosscutRequest result = new CrosscutRequest();
151       if( (advicePattern.signatureCathegory&(SignaturePattern.CONCRETE_1)) != 0
152       &&
153       (!advicePattern.signature[0].isAssignableFrom(cls)))
154       return result;
155
156       result.add(requestFactory.createJoinPointRequest(ExceptionJoinPoint.KIND,cls));
157
158       return result;
159     }
160
161   /**
162    *
163    */

164   public String JavaDoc toString()
165     {
166       return " Crosscut: 'ThrowCut' \n" +
167          " Advice:" + advicePattern.toString() + "\n" +
168          " PointCutter: " + getSpecializer() + "\n";
169      }
170
171
172 }
173
174 //======================================================================
175
//
176
// $Log: ThrowCut.java,v $
177
// Revision 1.2 2003/07/29 17:04:14 anicoara
178
// Bug fix: ThrowCut used to create problems during serialization (null pointer exception)
179
//
180
// Revision 1.1.1.1 2003/07/02 15:30:51 apopovic
181
// Imported from ETH Zurich
182
//
183
// Revision 1.4 2003/05/26 13:28:53 popovici
184
// Documentation Improvements
185
//
186
// Revision 1.3 2003/05/25 11:46:46 popovici
187
// Improved 'toString' presentation of aspects
188
//
189
// Revision 1.2 2003/05/06 15:51:31 popovici
190
// Mozilla-ification
191
//
192
// Revision 1.1 2003/05/05 13:58:09 popovici
193
// renaming from runes to prose
194
//
195
// Revision 1.2 2003/04/26 18:51:34 popovici
196
// 1 Bug fix which lead to a refactoring step:
197
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
198
// now this list belongs to the JoinPointManager;
199
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
200
//
201
// Revision 1.1 2003/04/17 12:49:20 popovici
202
// Refactoring of the crosscut package
203
// ExceptionCut renamed to ThrowCut
204
// McutSignature is now SignaturePattern
205
//
206
// Revision 1.6 2003/04/17 08:47:19 popovici
207
// Important functionality additions
208
// - Cflow specializers
209
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
210
// - Transactional capabilities
211
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
212
// between static and dynamic specializers.
213
// - Functionality pulled up in abstract classes
214
// - Uniformization of advice methods patterns and names
215
//
216
// Revision 1.5 2003/04/07 13:17:25 popovici
217
// Bug fix in serialization: Local hash maps were not static.
218
// > Deserialized join-points used to hit on a null when looking for their 'threadLocalJoinPoint'
219
//
220
// Revision 1.4 2003/03/04 18:36:34 popovici
221
// Organization of imprts
222
//
223
// Revision 1.3 2003/03/04 11:27:18 popovici
224
// Important refactorization step (march):
225
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
226
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
227
// structures
228
//
229
// Revision 1.2 2003/01/27 12:51:12 pschoch
230
// new toString() implementation; advice-Method now without parameter
231
//
232
// Revision 1.1 2002/10/31 18:26:52 pschoch
233
// Capability of crosscutting Exceptions added to prose.
234
//
235
//
236
Popular Tags