KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > emn > info > eaop > instrumentation > SelectiveInstrumentation


1 /* ----------------------------------------------------------------------------
2  * EAOP 1.0, 2002-12-19
3  * (c) 2002 Remi Douence, Mario Sudholt; OBASCO group; EMN/INRIA; France
4  * THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY WARRANTY
5   -------------------------------------------------------------------------- */

6
7 package fr.emn.info.eaop.instrumentation;
8
9 import fr.emn.info.eaop.*;
10
11 import recoder.abstraction.*;
12 import recoder.java.*;
13 import recoder.java.declaration.*;
14 import recoder.list.*;
15
16 /**
17  * Class enabling the selective instrumentation of source code (base and
18  * aspects) with event generation statements and calls to the monitor.
19  *
20  * This class provides three functionalities:
21  * <ol>
22  * <li> Restriction of the instrumentation to specific classes
23  * (method <tt>checkClass</tt>), and methods or constructors
24  * (<tt>checkMethodOrConstructor</tt>). <br>
25  * Attention: there are similar but different class-related and
26  * method-related predicates (e.g. <code>matchClassName</code> vs.
27  * <code>matchMethodClassName</code>).
28  * <li> Definition of aspects (as defined by <tt>checkAspect</tt>)
29  * which are to be instrumented.
30  * <li> Definition of the set of user-defined files (field <tt>files</tt>)
31  * constituting the EAOP application.
32  * </ol>
33  *
34  * An EAOP application may use these functionalities by defining a class
35  * inheriting from this class, defining <tt>fields</tt> and redefining the
36  * methods provided as needed using the predicates provided.
37  *
38  * @author MS
39  * @version 1.0
40  */

41 public class SelectiveInstrumentation {
42     protected TypeDeclaration td = null;
43     protected MethodDeclaration md = null;
44     public String JavaDoc[] files;
45
46     // -----------------------------------------------------------------
47
// customizable methods for the control of instrumentation
48
// -----------------------------------------------------------------
49

50     public SelectiveInstrumentation() {}
51
52     /**
53      * Predicate discriminating classes to be instrumented.
54      */

55     protected boolean checkClass(TypeDeclaration td) {
56     this.td = td;
57     
58     return true;
59     }
60
61     /**
62      * Predicate discriminating methods and constructors to be instrumented.
63      */

64     protected boolean checkMethodOrConstructor(MethodDeclaration md) {
65     this.md = md;
66     
67     return true;
68     }
69
70     /**
71      * Predicate discriminating aspects which are visible by other aspects.
72      */

73     protected boolean checkAspect(TypeDeclaration td) {
74     this.td = td;
75
76     return matchClassName("Billing");
77     }
78
79
80     // -----------------------------------------------------------------
81
// class-related predicates
82
// -----------------------------------------------------------------
83

84     protected boolean matchClassName(String JavaDoc regexp) {
85     return td.getName().matches(regexp);
86     }
87
88     protected boolean matchFieldNames(String JavaDoc regexp) {
89     FieldList fs = td.getFields();
90     String JavaDoc fsStr = "";
91     String JavaDoc fStr;
92
93     for (int i=0; i<fs.size(); i++) {
94         if (fsStr != "") fsStr += ",";
95         fsStr += fs.getField(i).getName();
96     }
97
98     return fsStr.matches(regexp);
99     }
100
101     protected boolean matchMethodNames(String JavaDoc regexp) {
102     MethodList ms = td.getMethods();
103     String JavaDoc msStr = "";
104     String JavaDoc mStr;
105
106     for (int i=0; i<ms.size(); i++) {
107         if (msStr != "") msStr += ",";
108         msStr += ms.getMethod(i).getName();
109     }
110
111     return msStr.matches(regexp);
112     }
113
114     protected boolean matchSupertypeNames(String JavaDoc regexp) {
115     ClassTypeList ss = td.getAllSupertypes();
116     String JavaDoc ssStr = "";
117     String JavaDoc sStr;
118
119     if (ss == null) return false;
120
121     for (int i=0; i<ss.size(); i++) {
122         if (ssStr != "") ssStr += ",";
123         ssStr += ss.getClassType(i).getName();
124     }
125
126     return ssStr.matches(regexp);
127     }
128
129     protected boolean matchSupertypeNames(ClassTypeList cts, String JavaDoc regexp) {
130     ClassTypeList ss = cts;
131     String JavaDoc ssStr = "";
132     String JavaDoc sStr;
133
134     if (ss == null) return false;
135
136     for (int i=0; i<ss.size(); i++) {
137         if (ssStr != "") ssStr += ",";
138         ssStr += ss.getClassType(i).getName();
139     }
140
141     return ssStr.matches(regexp);
142     }
143
144     protected boolean matchPackage(String JavaDoc regexp) {
145     return td.getPackage().getName().matches(regexp);
146     }
147
148
149     // -----------------------------------------------------------------
150
// aspect-related predicates
151
// -----------------------------------------------------------------
152

153     protected boolean isAspectInstance() {
154     return matchSupertypeNames(".*,Aspect,.*");
155     }
156
157
158     // -----------------------------------------------------------------
159
// method-related predicates
160
// -----------------------------------------------------------------
161

162     protected boolean matchMethodClassName(String JavaDoc regexp) {
163     return
164         Main.getSourceInfo()
165         .getContainingClassType((ProgramElement) md).getName()
166         .matches(regexp);
167     }
168
169     protected boolean matchMethodName(String JavaDoc regexp) {
170     return md.getName().matches(regexp);
171     }
172
173     protected boolean matchMethodPackage(String JavaDoc regexp) {
174     return md.getPackage().getName().matches(regexp);
175     }
176
177     protected boolean matchParameterTypes(String JavaDoc regexp) {
178     ParameterDeclarationMutableList ps = md.getParameters();
179     String JavaDoc psStr = "";
180     String JavaDoc tStr;
181     
182     if (ps.size() == 0) psStr = "void";
183     else
184         for (int i=0; i<ps.size(); i++) {
185         if (psStr != "") psStr += ",";
186         tStr = ps.getParameterDeclaration(i)
187             .getVariableSpecification().getType().getName();
188         if (tStr == "") psStr += "void";
189         else psStr += tStr;
190         }
191
192     return psStr.matches(regexp);
193     }
194
195     protected boolean matchReturnType(String JavaDoc regexp) {
196     Type t = md.getReturnType();
197     if (t == null) return "".matches(regexp) || "void".matches(regexp);
198     return t.getName().matches(regexp);
199     }
200
201     protected boolean isSystemMethodOrConstructor() {
202     return matchParameterTypes(
203          ".*" + Instrumentation.constructorDummyTypeStr + ".*")
204         || matchMethodName("definition")
205         || matchMethodName("next.*");
206     }
207
208     // ------------------------------------------------------------
209
// getter for files field
210
// ------------------------------------------------------------
211

212     public String JavaDoc[] getFiles() {
213     return files;
214     }
215 }
216
217
Popular Tags