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 java.util.ArrayList;
24  import java.util.Iterator;
25  
26  import javassist.CannotCompileException;
27  import javassist.ClassPool;
28  import javassist.CtClass;
29  import javassist.CtConstructor;
30  import javassist.CtMethod;
31  import javassist.NotFoundException;
32  import net.sf.jour.InterceptorException;
33  
34  /**
35   *
36   * Contributing Author(s):
37   *
38   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
39   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
40   *
41   * @author michaellif
42   * @version $Revision: 46 $ ($Author: vlads $) $Date: 2007-08-28 14:30:40 -0400 (Tue, 28 Aug 2007) $
43   */
44  public class ExceptionCatcherInstrumentor extends AbstractInstrumentor {
45  
46      private ArrayList exceptions = new ArrayList();
47      
48      private String code;
49  
50      /**
51       * Creates a new ExceptionCatcherInstrumentor object.
52       */
53      public ExceptionCatcherInstrumentor() {
54      }
55  
56      public void exceptionType(String exception) {
57          exceptions.add(exception);
58      }
59      
60  	public void code(String code) {
61  		this.code = code;
62  	}
63  
64      public boolean instrumentClass(CtClass clazz) throws InterceptorException {
65      	return false;
66      }
67  
68      public boolean instrumentMethod(CtClass clazz, CtMethod method)
69          throws InterceptorException {
70          if (method.isEmpty()) {
71              return false;
72          }
73  
74          try {
75          	boolean modified = false;
76              for (Iterator iter = exceptions.iterator(); iter.hasNext();) {
77                  String exception = (String) iter.next();
78                  addCatch(clazz, method, exception);
79  				modified = true;
80              }
81  			return modified;
82          } catch (Exception e) {
83              e.printStackTrace();
84              throw new InterceptorException("Failed to add catch to method " +
85                  method + " of class " + clazz);
86          }
87      }
88  
89      private void addCatch(CtClass clazz, CtMethod method, String exception)
90          throws NotFoundException, CannotCompileException {
91          String mname = method.getName();
92          CtClass etype = ClassPool.getDefault().get(exception);
93          StringBuffer codeBuffer = new StringBuffer();
94          if (this.code == null) {
95          	codeBuffer.append("{ System.out.println(\"Exception ").append(exception).append(" at ");
96          	codeBuffer.append(clazz.getName()).append(".").append(mname).append("\");");
97          	codeBuffer.append(" throw $e; }");
98          } else {
99          	codeBuffer.append(this.code);
100         }
101         method.addCatch(codeBuffer.toString(), etype);
102     }
103 
104     public boolean instrumentConstructor(CtClass clazz, CtConstructor constructor)
105         throws InterceptorException {
106 			return false;
107     }
108    
109 }