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 javassist.CannotCompileException;
24  import javassist.CtClass;
25  import javassist.CtConstructor;
26  import javassist.CtMethod;
27  import javassist.CtNewMethod;
28  import javassist.NotFoundException;
29  import net.sf.jour.InterceptorException;
30  
31  /**
32   *
33   * Contributing Author(s):
34   *
35   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
36   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
37   *
38   * @author vlads
39   * @version $Revision: 46 $ ($Author: vlads $) $Date: 2007-08-28 14:30:40 -0400 (Tue, 28 Aug 2007) $
40   */
41  public class InstanceCounterInstrumentor extends AbstractInstrumentor
42      implements InstrumentorConsts {
43  
44      public boolean instrumentClass(CtClass clazz) throws InterceptorException {
45          try {
46              addCounterDecrementer(clazz);
47  			return true;
48          } catch (Exception e) {
49              e.printStackTrace();
50              throw new InterceptorException(
51                  "Failed to add instance counter member to class " + clazz);
52          }
53      }
54  
55      public boolean instrumentMethod(CtClass clazz, CtMethod method)
56          throws InterceptorException {
57  			return false;
58      }
59  
60      public boolean instrumentConstructor(CtClass clazz, CtConstructor constructor)
61          throws InterceptorException {
62          if (constructor.isClassInitializer()) {
63              return false;
64          }
65  
66          try {
67              addCounterIncrementer(clazz, constructor);
68              return true;
69          } catch (Exception e) {
70              e.printStackTrace();
71              throw new InterceptorException(
72                  "Failed to add instance counter incrementor to constructor of class " +
73                  clazz);
74          }
75      }
76  
77      private void addCounterIncrementer(CtClass clazz, CtConstructor constructor)
78          throws NotFoundException, CannotCompileException {
79          //String cname = constructor.getName();
80          String code = "net.sf.jour.rt.agent.Elog.logEvent(new net.sf.jour.rt.agent.InstanceCounterEvent(this.getClass(), true));";
81          constructor.insertAfter(code);
82      }
83  
84      private void addCounterDecrementer(CtClass clazz)
85          throws CannotCompileException {
86          CtMethod finalize;
87          try {
88  	        finalize = clazz.getDeclaredMethod("finalize",
89  	                new CtClass[] {});
90  	        if (finalize != null) {
91  	            String code = "net.sf.jour.rt.agent.Elog.logEvent(new net.sf.jour.rt.agent.InstanceCounterEvent(this.getClass(), false));";
92  	            finalize.insertAfter(code, true);
93  	        }
94          } catch (NotFoundException nfe) {
95              StringBuffer code = new StringBuffer(
96                      "protected void finalize() throws Throwable { super.finalize(); ");
97              code.append(
98                  "net.sf.jour.rt.agent.Elog.logEvent(new net.sf.jour.rt.agent.InstanceCounterEvent(this.getClass(), false));}");
99              finalize = CtNewMethod.make(code.toString(), clazz);
100             clazz.addMethod(finalize);
101         }
102     }
103     
104 }