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 java.util.*;
26  
27  import javassist.CannotCompileException;
28  import javassist.ClassPool;
29  import javassist.CtClass;
30  import javassist.CtConstructor;
31  import javassist.CtMethod;
32  import javassist.NotFoundException;
33  
34  /***
35   * TODO Add docs
36   *
37   * Contributing Author(s):
38   *
39   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
40   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
41   *
42   * @author michaellif
43   * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/05 02:25:20 $
44   */
45  public class ExceptionCatcherInstrumentor extends AbstractInstrumentor {
46      /***   DOCUMENT ME!   */
47      private ArrayList exceptions = new ArrayList();
48  
49      /***
50       * Creates a new ExceptionCatcherInstrumentor object.
51       */
52      public ExceptionCatcherInstrumentor() {
53      }
54  
55      /***
56       * DOCUMENT ME!
57       *
58       * @param exception DOCUMENT ME!
59       */
60      public void addExceptionType(String exception) {
61          exceptions.add(exception);
62      }
63  
64      /***
65       * DOCUMENT ME!
66       *
67       * @param clazz DOCUMENT ME!
68       *
69       * @throws InterceptorException DOCUMENT ME!
70       */
71      public boolean instrumentClass(CtClass clazz) throws InterceptorException {
72      	return false;
73      }
74  
75      /***
76       * DOCUMENT ME!
77       *
78       * @param clazz DOCUMENT ME!
79       * @param method DOCUMENT ME!
80       *
81       * @throws InterceptorException DOCUMENT ME!
82       */
83      public boolean instrumentMethod(CtClass clazz, CtMethod method)
84          throws InterceptorException {
85          if (method.isEmpty()) {
86              return false;
87          }
88  
89          try {
90          	boolean modified = false;
91              for (Iterator iter = exceptions.iterator(); iter.hasNext();) {
92                  String exception = (String) iter.next();
93                  addCatch(clazz, method, exception);
94  				modified = true;
95              }
96  			return modified;
97          } catch (Exception e) {
98              e.printStackTrace();
99              throw new InterceptorException("Failed to add catch to method " +
100                 method + " of class " + clazz);
101         }
102     }
103 
104     /***
105      * DOCUMENT ME!
106      *
107      * @param clazz DOCUMENT ME!
108      * @param method DOCUMENT ME!
109      * @param exception DOCUMENT ME!
110      *
111      * @throws NotFoundException DOCUMENT ME!
112      * @throws CannotCompileException DOCUMENT ME!
113      */
114     private void addCatch(CtClass clazz, CtMethod method, String exception)
115         throws NotFoundException, CannotCompileException {
116         String mname = method.getName();
117         CtClass etype = ClassPool.getDefault().get(exception);
118         StringBuffer code = new StringBuffer(300);
119         code.append("{ System.out.println(\"Exception ").append(exception)
120             .append(" at ");
121         code.append(clazz.getName()).append(".").append(mname).append("\");");
122         code.append(" throw $e; }");
123         method.addCatch(code.toString(), etype);
124     }
125 
126     /***
127      * DOCUMENT ME!
128      *
129      * @param clazz DOCUMENT ME!
130      * @param constructor DOCUMENT ME!
131      *
132      * @throws InterceptorException DOCUMENT ME!
133      */
134     public boolean instrumentConstructor(CtClass clazz, CtConstructor constructor)
135         throws InterceptorException {
136 			return false;
137     }
138     
139 }