KickJava   Java API By Example, From Geeks To Geeks.

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


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: GetCut.java,v 1.3 2003/11/07 22:34:57 anicoara Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose.crosscut;
28
29 // used packages
30
import java.lang.reflect.Field JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 import ch.ethz.jvmai.ExceptionJoinPoint;
34 import ch.ethz.jvmai.FieldAccessJoinPoint;
35 import ch.ethz.jvmai.FieldModificationJoinPoint;
36 import ch.ethz.jvmai.JoinPoint;
37 import ch.ethz.jvmai.MethodEntryJoinPoint;
38 import ch.ethz.jvmai.MethodExitJoinPoint;
39 import ch.ethz.prose.engine.FieldAccessRequest;
40
41
42 /**
43  * Class GetCut is a crosscut which cuts field gets.
44  *
45  *
46  *
47  * The advice method is <code>GET_ARGS</code>. The join-point
48  * where the advice method is executted depend on:
49  * <ol>
50  * <li> the signature of <code>GET_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.3 $
60  * @author Andrei Popovici
61  */

62 public abstract class GetCut extends AbstractCrosscut {
63       private static class SetGetArgs
64     {
65     Object JavaDoc[] cc;
66     Object JavaDoc[] cw;
67     Object JavaDoc[] wc;
68     Object JavaDoc[] ww;
69     SetGetArgs()
70     {
71         cc = new Object JavaDoc[2];
72         cw = new Object JavaDoc[2]; cw[1]=new ANY();
73         wc = new Object JavaDoc[2]; wc[0]=new ANY();
74         ww = new Object JavaDoc[2]; ww[0]=new ANY(); ww[1]=new ANY();
75     }
76     }
77
78
79     private ThreadLocal JavaDoc threadLocalArgs;
80
81   /** This method must be defined by subclasses. It will be called
82    * each time one of the selected fields is
83    *
84    */

85     protected void GET_ARGS(){}
86
87
88     private transient SetGetSignaturePattern advicePattern;
89
90
91     static class SetGetThreadLocal extends ThreadLocal JavaDoc implements java.io.Serializable JavaDoc
92     {
93     protected Object JavaDoc initialValue()
94         {
95             return new SetGetArgs();
96         }
97     };
98     protected GetCut()
99     {
100     threadLocalArgs = new SetGetThreadLocal();
101     initState();
102     }
103
104     private void initState()
105     {
106     if (advicePattern == null)
107       {
108         advicePattern = new SetGetSignaturePattern(this);
109         toStringSignature = advicePattern.toString();
110       }
111     }
112
113   String JavaDoc toStringSignature = null;
114   public void insertionAction(boolean isBeforeInsertion)
115     {
116       if (isBeforeInsertion)
117     initState();
118     }
119
120   protected void joinPointAction(FieldAccessJoinPoint ev)
121     {
122     Object JavaDoc[] args = null;
123     SetGetArgs setGetArgs=(SetGetArgs)threadLocalArgs.get();
124
125     switch (advicePattern.signatureCathegory)
126         {
127         case SignaturePattern.SIGNATURE__EMPTY: //GET_ARGS(); //BUGFIX: the advice method is executed twice
128
break;
129         case SignaturePattern.SIGNATURE__CONCRETE__CONCRETE:
130         args = setGetArgs.cc;
131         args[0]= ev.getTarget();
132         args[1] = ev.getValue();
133         break;
134         case SignaturePattern.SIGNATURE__WILDCARD__CONCRETE:
135         args=setGetArgs.wc;
136         ((ANY)args[0]).setObject(ev.getTarget());
137         args[1]=ev.getValue();
138         break;
139         case SignaturePattern.SIGNATURE__CONCRETE__WILDCARD:
140         args=setGetArgs.cw;
141         args[0]=ev.getTarget();
142         ((ANY)args[1]).setObject(ev.getValue());
143         break;
144         case SignaturePattern.SIGNATURE__WILDCARD__WILDCARD:
145         args=setGetArgs.ww;
146         ((ANY)args[0]).setObject(ev.getTarget());
147         ((ANY)args[1]).setObject(ev.getValue());
148         break;
149         default:
150         throw new Error JavaDoc("Wrong pattern signature in " + this);
151         }
152     try
153         {
154         advicePattern.methodObj.invoke(this,args);
155         }
156     catch (IllegalAccessException JavaDoc e)
157         {
158         throw new RuntimeException JavaDoc();
159         }
160     catch (java.lang.reflect.InvocationTargetException JavaDoc e)
161         {
162         throw new RuntimeException JavaDoc();
163         }
164
165     }
166
167
168   /**
169    * Create a new CrosscutRequest consisting of FieldAccessRequests
170    * and FieldModificationRequests for every field (public, protected,
171    * private or package-scoped) declared in class <code>c</code>. (No
172    * request is generated for inherited fields!)
173    */

174   protected CrosscutRequest doCreateRequest(Class JavaDoc c)
175     throws SecurityException JavaDoc
176   {
177     // retrieve all fields of this class and install
178
// a field access and a field modification watch.
179
Field JavaDoc[] declaredFields = null;
180     CrosscutRequest result = new CrosscutRequest();
181
182     try { declaredFields = c.getDeclaredFields(); }
183     catch (NoClassDefFoundError JavaDoc e)
184       {
185     return result;
186       }
187     for ( int i = 0; i < declaredFields.length; i++ )
188     {
189         if (advicePattern.matchesOperationOnField(declaredFields[i]))
190         result.add(requestFactory.createJoinPointRequest(FieldAccessJoinPoint.KIND,declaredFields[i]));
191     }
192
193     return result;
194   }
195
196   /**
197    *
198    */

199   public String JavaDoc toString()
200     {
201       return " Crosscut: 'GetCut' \n" +
202          " Advice:" + toStringSignature + "\n" +
203          " PointCutter: " + getSpecializer() + "\n";
204     }
205 }
206
207
208 //======================================================================
209
//
210
// $Log: GetCut.java,v $
211
// Revision 1.3 2003/11/07 22:34:57 anicoara
212
// GetCut bugfix: the advice method has been executed twice
213
//
214
// Revision 1.2 2003/07/11 12:27:43 apopovic
215
// Bug fix for rare cases. Some classed loaded late may throw a 'NoClassDefError' when
216
// trying to retrieve the declared methods; The crosscut for such cases is now an empty listt
217
//
218
// Revision 1.1.1.1 2003/07/02 15:30:51 apopovic
219
// Imported from ETH Zurich
220
//
221
// Revision 1.3 2003/05/26 13:28:52 popovici
222
// Documentation Improvements
223
//
224
// Revision 1.2 2003/05/25 11:46:44 popovici
225
// Improved 'toString' presentation of aspects
226
//
227
// Revision 1.1 2003/05/05 13:58:18 popovici
228
// renaming from runes to prose
229
//
230
// Revision 1.13 2003/04/29 17:34:46 pfalcari
231
// Serialization problem fixed
232
//
233
// Revision 1.12 2003/04/26 18:51:33 popovici
234
// 1 Bug fix which lead to a refactoring step:
235
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
236
// now this list belongs to the JoinPointManager;
237
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
238
//
239
// Revision 1.11 2003/04/17 12:49:25 popovici
240
// Refactoring of the crosscut package
241
// ExceptionCut renamed to ThrowCut
242
// McutSignature is now SignaturePattern
243
//
244
// Revision 1.10 2003/04/17 08:47:20 popovici
245
// Important functionality additions
246
// - Cflow specializers
247
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
248
// - Transactional capabilities
249
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
250
// between static and dynamic specializers.
251
// - Functionality pulled up in abstract classes
252
// - Uniformization of advice methods patterns and names
253
//
254
// Revision 1.9 2003/04/07 13:17:26 popovici
255
// Bug fix in serialization: Local hash maps were not static.
256
// > Deserialized join-points used to hit on a null when looking for their 'threadLocalJoinPoint'
257
//
258
// Revision 1.8 2003/03/04 18:36:34 popovici
259
// Organization of imprts
260
//
261
// Revision 1.7 2003/03/04 11:27:18 popovici
262
// Important refactorization step (march):
263
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
264
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
265
// structures
266
//
267
// Revision 1.6 2003/01/27 12:49:04 pschoch
268
// new toString() implementation
269
//
270
// Revision 1.5 2002/10/31 18:26:50 pschoch
271
// Capability of crosscutting Exceptions added to prose.
272
//
273
// Revision 1.4 2002/10/25 07:42:34 popovici
274
// Undo Chnages Phillippe
275
//
276
// Revision 1.2 2002/06/07 15:30:51 popovici
277
// Documentation updates of FunctionalCrosscut/ClasseS refactorings
278
//
279
// Revision 1.1 2002/06/05 09:27:59 popovici
280
// thisJoinPoint() introduced in Abstract Crosscut and subclasses;
281
//
282
// Revision 1.5 2002/03/28 13:48:41 popovici
283
// Mozilla-ified
284
//
285
// Revision 1.4 2002/03/06 13:48:37 popovici
286
// joinPointAction now in 4 flavours, depending on the join point type
287
//
288
// Revision 1.3 2002/02/21 12:39:20 popovici
289
// Crosscut efficiency issues:
290
// - joinPointAction dispatch based on static optimization
291
// (->doesEventSpecialization, 'setSpecializer' modified)
292
// (->calculation of 'adviceMethodOptimization', in Func.Crossc)
293
// - joinPointAction now uses JoinPoints (not Events)
294
// - JoinPointListeners (including crosscuts) now use joinPointReached(XXXJoinPoint) to avoid casting
295
// Crosscut architectural issues:
296
// - AbstractCrosscut now insertable.
297
// - AbstractCrosscuts owns referecnces to JVMAI
298
//
299
// Revision 1.2 2002/02/05 09:47:12 smarkwal
300
// JVMDI-specific code replaced by JVMAI. Prose-implementation classes and reflection package removed.
301
//
302
// Revision 1.1.1.1 2001/11/29 18:13:17 popovici
303
// Sources from runes
304
//
305
// Revision 1.1.2.1 2000/11/28 16:39:38 groos
306
// Initial revision.
307
//
308
Popular Tags