KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > Aspect


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: Aspect.java,v 1.1.1.1 2003/07/02 15:30:50 apopovic Exp $
22
// =====================================================================
23
//
24
// (history at end)
25
//
26

27 package ch.ethz.prose;
28 import java.io.Serializable JavaDoc;
29 import java.util.List JavaDoc;
30
31 import ch.ethz.prose.crosscut.Crosscut;
32
33
34 /** Class aspect is the
35  *
36  */

37 public
38 abstract class Aspect implements Insertable,Serializable JavaDoc {
39
40   /** This method is called <em>before and after</em> this
41    * aspect is inserted into an <code>AspectManager</code>.
42    * By overriding the default (which is a no-op), one can
43    * define setup actions. If this method throws an exception,
44    * the aspect insertion fails.
45    */

46   public void insertionAction(boolean isBefore) {}
47
48
49   /** this method is called before and after the withdrawal of an aspect.
50    */

51   public void withdrawalAction(boolean isBefore){}
52
53
54   /** Aspects are associated to objects. This method generates a 'random'
55    * object.
56    */

57   public static Object JavaDoc generateUniqueAssociation()
58     {
59       return "" + System.currentTimeMillis() + birthDayCount++;
60     }
61
62   protected static long birthDayCount = 0;
63   private List JavaDoc ownedCrosscutList= null;
64   protected Object JavaDoc aspectID = null;
65   private boolean aspectIDisSet = false;
66   protected int priority = 0;
67
68   /** Associate aspect with a given object. Note that
69    * the associated has in PROSE the same role as the 'perObject', 'perVM',
70    * etc. role in other languages and frameworks. For one given object,
71    * just ONE aspect instance may be inserted in PROSE at a given time.
72    * With this, if you specify the name "foo" as an associated object, any insertion
73    * of a second aspect associated to "foo" results in an exception.
74    * In other words, a unique name means a 'per VM' aspect. However,
75    * you may associate aspects to the elements o1..oN of a vector
76    * of your choice. In this case, it will be a 'per Object' aspect.
77    * An aspect not explicitely associated to an id is associated to a random
78    * object.
79    */

80   public synchronized void associateTo(Object JavaDoc id)
81     {
82       if (aspectIDisSet)
83     throw new IllegalStateException JavaDoc("reset of IDs not allowed in aspects");
84
85       if (id == null)
86     throw new IllegalArgumentException JavaDoc("null ids not accepted as arguments");
87
88       this.aspectIDisSet = true;
89       this.aspectID = id;
90     }
91
92   /** Create an aspect with an automatically generated association. Note that
93    * the aspects associations have in PROSE the same role as the 'perObject', 'perVM',
94    * etc. role in other languages and frameworks.
95    */

96   public Aspect()
97     {
98       aspectID = generateUniqueAssociation();
99     }
100
101     /** Return a list of crosscuts owned by this Aspect. Note that this
102      * method returns the same list of objects during
103      * the existence of asspect. This list is initialized using the
104      * <code>crosscuts</code> template method.
105      *
106      */

107     public List JavaDoc getCrosscuts()
108     {
109     if (ownedCrosscutList != null)
110         return ownedCrosscutList;
111
112
113     Crosscut[] ownedCrosscuts = crosscuts();
114     ownedCrosscutList = new java.util.Vector JavaDoc();
115
116     for (int i = 0; i < ownedCrosscuts.length; i++)
117         {
118         ownedCrosscuts[i].setOwner(this);
119         ownedCrosscutList.add(ownedCrosscuts[i]);
120         }
121
122     return ownedCrosscutList;
123     }
124
125
126
127   /** Get the priority of this aspect. Advices (indireclty) belonging
128    * to this aspect are executed from low priority values to
129    * high prioirity values. The default priority is 0.
130    *
131    */

132   public int getPriority()
133     {
134       return priority;
135     }
136
137
138   /** Set the priority of this aspect. Advices (indireclty) belonging
139    * to this aspect are executed from low priority values to
140    * high prioirity values. The default priority is 0.
141    */

142   public void setPriority(int prio)
143     {
144       this.priority = prio;
145     }
146
147
148
149   /** Return the object associated to this aspect */
150   public Object JavaDoc getAssociatedObject()
151     {
152       return aspectID;
153     }
154
155   /** This is a template meethod. Users must define
156    * this method to return a list of crosscut objects
157    * that belong to this aspect
158    */

159   protected abstract Crosscut[] crosscuts();
160
161   /**
162    * Indicates whether some other object is "equal to" this one. The
163    * result is <code>true</code> if and only if <code>obj</code> is
164    * not <code>null</code> and is a instance of
165    * <code>DefaultAspect</code> and has equal contents as this
166    * object.
167    * @param obj other object with which to compare
168    * @return <code>true</code> iff <code>obj</code> is equal to this
169    * <code>DefaultAspect</code>
170    */

171   public boolean equals(Object JavaDoc obj)
172     {
173       return
174     obj instanceof Aspect &&
175     aspectID.equals( ((Aspect) obj).aspectID);
176     }
177
178   /**
179    * Returns a hashcode for this object.
180    * @return hashcode value
181    */

182   public int hashCode()
183     {
184       return (int)aspectID.hashCode();
185     }
186
187
188   public String JavaDoc toString()
189     {
190       return "Aspect [" + aspectID + "]";
191     }
192
193 }
194
195
196 //======================================================================
197
//
198
// $Log: Aspect.java,v $
199
// Revision 1.1.1.1 2003/07/02 15:30:50 apopovic
200
// Imported from ETH Zurich
201
//
202
// Revision 1.6 2003/06/07 15:33:54 popovici
203
// Compile bug fix: getCrosscuts couldnt have been final
204
//
205
// Revision 1.5 2003/05/26 13:28:48 popovici
206
// Documentation Improvements
207
//
208
// Revision 1.4 2003/05/25 11:44:20 popovici
209
// default association now over the new method 'generateUniqueAssociation'
210
//
211
// Revision 1.3 2003/05/20 16:05:01 popovici
212
//
213
// New QueryManager replaces functionality in AspectManager (better Soc)
214
// New 'Surrogate' classes for usage in the QueryManager
215
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
216
//
217
// Revision 1.2 2003/05/06 15:13:34 popovici
218
// aspectId renamed to 'associated object'
219
//
220
// Revision 1.1 2003/05/05 13:58:29 popovici
221
// renaming from runes to prose
222
//
223
// Revision 1.4 2003/04/29 12:41:03 popovici
224
// Feature added:
225
// - the 'setPriority' in class insertable allows now Aspects and Crosscuts to have a priority.
226
// Notitification is done from low int priorities to high int priorities.
227
// - the 'setAspectID' introduced to replace constuctor; used to be cumberstone for subclasses
228
//
229
// Revision 1.3 2003/04/27 13:21:32 popovici
230
// Aspects now have identities. Equality is based
231
// either on an ad-hoc identity, or on a specific
232
// name given by the user
233
//
234
// Revision 1.2 2003/04/17 15:43:45 popovici
235
// crosscuts renamed to 'getCrosscuts'
236
// createCrosscuts renamed to 'crosscuts'
237
//
238
// Revision 1.1 2003/04/17 15:15:09 popovici
239
// Extension->Aspect renaming
240
//
241
// Revision 1.6 2003/04/17 12:49:41 popovici
242
// Refactoring of the crosscut package
243
// ExceptionCut renamed to ThrowCut
244
// McutSignature is now SignaturePattern
245
//
246
// Revision 1.5 2003/04/17 08:47:12 popovici
247
// Important functionality additions
248
// - Cflow specializers
249
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
250
// - Transactional capabilities
251
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
252
// between static and dynamic specializers.
253
// - Functionality pulled up in abstract classes
254
// - Uniformization of advice methods patterns and names
255
//
256
// Revision 1.4 2003/03/04 18:36:36 popovici
257
// Organization of imprts
258
//
259
// Revision 1.3 2002/03/28 13:48:33 popovici
260
// Mozilla-ified
261
//
262
// Revision 1.2 2002/02/21 12:36:47 popovici
263
// Interface 'Insertable' added. Extensions and crosscuts
264
// are now insertable objects.
265
//
266
// Revision 1.1.1.1 2001/11/29 18:13:15 popovici
267
// Sources from runes
268
//
269
// Revision 1.1.2.3 2001/02/07 11:47:01 popovici
270
// Abstract methods 'insertionAction' and 'withdrawalAction' added.
271
//
272
// Revision 1.1.2.2 2001/02/05 13:27:21 mrmuller
273
// needs to implement Serializable, so that Extensions can be passed by value
274
//
275
// Revision 1.1.2.1 2000/10/24 17:44:51 popovici
276
// Documentation added.
277
//
278
// Revision 1.1 2000/10/16 11:53:24 popovici
279
// Initial Revision
280
//
281
Popular Tags