KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > asm > TraceCodeGenerator


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia.asm;
25
26 import org.objectweb.fractal.julia.loader.Initializable;
27 import org.objectweb.fractal.julia.loader.Tree;
28
29 import org.objectweb.asm.CodeVisitor;
30
31 import java.lang.reflect.Method JavaDoc;
32
33 /**
34  * A {@link CodeGenerator} to generate pre and post code to trace
35  * method calls. More precisely the code adapters returned by the {@link
36  * #generateInterceptionCode generateInterceptionCode} method (see {@link
37  * CodeGenerator}) transform the original methods into methods of
38  * the following form:
39  * <pre>
40  * &lt;method signature&gt; {
41  * &lt;return type&gt; result;
42  * System.err.println("Entering &lt;method signature&gt;");
43  * // original method code, where returns are replaced with gotos
44  * System.err.println("Leaving &lt;method signature&gt;");
45  * return result;
46  * }
47  * </pre>
48  */

49
50 public class TraceCodeGenerator extends AbstractCodeGenerator
51   implements Initializable
52 {
53
54   /**
55    * The type of this code generator.
56    */

57
58   private int type = IN;
59
60   // -------------------------------------------------------------------------
61
// Implementation of the Init interface
62
// -------------------------------------------------------------------------
63

64   /**
65    * Initializes this object with the given arguments.
66    *
67    * @param args the type of this code generator. This tree must be of the form
68    * "(in|out|inout)".
69    */

70
71   public void initialize (final Tree args) {
72     String JavaDoc type = args.getSubTree(0).toString();
73     if (type.equals("in")) {
74       this.type = IN;
75     } else if (type.equals("out")) {
76       this.type = OUT;
77     } else if (type.equals("inout")) {
78       this.type = IN_OUT;
79     }
80   }
81
82   // -------------------------------------------------------------------------
83
// Overriden methods
84
// -------------------------------------------------------------------------
85

86   public int init (final InterceptorClassGenerator icg) {
87     return type;
88   }
89
90   // -------------------------------------------------------------------------
91
// Implementation of inherited abstract methods
92
// -------------------------------------------------------------------------
93

94   protected void generateInterceptionCodeBlock (
95     final Method JavaDoc m,
96     final boolean pre,
97     final CodeVisitor cv,
98     final int formals)
99   {
100     // pushes the System.err static field
101
cv.visitFieldInsn(
102       GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
103     if (pre) {
104       // pushes the "Entering ..." String constant
105
cv.visitLdcInsn("Entering " + m);
106     } else {
107       // pushes the "Leaving ..." String constant
108
cv.visitLdcInsn("Leaving " + m);
109     }
110     // invokes the 'println' method (defined in the PrintStream class)
111
cv.visitMethodInsn(
112       INVOKEVIRTUAL,
113       "java/io/PrintStream",
114       "println",
115       "(Ljava/lang/String;)V");
116   }
117 }
118
Popular Tags