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;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.InputStream;
25  
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  
29  import javassist.ClassPath;
30  import javassist.ClassPool;
31  import javassist.CtClass;
32  
33  
34  /***
35   * DOCUMENT ME!
36   *
37   * @author $author$
38   * @version $Revision: 1.3 $
39   */
40  public class EmptyClassClassPath implements ClassPath {
41      /***
42       * Creates a new EmptyClassClassPath object.
43       */
44      public EmptyClassClassPath() {
45      }
46  
47      /***
48       * DOCUMENT ME!
49       *
50       * @param className DOCUMENT ME!
51       *
52       * @return DOCUMENT ME!
53       */
54      public InputStream openClassfile(String className) {
55          return new ByteArrayInputStream(createEmptyClass(className));
56      }
57  
58      /***
59       * DOCUMENT ME!
60       *
61       * @param className DOCUMENT ME!
62       *
63       * @return DOCUMENT ME!
64       */
65      private static byte[] createEmptyClass(String className) {
66          try {
67              ClassPool pool = new ClassPool(null);
68              pool.appendSystemPath();
69  
70              CtClass clazz = pool.makeClass(className);
71  
72              return clazz.toBytecode();
73          } catch (Exception e) {
74  			// Java 1.3
75              throw new RuntimeException(e.getMessage());
76          }
77      }
78  
79      /***
80       * DOCUMENT ME!
81       *
82       * @param classname DOCUMENT ME!
83       *
84       * @return DOCUMENT ME!
85       */
86      public URL find(String classname) {
87          String cname = classname.replace('.', '/') + ".class";
88  
89          try {
90              return new URL("file:/ByteArrayClassPath/" + cname);
91          } catch (MalformedURLException e) {
92          }
93  
94          return null;
95      }
96  
97      /***
98       * DOCUMENT ME!
99       */
100     public void close() {
101     }
102 }