KickJava   Java API By Example, From Geeks To Geeks.

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


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.IO;
10
11 import recoder.*;
12 import recoder.abstraction.*;
13 import recoder.kit.*;
14 import recoder.list.*;
15 import recoder.java.*;
16 import recoder.java.declaration.*;
17
18
19 /**
20  * Transformation handling termination issues of EAOP programs
21  *
22  * @author MS
23  * @version 1.0
24  */

25 public class HandleTermination extends Instrumentation {
26
27     public HandleTermination(CrossReferenceServiceConfiguration sc,
28                  CompilationUnit cu) {
29     super(sc, cu, null);
30     }
31
32     // Attention: execute simply tests for the name "main" (MS)
33

34     /**
35      * Transformation ensuring termination of EAOP programs.
36      *
37      * Since aspects each have their own thread, an EAOP program does not
38      * simply terminate when the base program execution stops.
39      * Special action has to be taken to terminate the aspect threads
40      * after the base program execution has stopped.
41      */

42     public ProblemReport execute() {
43     for (int i=0; i<cu.getTypeDeclarationCount(); i++) {
44         TypeDeclaration td = cu.getTypeDeclarationAt(i);
45         MethodList ml = td.getMethods();
46         for (int j=0; j<ml.size(); j++) {
47         Method m = ml.getMethod(j);
48         if (m.getName().equals("main")) {
49             if (!(m instanceof MethodDeclaration))
50             IO.fail("[execute] No valid main method");
51             MethodDeclaration md = (MethodDeclaration) m;
52             StatementBlock sb = md.getBody();
53             StatementMutableList sl = sb.getBody();
54             try {
55             sl.add(pf.parseStatements("System.exit(0);"));
56             } catch (ParserException e) {
57             IO.fail("[execute] Parsing for termination handling");
58             }
59             if (!md.replaceChild(sb, new StatementBlock(sl)))
60             IO.fail("[execute] Termination handling for main");
61         }
62         }
63     }
64
65     return Transformation.NO_PROBLEM;
66     }
67 }
68
69
Popular Tags