View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.instrumentor;
22  
23  import net.sf.jour.InterceptorException;
24  
25  import javassist.*;
26  import javassist.CannotCompileException;
27  import javassist.CtConstructor;
28  import javassist.CtMethod;
29  import javassist.NotFoundException;
30  
31  /***
32   * TODO Add docs
33   *
34   * Contributing Author(s):
35   *
36   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
37   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
38   *
39   * @author vlads
40   * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/05 02:25:20 $
41   */
42  public class InstanceCounterInstrumentor extends AbstractInstrumentor
43      implements InstrumentorConsts {
44      /***
45       * DOCUMENT ME!
46       *
47       * @param clazz DOCUMENT ME!
48       *
49       * @throws InterceptorException DOCUMENT ME!
50       */
51      public boolean instrumentClass(CtClass clazz) throws InterceptorException {
52          try {
53              addCounterDecrementer(clazz);
54  			return true;
55          } catch (Exception e) {
56              e.printStackTrace();
57              throw new InterceptorException(
58                  "Failed to add instance counter member to class " + clazz);
59          }
60      }
61  
62      /***
63       * DOCUMENT ME!
64       *
65       * @param clazz DOCUMENT ME!
66       * @param method DOCUMENT ME!
67       *
68       * @throws InterceptorException DOCUMENT ME!
69       */
70      public boolean instrumentMethod(CtClass clazz, CtMethod method)
71          throws InterceptorException {
72  			return false;
73      }
74  
75      /***
76       * DOCUMENT ME!
77       *
78       * @param clazz DOCUMENT ME!
79       * @param constructor DOCUMENT ME!
80       *
81       * @throws InterceptorException DOCUMENT ME!
82       */
83      public boolean instrumentConstructor(CtClass clazz, CtConstructor constructor)
84          throws InterceptorException {
85          if (constructor.isClassInitializer()) {
86              return false;
87          }
88  
89          try {
90              addCounterIncrementer(clazz, constructor);
91              return true;
92          } catch (Exception e) {
93              e.printStackTrace();
94              throw new InterceptorException(
95                  "Failed to add instance counter incrementor to constructor of class " +
96                  clazz);
97          }
98      }
99  
100     /***
101      * DOCUMENT ME!
102      *
103      * @param clazz DOCUMENT ME!
104      * @param constructor DOCUMENT ME!
105      *
106      * @throws NotFoundException DOCUMENT ME!
107      * @throws CannotCompileException DOCUMENT ME!
108      */
109     private void addCounterIncrementer(CtClass clazz, CtConstructor constructor)
110         throws NotFoundException, CannotCompileException {
111         String cname = constructor.getName();
112         String code = "net.sf.jour.rt.agent.Elog.logEvent(new net.sf.jour.rt.agent.InstanceCounterEvent(this.getClass(), true));";
113         constructor.insertAfter(code);
114     }
115 
116     /***
117      * DOCUMENT ME!
118      *
119      * @param clazz DOCUMENT ME!
120      *
121      * @throws NotFoundException DOCUMENT ME!
122      * @throws CannotCompileException DOCUMENT ME!
123      */
124     private void addCounterDecrementer(CtClass clazz)
125         throws CannotCompileException {
126         CtMethod finalize;
127         try {
128 	        finalize = clazz.getDeclaredMethod("finalize",
129 	                new CtClass[] {});
130 	        if (finalize != null) {
131 	            String code = "net.sf.jour.rt.agent.Elog.logEvent(new net.sf.jour.rt.agent.InstanceCounterEvent(this.getClass(), false));";
132 	            finalize.insertAfter(code, true);
133 	        }
134         } catch (NotFoundException nfe) {
135             StringBuffer code = new StringBuffer(
136                     "protected void finalize() throws Throwable { super.finalize(); ");
137             code.append(
138                 "net.sf.jour.rt.agent.Elog.logEvent(new net.sf.jour.rt.agent.InstanceCounterEvent(this.getClass(), false));}");
139             finalize = CtNewMethod.make(code.toString(), clazz);
140             clazz.addMethod(finalize);
141         }
142     }
143     
144 }