KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > emn > info > eaop > Main


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;
8
9 import fr.emn.info.eaop.instrumentation.*;
10
11 import java.io.*;
12 import java.util.*;
13
14 import recoder.*;
15 import recoder.abstraction.*;
16 import recoder.bytecode.*;
17 import recoder.convenience.*;
18 import recoder.list.*;
19 import recoder.io.*;
20 import recoder.java.*;
21 import recoder.java.declaration.*;
22 import recoder.kit.*;
23 import recoder.service.*;
24
25 /**
26  * Class interpreting runtime arguments, setting up the
27  * transformation system "Recoder" and performing the
28  * instrumentation transformations.
29  *
30  * @author MS
31  * @version 1.0
32  */

33 public class Main {
34     private static CrossReferenceServiceConfiguration sc = null;
35     private static SourceFileRepository sfr = null;
36     private static SourceInfo si = null;
37     private static String JavaDoc[] files = null;
38     private static CompilationUnit cu = null;
39
40     private final static String JavaDoc
41         eaopPathRexp = ".*fr.emn.info.eaop.*";
42
43     private final static String JavaDoc[] eaopSystemFiles = {
44         "aspect/Aspect.java",
45         "aspect/AspectS.java",
46         "aspect/Coroutining.java",
47         "aspect/Fst.java",
48         "aspect/Seq.java",
49         "aspect/Cond.java",
50         "aspect/Root.java",
51         "aspect/Any.java",
52         "aspect/BinaryAspectS.java",
53         "Monitor.java",
54         "event/Event.java",
55         "event/MethodCall.java",
56         "event/MethodReturn.java",
57         "event/ConstructorCall.java",
58         "event/ConstructorReturn.java",
59         "instrumentation/ConstructorDummyType.java"
60     };
61
62
63     private static String JavaDoc baseDir = "";
64     private static String JavaDoc exampleSrcDir = "";
65
66     /**
67      * Object controlling which classes and methods are to be
68      * instrumented and which aspects may be crosscut by other
69      * aspects.
70      */

71     public static void main(String JavaDoc[] args) {
72     exampleSrcDir = "examples/" + args[0] + "/src";
73
74     setup();
75     transform();
76     }
77
78     static void setup() {
79     sc = new CrossReferenceServiceConfiguration();
80     sfr = sc.getSourceFileRepository();
81     si = sc.getSourceInfo();
82
83     // intialize general project settings
84
ProjectSettings ps = sc.getProjectSettings();
85     ps.setProperty("output.path", exampleSrcDir + "/../exec");
86     if (!ps.ensureSystemClassesAreInPath())
87         IO.fail("Problem with system setup: Cannot locate system classes");
88     ps.ensureExtensionClassesAreInPath();
89
90         // initialize selective instrumentation
91
IO.initializeSelectiveInstrumentation();
92     files = IO.getInstrumentationContext().getFiles();
93     }
94
95     static void transform() {
96     ProblemReport report = null;
97     CompilationUnitList cus = null;
98     String JavaDoc fn;
99     int i;
100
101     try {
102         baseDir = new File(".").getCanonicalPath();
103         for (i = 0; i < eaopSystemFiles.length; i++) {
104         // construct/normalize (Unix, Windows) names of system files
105
fn = baseDir + "/fr/emn/info/eaop/" + eaopSystemFiles[i];
106         eaopSystemFiles[i] = new File(fn).getCanonicalPath();
107         }
108         // import system classes into Recoder program model
109
cus = sfr.getCompilationUnitsFromFiles(eaopSystemFiles);
110     } catch (Exception JavaDoc e) {
111         IO.fail("[transform] " + e.toString());
112     }
113
114     for (i = 0; i < files.length; i++) {
115         try {
116         // construct/normalize (Unix, Windows) names of user files
117
fn = baseDir + "/" + exampleSrcDir + "/" + files[i];
118         files[i] = new File(fn).getCanonicalPath();
119         // import user classes into Recoder program model
120
cu = sfr.getCompilationUnitFromFile(files[i]);
121         } catch (Exception JavaDoc e) {
122         IO.fail("[main] " + e.toString());
123         }
124     };
125
126     for (i = 0; i < files.length; i++) {
127         try {
128         cu = sfr.getCompilationUnitFromFile(files[i]);
129         } catch (ParserException p) {
130         IO.fail("[main] " + p.toString());
131         }
132
133         report = addImports();
134         
135         TypeDeclarationList tds = cu.getDeclarations();
136         
137         report = wrapConstructors(tds);
138         report = wrapMethods(tds);
139         report = insertEvents(tds);
140         
141         report = handleTermination(cu);
142         
143         output();
144     }
145     }
146
147     static ProblemReport addImports() {
148     Instrumentation transform = new AddImports(sc, cu);
149     return transform.execute();
150     }
151
152     /**
153      * Wrap constructors and separate structure creation from initialization
154      * of local fields.
155      *
156      * For a class <code>td</code> all user-defined superclasses
157      * of <code>td</code> wrapped.
158      */

159     static ProblemReport wrapConstructors(TypeDeclarationList tds) {
160     TypeDeclaration td;
161     ProblemReport report = null;
162     LinkedList transformedClasses = new LinkedList();
163
164     for (int j=0; j<tds.size(); j++) {
165         td = tds.getTypeDeclaration(j);
166         if (td.getName().equals("Base")) continue;
167         ClassTypeList cl = si.getAllSupertypes(td);
168         for (int k=cl.size()-1; k>=0; k--) {
169         ClassType ct = cl.getClassType(k);
170         if (ct instanceof TypeDeclaration) {
171             td = (TypeDeclaration) ct;
172             String JavaDoc unitName = UnitKit.getCompilationUnit(td).getName();
173             if (!unitName.matches(eaopPathRexp)) {
174             boolean superTransformed = false;
175             if (transformedClasses.contains(
176                       td.getSupertypes().getClassType(0)))
177                 superTransformed = true;
178             Instrumentation transform =
179                 new WrapConstructors(sc, td, superTransformed);
180             report = transform.execute();
181             transformedClasses.add(td);
182             }
183         }
184         }
185     }
186
187     return report; // only last report
188
}
189     
190     static ProblemReport wrapMethods(TypeDeclarationList tds) {
191     ProblemReport report = null;
192     TypeDeclaration td;
193
194     for (int j=0; j<tds.size(); j++) {
195         td = tds.getTypeDeclaration(j);
196         Instrumentation transform = new WrapMethods(sc, td);
197         report = transform.execute();
198     }
199
200     return report; // only last report
201
}
202
203     static ProblemReport insertEvents(TypeDeclarationList tds) {
204     ProblemReport report = null;
205     TypeDeclaration td;
206     
207     for (int j=0; j<tds.size(); j++) {
208         td = tds.getTypeDeclaration(j);
209         Instrumentation transform = new InsertEvents(sc, td);
210         report = transform.execute();
211     }
212     
213     return report; // only last report
214
}
215
216     static ProblemReport handleTermination(CompilationUnit cu) {
217     Instrumentation transform = new HandleTermination(sc, cu);
218     return transform.execute();
219     }
220
221     static void output() {
222     System.out.println(Format.toString("%u [%f]", cu));
223     try {
224         sc.getSourceFileRepository().print(cu);
225     } catch (IOException ioe) {
226         IO.warn("An IO Exception has occured: " + ioe);
227     }
228     }
229
230     public static SourceInfo getSourceInfo() {
231     return si;
232     }
233 }
234
Popular Tags