KickJava   Java API By Example, From Geeks To Geeks.

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


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 import fr.emn.info.eaop.recoderaux.AuxListKit;
11
12 import recoder.*;
13 import recoder.abstraction.*;
14 import recoder.list.*;
15 import recoder.kit.*;
16 import recoder.java.*;
17 import recoder.java.declaration.*;
18 import recoder.java.reference.*;
19
20
21 /**
22  * Class constructing wrappers for methods which delegate
23  * to renamed versions of the original methods.
24  *
25  * Renamed original methods can then be
26  * instrumented using the transformation {@link InsertEvents}.
27  *
28  * @author MS
29  * @version 1.0
30  */

31 public class WrapMethods extends Instrumentation {
32
33     public WrapMethods(CrossReferenceServiceConfiguration sc,
34                TypeDeclaration td) {
35     super(sc, null, td);
36     }
37
38     /**
39      * Method executing the wrapping transformation. Build a
40      * delegatee for each constructor/method and delegate from the
41      * constructors/methods to the new respectifs methods.
42      */

43     public ProblemReport execute() {
44     SelectiveInstrumentation instrument = IO.getInstrumentationContext();
45     Method m;
46
47     if (instrument.checkClass(td)) {
48         MethodList ml = Main.getSourceInfo().getMethods(td);
49         
50         for (int i = 0, s = ml.size(); i < s; i += 1) {
51         m = ml.getMethod(i);
52         if (!(m instanceof MethodDeclaration) ||
53             (m instanceof Constructor)) continue;
54         MethodDeclaration md = (MethodDeclaration) m;
55
56         if (!instrument.checkMethodOrConstructor(md)) continue;
57         attach(buildDelegatee(md), td, td.getMembers().size());
58
59         if (!td.replaceChild(md, buildDelegator(md))) {
60             IO.fail("[execute] Method replacement");
61         };
62         }
63     }
64     
65     return Transformation.NO_PROBLEM;
66     }
67
68     /**
69      * Construct a delegator from a constructor declaration.
70      *
71      * @return A delegator (method declaration) for the
72      * argument constructor.
73      */

74     MethodDeclaration buildDelegator(MethodDeclaration md) {
75     MethodDeclaration newMd = (MethodDeclaration) md.deepClone();
76
77     Identifier id = new Identifier(md.getName() + origStr);
78     ExpressionMutableList el = MethodKit.createArguments(md);
79
80     StatementArrayList sl =
81         new StatementArrayList(new MethodReference(id, el));
82     StatementBlock sb = new StatementBlock(sl);
83
84     if (!newMd.replaceChild(newMd.getBody(), sb)) {
85         IO.fail("[buildDelegator] Body replacement");
86     };
87
88     return newMd;
89     }
90
91     /**
92      * Construct a delegatee from a member declaration.
93      *
94      * @return A delegatee (method declaration) for constructors/methods
95      * or null if another member has been encountered.
96      */

97     MethodDeclaration buildDelegatee(MethodDeclaration m) {
98     MethodDeclaration res = null;
99
100     Identifier id =
101         new Identifier(m.getIdentifier().getText() + origStr);
102     res = (MethodDeclaration) m.deepClone();
103     res.setIdentifier(id);
104
105     return res;
106     }
107 }
108
109
Popular Tags