KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > wrappers > VerboseWrapper


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.wrappers;
19
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Date JavaDoc;
23 import org.aopalliance.intercept.ConstructorInvocation;
24 import org.aopalliance.intercept.MethodInvocation;
25 import org.objectweb.jac.aspects.gui.GuiAC;
26 import org.objectweb.jac.core.AspectComponent;
27 import org.objectweb.jac.core.Collaboration;
28 import org.objectweb.jac.core.Interaction;
29 import org.objectweb.jac.core.Jac;
30 import org.objectweb.jac.core.NameRepository;
31 import org.objectweb.jac.core.Wrapper;
32 import org.objectweb.jac.core.Wrapping;
33 import org.objectweb.jac.core.WrappingChain;
34 import org.objectweb.jac.core.rtti.MethodItem;
35 import org.objectweb.jac.util.Log;
36
37 /**
38  * This wrapper prints some informations about the wrapped method when
39  * it is called. */

40
41 public class VerboseWrapper extends Wrapper {
42
43     public static final int TIMED = 0;
44     public static final int NAMED = 1;
45     public static final int STACK = 2;
46     public static final int WRAPPERS= 3;
47
48     protected int type = TIMED;
49
50     static final String JavaDoc DISABLE_TRACE = "DISABLE_TRACE";
51
52     public VerboseWrapper(AspectComponent ac, int type) {
53         super(ac);
54         this.type = type;
55     }
56
57     /**
58      * A wrapping method that prints out the calling informations.
59      *
60      * @return the value of the called method */

61
62     public Object JavaDoc printCallingInfos(Interaction interaction) {
63
64         /** Before code ... */
65         Date JavaDoc d1 = new Date JavaDoc();
66         String JavaDoc on =
67             interaction.method.isStatic()
68                 ? ""
69                 : (" on '"
70                     + NameRepository.get().getName(interaction.wrappee)
71                     + "'");
72
73         System.out.println(
74             "<<< "
75                 + (d1.getTime() - Jac.getStartTime().getTime())
76                 + " ms: calling '" + interaction.method + "'"
77                 + on
78                 + ", args = "
79                 + Arrays.asList(interaction.args)
80                 + " >>>");
81         /** End before code */
82
83         Object JavaDoc ret = interaction.proceed();
84
85         /** After code ... */
86         Date JavaDoc d2 = new Date JavaDoc();
87         System.out.println(
88             "<<< "
89                 + (d2.getTime() - Jac.getStartTime().getTime())
90                 + " ms: returning '"
91                 + interaction.method
92                 + "'"
93                 + on
94                 + ", value = "
95                 + ret
96                 + ", duration = "
97                 + (d2.getTime() - d1.getTime())
98                 + " ms >>>");
99         /** End after code */
100
101         return ret;
102     }
103
104     public Object JavaDoc printNamedArgs(Interaction interaction) {
105         Collaboration collab = Collaboration.get();
106         if (collab.getAttribute(DISABLE_TRACE)!=null)
107             return proceed(interaction);
108         // printing of parameters can call the wrapped method
109
collab.addAttribute(DISABLE_TRACE, Boolean.TRUE);
110         try {
111             String JavaDoc[] paramNames = GuiAC.getParameterNames(interaction.method);
112             String JavaDoc params =
113                 interaction.method.isStatic()
114                 ? ""
115                 : "this=" + GuiAC.toString(interaction.wrappee);
116             Object JavaDoc[] args = interaction.args;
117             for (int i = 0; i < args.length; i++) {
118                 if (paramNames != null) {
119                     params += "," + paramNames[i] + "=" + GuiAC.toString(interaction.args[i]);
120             } else {
121                 params += ",arg[" + i + "]=" + GuiAC.toString(interaction.args[i]);
122             }
123             }
124             System.out.println("Calling " + interaction.method + " with " + params);
125         } finally {
126             collab.removeAttribute(DISABLE_TRACE);
127         }
128
129         Object JavaDoc result = proceed(interaction);
130
131         collab.addAttribute(DISABLE_TRACE, Boolean.TRUE);
132         try {
133             if (interaction.method instanceof MethodItem) {
134                 Class JavaDoc returnType = ((MethodItem) interaction.method).getType();
135                 if (returnType != void.class) {
136                     if (returnType.isArray()
137                         && Object JavaDoc.class.isAssignableFrom(
138                             returnType.getComponentType()))
139                         System.out.println(
140                             "Called "
141                             + interaction.method
142                             + " -> "
143                             + Arrays.asList((Object JavaDoc[]) result));
144                     else
145                     System.out.println(
146                         "Called " + interaction.method + " -> " + GuiAC.toString(result));
147                 }
148             }
149         } finally {
150             collab.removeAttribute(DISABLE_TRACE);
151         }
152         return result;
153     }
154
155     public Object JavaDoc printStackTrace(Interaction interaction) {
156         System.out.println(
157             "Calling '"
158                 + interaction.method
159                 + "' on '"
160                 + NameRepository.get().getName(interaction.wrappee)
161                 + "', args = "
162                 + Arrays.asList(interaction.args));
163         new Exception JavaDoc().printStackTrace();
164         return proceed(interaction);
165     }
166
167     public Object JavaDoc printWrappersTrace(Interaction interaction) {
168         String JavaDoc on =
169             interaction.method.isStatic()
170                 ? ""
171                 : (" on '"
172                     + NameRepository.get().getName(interaction.wrappee)
173                     + "'");
174
175         System.out.println(
176             "Calling '"
177                 + interaction.method
178                 + "'"
179                 + on
180                 + ", args = "
181                 + Arrays.asList(interaction.args));
182         WrappingChain chain =
183             Wrapping.getWrappingChain(interaction.wrappee, interaction.method);
184         System.out.println("WrappingChain = " + chain);
185         return proceed(interaction);
186     }
187
188     // TODO: support all the possible traces
189

190     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
191         switch (type) {
192             case TIMED:
193                 return printCallingInfos((Interaction) invocation);
194             case NAMED:
195                 return printNamedArgs((Interaction) invocation);
196             case STACK:
197                 return printStackTrace((Interaction) invocation);
198             case WRAPPERS:
199                 return printWrappersTrace((Interaction) invocation);
200             default:
201                 Log.error("VerboseWrapper: unknown type "+type);
202         }
203         return invocation.proceed();
204     }
205
206     public Object JavaDoc construct(ConstructorInvocation invocation)
207         throws Throwable JavaDoc {
208         return printCallingInfos((Interaction) invocation);
209     }
210 }
211
Popular Tags